2015-08-29 167 views
1

晚上好, 我正在网站上使用“单页布局”。而且我对移动响应性有很大的问题。特别是在菜单和背景图片部分。我会发布我的代码,请给我一些建议,编辑什么? 非常感谢!如何设置背景图像响应

HTML:

<div id="menu"> 
<div id="content"> 
<div id="logo"> 
<a href="#uvod"><img src="style/img/logo.png" border="0"></a> 
</div> 
<div id="nabidka"> 
<ul> 
    <li href="about"><a href='#about'>O nás</a></li> 
    <li href="cenik"><a href='#cenik'>Ceník</a></li> 
    <li href="sluzby"><a href='#sluzby'>Služby</a></li> 
    <li href="kontakt"><a href='#kontakt'>Kontakt</a></li> 
</ul> 
</div> 
<div id="socsite"> 
<table style="width: 210px; height: 80px;" border="0"> 
<tr><td width="70"><a class="fb" href=""></a> </td> 
<td width="70"><a class="twitter" href=""></a> </td> 
<td width="70"><a class="youtube" href=""></a>  </td></tr> 
</table> 

</div> 
</div> 
</div> 

<div id="uvod" class="section"> 
<div id="content"> 
...text text text... 
    </div> 
</div> 

和CSS:

#uvod { 
background:url('img/bg-uvod.png') top no-repeat fixed; 
float:left; 
width: 100%; 
min-height: 100%; 
z-index: 1; 
} 

#menu { 
background-color: #535353; 
width: 100%; 
height: 90px; 
    padding: 0; 
    margin: 0 auto; 
    position: fixed; 
top: 0; 
z-index: 100; 
} 

#logo { 

width: 200px; 
height: 90px; 
margin-left: 1em; 
    padding-top: 5px; 
    float: left; 
} 
#nabidka { 

width:550px; 
height: 90px; 

margin-left: 1.5em; 
    padding-top: 5px; 
float:left; 

} 
#socsite { 

width:240px; 
height: 90px; 
    padding-top: 5px; 
    margin-left: 7em; 
    float: left; 

} 

#content { 
width: 1200px; 
margin: 0 auto; 
} 

回答

3
#uvod 
{ 
    background: url('img/bg-uvod.png') no-repeat center center fixed; 
    -webkit-background-size: cover; 
    -moz-background-size: cover; 
    -o-background-size: cover; 
    background-size: cover; 
} 

为什么不试试封面?如果这不起作用,请尝试在背景中放置绝对位置div,然后设置背景。

+1

谢谢,它确实有帮助,但仅限于身高。当我在手机上打开它时,我使用背景图片的部分没有100%的宽度。菜单有1200px,也有白色背景的部分,但图像背景有75%的宽度...第二个问题,当我缩小菜单pannel,按钮下来,在灰色的面板菜单和社交网络的图标...什么我应该这样做吗?我把它固定,我给它固定宽度(1200px)... –

+0

我建议在你的内容的背景中放置一个位置:绝对div并使其成为你需要的大小。然后将上述背景应用到这个新的背景div。 – Kelsey

0

你将不得不使用规则:

background-size: cover;

添加到您的#uvod

这将覆盖整个屏幕的宽度,当屏幕缩小时,屏幕会随之缩小。

0

查看background-size属性。具体来说,就是containcover的值。

看一看上MDN文档的详细: https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

这是一个很好的阅读,以及: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images

另外,一定要看看浏览器的兼容性表在底部第一个链接,并确保它涵盖您正在寻找的浏览器。

如果你只是针对现代浏览器,我可能还建议考虑使用SVG作为你的背景图片,因为它们在任何尺寸上都很好看。