Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

html - How to ensure the background image inside a DIV is evenly shown

I have the following code in my ASP.net page (I would like it to be responsive):

<div class="mobileTabsNav">
    <div style="width: 100%; height: auto; min-height: 50px; max-height: 50px; background: #00ff00; background: url('/theImages/mobileMWBtn.png'); background-size: cover; text-align: center;">
        PLEASE SIGN IN
    </div>
</div>

CSS:

.mobileTabsNav
{
    display: none;
    width: 100%;
    overflow: hidden;
}
@media (max-width: 715px)
{
    .mobileTabsNav
    {
        display: block;
    }
}

It seems the bottom portion of the image is getting cut off:

enter image description here

When I change it to background-size: 100% 100%;, the image is stretched too much on certain screen as in the example shown here:

enter image description here

As you can see the image is distorted.

Is there anyway to ensure the curve is the same across the board of different screen sizes or do I have to add three separate images?

The image being used on the Fiddle:

enter image description here

enter image description here enter image description here enter image description here

.mobileTabsNav
{
    width: 100%;
    overflow: hidden;
}
<div class="mobileTabsNav">
                        <div style="float: left; background: url('http://i.stack.imgur.com/NnCbY.png') no-repeat; min-height: 50px; min-width: 10px; max-width: 10px; background-size: contain;">

                        </div>
    <div style="background: url('http://i.stack.imgur.com/KbPm8.png')"></div>
                        <div style="float: right; background: url('http://i.stack.imgur.com/rll1d.png') no-repeat; min-height: 50px; min-width: 10px; max-width: 10px; background-size: contain;">

                        </div>
                    </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I would do is create a background image that works well as a repeated pattern (tileable) and then use the border-radius property to get the rounded corners.

In this method, the dots will always be the same size regardless of the screen size, and you only need a single image for all devices.

.mobileTabsNav {
}
.mobileTabsNav .inner {
  min-height: 400px; 
  background: #00ff00 url(http://i.stack.imgur.com/M8WDz.png); 
  text-align: center;
  border-radius: 25px;
}
<div class="mobileTabsNav">
    <div class="inner">
        PLEASE SIGN IN
    </div>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...