CSS背景拉伸到窗口底部?
我正在帮助重新设计一个网站 www.liveatsheastadium.com/test
我正在尝试找出一种方法来获取透明背景(使用background:rgba 属性)可拉伸页面的整个高度,无论分辨率如何。
我尝试将容器、html 和正文高度设置为 100% 并使用 background:cover 属性。
有关如何使背景延伸到页面底部(无论浏览器窗口大小如何)的任何想法?
<body>
<div id="container">
<div id="mainContent">
<ul id="nav">
<li><img src="images/nav/archives.jpg" /><a href="archives.html" title="archives">archives</a></li>
<li><img src="images/nav/calendar.jpg" /><a href="calendar.html" title="calendar">calendar</a></li>
<li><img src="images/nav/studio.jpg" /><a href="studio.html" title="studio" >studio</a></li>
<li><img src="images/nav/blog.jpg" /><a href="http://sheastadiumbk.blogspot.com/" title="blog">blog</a></li>
<li><img src="images/nav/contact.jpg" /><a href="contact.html" title="contact" >contact</a></li>
</ul>
<div class="blogPost ">
<h2><a href="#">The Lebonese, Tits, and The Chinese at Shea tonight</a></h2>
<p class="date">posted on 7.15.11</p>
<h3>Face Melting, coke snorting good times</h3>
<div class="blogBody">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Borem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!--END BLOGBODY-->
<div class="meta">
<ul>
<li><a href="http://google.com ">read more...</a></li>
<li><a href="">Tweet This</a></li>
<li><a href="">re-post on facebook</a></li>
</ul>
</div><!--end META-->
</div><!--END BLOGPOST-->
</div><!--END MAIN CONTENT-->
<div id="aside">
<ul id="socialIcons">
<li><a href="http//twitter.com"><img src="images/icons/facebook.png" alt=""/></a></li>
<li><a href="http//facebook.com"><img src="images/icons/twitter.png" alt=""/></a></li>
<li><a href="subscribeToMailingList"><img src="images/icons/rss.png" alt=""/></a></li>
</ul>
<div id="tabs">
<div id="latest">
<h2>Latest</h2>
<ul>
<li><a href="">liturgy - 7.04.11</a></li>
<li><a href="">sightings - 7.04.11 (full set)</a></li>
<li><a href="">liturgy - 7.04.11</a></li>
<li><a href="">sightings - 7.04.11 (full set)</a></li>
<li><a href="">liturgy - 7.04.11</a></li>
</ul>
</div>
<div id="sheaFaves">
<h2>Shea Faves</h2>
<ul>
<li><a href="">future islands - 7.4.10</a></li>
<li><a href="">Small Black - 10.15.10</a></li>
<li><a href="">future islands - 7.4.10</a></li>
<li><a href="">Small Black - 10.15.10</a></li>
<li><a href="">future islands - 7.4.10</a></li>
</ul>
</div>
</div><!--END TABS-->
<div id="featuredBand">
<h2>Featured Band:</h2>
<img src="http://www.placehold.it/100" />
<h3><a href="">The Chinese</a></h3>
<p>The Chinese are a Brooklyn Based band whose style mixes blah blah blah blah blah.</p>
</div>
<div id="guestPlaylist">
<h2>Guest Playlist</h2>
<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F19242183&show_comments=false&auto_play=false&color=000000"></param> <param name="allowscriptaccess" value="always"></param> <param name="wmode" value="window"></param><embed wmode="window" allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F19242183&show_comments=false&auto_play=false&color=000000" type="application/x-shockwave-flash" width="100%"></embed> </object> <span><a href="http://soundcloud.com/shlohmo/marvins-room-shlohmos-thru-tha">Marvin's Room (Shlohmo's thru tha floor remix) - Drake</a> by <a href="http://soundcloud.com/shlohmo">shlohmo</a></span>
</div>
</div><!--END ASIDE-->
</div><!--END CONTAINER-->
还有CSS
body {
background: #000 url('../images/bg-light.jpg') no-repeat top left fixed;
}
#container {
background:rgba(0,0,0,0.65);
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:relative;
overflow:hidden;
width:1080px;
margin-left:auto;
margin-right:auto;
}
#nav {
padding:15px 0px;
width:100%;
overflow:hidden;
margin-bottom:10px;
}
#nav li {
text-align:center;
width:110px;
float:left;
}
#nav img {
width:60px;
height:60px;
}
#nav a {
font-family:'Oswald', arial, sans-serif;
margin-top:5px;
display:block;
font-size:.80em;
font-weight:500;
letter-spacing: .3em;
text-decoration:none;
color:#ffffff;
text-transform:uppercase;
}
#nav a:hover {
color:#fff;
text-shadow: 2px 2px 3px #bebebe;
}
#socialIcons li {
display:inline;
margin-right:15px;
}
#socialIcons img {
width:50px;
}
#mainContent {
width:70%;
float:left;
}
#aside {
overflow:hidden;
width:30%;
float:right;
}
#tabs, #guestPlaylist, #featuredBand {
margin-bottom:50px;
padding-right:15px;
}
#tabs {
margin-top:115px;
font-family: 'Oswald', arial, sans-serif;
}
#latest, #sheaFaves {
float:left;
width:50%;
}
#aside h2 {
border-bottom:1px dotted #fff;
color:#fff;
letter-spacing:.1em;
margin-bottom:10px;
}
#latest li a, #sheaFaves li a {
font-size:.75em;
}
#tabs-1 li, #tabs-2 li {
letter-spacing:.1em;
font-size:.65em;
line-height:1.3em;
}
#featuredBand {
margin-top:260px;
clear:both;
overflow:hidden;
}
#featuredBand h2 {
margin-bottom:15px;
}
#featuredBand img {
float:left;
width:40%;
margin-right:10px;
}
#featuredBand h3 {
margin-bottom:5px;
}
#featuredBand p {
line-height:1.2em;
}
I am helping to redesign a website www.liveatsheastadium.com/test
I am trying to figure out a way to get the transparent background (using the background:rgba property) to stretch the entire height of the page regardless of resolution.
I have tried setting the container, html and body height to 100% and using the background:cover property.
Any ideas on how to get the background to extend to the bottom of the page regardless of broswer window size?
<body>
<div id="container">
<div id="mainContent">
<ul id="nav">
<li><img src="images/nav/archives.jpg" /><a href="archives.html" title="archives">archives</a></li>
<li><img src="images/nav/calendar.jpg" /><a href="calendar.html" title="calendar">calendar</a></li>
<li><img src="images/nav/studio.jpg" /><a href="studio.html" title="studio" >studio</a></li>
<li><img src="images/nav/blog.jpg" /><a href="http://sheastadiumbk.blogspot.com/" title="blog">blog</a></li>
<li><img src="images/nav/contact.jpg" /><a href="contact.html" title="contact" >contact</a></li>
</ul>
<div class="blogPost ">
<h2><a href="#">The Lebonese, Tits, and The Chinese at Shea tonight</a></h2>
<p class="date">posted on 7.15.11</p>
<h3>Face Melting, coke snorting good times</h3>
<div class="blogBody">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Borem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div><!--END BLOGBODY-->
<div class="meta">
<ul>
<li><a href="http://google.com ">read more...</a></li>
<li><a href="">Tweet This</a></li>
<li><a href="">re-post on facebook</a></li>
</ul>
</div><!--end META-->
</div><!--END BLOGPOST-->
</div><!--END MAIN CONTENT-->
<div id="aside">
<ul id="socialIcons">
<li><a href="http//twitter.com"><img src="images/icons/facebook.png" alt=""/></a></li>
<li><a href="http//facebook.com"><img src="images/icons/twitter.png" alt=""/></a></li>
<li><a href="subscribeToMailingList"><img src="images/icons/rss.png" alt=""/></a></li>
</ul>
<div id="tabs">
<div id="latest">
<h2>Latest</h2>
<ul>
<li><a href="">liturgy - 7.04.11</a></li>
<li><a href="">sightings - 7.04.11 (full set)</a></li>
<li><a href="">liturgy - 7.04.11</a></li>
<li><a href="">sightings - 7.04.11 (full set)</a></li>
<li><a href="">liturgy - 7.04.11</a></li>
</ul>
</div>
<div id="sheaFaves">
<h2>Shea Faves</h2>
<ul>
<li><a href="">future islands - 7.4.10</a></li>
<li><a href="">Small Black - 10.15.10</a></li>
<li><a href="">future islands - 7.4.10</a></li>
<li><a href="">Small Black - 10.15.10</a></li>
<li><a href="">future islands - 7.4.10</a></li>
</ul>
</div>
</div><!--END TABS-->
<div id="featuredBand">
<h2>Featured Band:</h2>
<img src="http://www.placehold.it/100" />
<h3><a href="">The Chinese</a></h3>
<p>The Chinese are a Brooklyn Based band whose style mixes blah blah blah blah blah.</p>
</div>
<div id="guestPlaylist">
<h2>Guest Playlist</h2>
<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F19242183&show_comments=false&auto_play=false&color=000000"></param> <param name="allowscriptaccess" value="always"></param> <param name="wmode" value="window"></param><embed wmode="window" allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F19242183&show_comments=false&auto_play=false&color=000000" type="application/x-shockwave-flash" width="100%"></embed> </object> <span><a href="http://soundcloud.com/shlohmo/marvins-room-shlohmos-thru-tha">Marvin's Room (Shlohmo's thru tha floor remix) - Drake</a> by <a href="http://soundcloud.com/shlohmo">shlohmo</a></span>
</div>
</div><!--END ASIDE-->
</div><!--END CONTAINER-->
And the CSS
body {
background: #000 url('../images/bg-light.jpg') no-repeat top left fixed;
}
#container {
background:rgba(0,0,0,0.65);
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
position:relative;
overflow:hidden;
width:1080px;
margin-left:auto;
margin-right:auto;
}
#nav {
padding:15px 0px;
width:100%;
overflow:hidden;
margin-bottom:10px;
}
#nav li {
text-align:center;
width:110px;
float:left;
}
#nav img {
width:60px;
height:60px;
}
#nav a {
font-family:'Oswald', arial, sans-serif;
margin-top:5px;
display:block;
font-size:.80em;
font-weight:500;
letter-spacing: .3em;
text-decoration:none;
color:#ffffff;
text-transform:uppercase;
}
#nav a:hover {
color:#fff;
text-shadow: 2px 2px 3px #bebebe;
}
#socialIcons li {
display:inline;
margin-right:15px;
}
#socialIcons img {
width:50px;
}
#mainContent {
width:70%;
float:left;
}
#aside {
overflow:hidden;
width:30%;
float:right;
}
#tabs, #guestPlaylist, #featuredBand {
margin-bottom:50px;
padding-right:15px;
}
#tabs {
margin-top:115px;
font-family: 'Oswald', arial, sans-serif;
}
#latest, #sheaFaves {
float:left;
width:50%;
}
#aside h2 {
border-bottom:1px dotted #fff;
color:#fff;
letter-spacing:.1em;
margin-bottom:10px;
}
#latest li a, #sheaFaves li a {
font-size:.75em;
}
#tabs-1 li, #tabs-2 li {
letter-spacing:.1em;
font-size:.65em;
line-height:1.3em;
}
#featuredBand {
margin-top:260px;
clear:both;
overflow:hidden;
}
#featuredBand h2 {
margin-bottom:15px;
}
#featuredBand img {
float:left;
width:40%;
margin-right:10px;
}
#featuredBand h3 {
margin-bottom:5px;
}
#featuredBand p {
line-height:1.2em;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加这些规则:
但是,并非所有浏览器都完全支持
min-height
。如果您使用height
相反,如果容器中的内容高于页面,则会导致问题。要解决此问题,您可以创建一个 100% x 100% div 作为 body 的副本,为其提供一个作为透明叠加层的背景图像,并将其overflow
设置为auto,所以它的行为就像第二个 body 标签。
HTML:
CSS:
transparent.png 将是containerWidth x 1px,填充您想要作为容器背景的rgba 颜色。
Add these rules:
However,
min-height
isn't fully supported in all browsers. If you useheight
instead it will cause problems if the content in container is higher than the page. To work around this, you could create a 100% x 100% div as a duplicate of body, give it a background image that is your transparent overlay, and set itsoverflow
toauto
, so it behaves like a second body tag.HTML:
CSS:
transparent.png would be containerWidth x 1px filled with the rgba color you wanted as the container background.
视口高度适用于现代浏览器。
试试这个:
高度:100vh;
这是支持(ie11+):
https://caniuse.com/#search=vh
Viewport height works for modern browsers.
Try this:
height:100vh;
Here's the support(ie11+):
https://caniuse.com/#search=vh