我有一个居中固定宽度布局的页面。为此,我将所有内容包装在一个div中,添加一个固定的宽度和margin: auto。但是,我的标题应该有一个背景,延伸到整个页面(只有背景)。有没有办法做到这一点(不需要将单个固定宽度+边距的自动div拆分成许多许多div)?
发布于 2012-05-26 00:22:04
您可以在这些div上使用绝对定位的伪元素:
#main-page {
width: 50%;
margin: 0 auto;
}
div.full-width {
position: relative;
background: black;
}
div.full-width:before, div.full-width:after {
content: "";
position: absolute;
background: black; /* Match the background */
top: 0;
bottom: 0;
width: 9999px; /* some huge width */
}
div.full-width:before {
right: 100%;
}
div.full-width:after {
left: 100%;
}发布于 2012-05-26 00:28:30
不是很优雅,但这里有一个解决方案:http://dabblet.com/gist/2789029 -它使用巨大的负边距和相等的填充。
这样做是可行的:
body {
overflow-x: hidden;
}
.central {
width: 400px;
margin: 0 auto;
outline: solid 1px red;
}
h1 {
margin: 5px -2000px;
padding: 5px 2000px;
outline: solid 1px blue;
background: cyan;
}发布于 2012-05-26 01:01:10
类似的,但仍然是一个简单且可用的解决方案的示例:http://jsfiddle.net/grE5A/41/
#main_container {
width:400px;
min-height:360px;
margin:10px auto;
background:#000;
overflow:visible;
}
#heading_div_bg {
background-color:green;
height:50px;
width:100%;
position:absolute;
margin:0px auto;
left:0px;
top:50px;
}
#heading_div {
background-color:darkgreen; /*set to same as header_div*/
text-align:center;
height:50px;
width:400px; /*same as container*/
position:relative;
margin: 0 auto;
}https://stackoverflow.com/questions/10757993
复制相似问题