我有一个宽度为900px的容器,然后在其中有一个宽度为100%的标头,但它只占用了容器的100%,如何使它占据整个页面并忽略容器而不将其从容器中取出html标签?

#container {
width: 900px;
margin: auto;
padding: auto;
position: relative;
}

#header {
background-image: url(pat.png);
background-repeat: repeat;
padding: 0px;
margin: 0px;
height: 150px;
width: 100%;
}

最佳答案

使用绝对定位。然后将根据已定义position: relative;的最近父级(默认情况下为<body>元素)调整header元素的大小和位置。像这样:

#header {
    position: absolute;
    left: 0;
    right: 0; /* it will span from the left to the right edges */
    height: 100px; /* it helps to set a fixed-sized height too, but this isn't required */
}

09-04 05:50
查看更多