- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title></title>
- <style type="text/CSS">
- *{
- margin: 0px;
- padding: 0px;
- }
- .wrap{
- width: 100%;
- }
- header{
- background: black;
- height: 200px;
- //本来header可以继承父级wrap的宽度,如果加了absolute 或者fixed就不能继承父级的宽度,因为脱离 //当前文本流
- /*position: fixed*/
- /*position: absolute;*/
- }
- </style>
- </head>
- <body>
- <div class="wrap">
- <header>
- </header>
- </div>
- </body>
- </html>
一般 block 属性会自动继承父级的宽度,但是如果用了 position :fixed (相对浏览器窗口定位)或者 position:absolute,就不能继承父级的宽度,因为此时脱离了当前文档流,就应该设置宽度
来源: http://www.2cto.com/kf/201701/593198.html