【发布时间】:2017-03-31 00:02:41
【问题描述】:
HTML:
<div id='id1'>
<div id='id2'>
</div>
</div>
CSS:
#id1 {
width: 300px;
height: 200px;
margin: 0 auto;
background: red;
position: relative;
}
#id2 {
position: absolute; left: 0; right: 0; margin-left: auto; margin-right: auto;
width: 100%; top: 20px; background: green; height: 30px;
}
我希望 #id2 占据全屏宽度。我可以指定左:-Npx 和右:-Npx,但这仅适用于某些屏幕尺寸。
id2不能超出屏幕,所以不能指定left:-9999px;
【问题讨论】:
-
您必须删除
#id1上的position: relative,因为该属性的作用与您尝试的完全相反。见force “position: absolute” to be relative to document not parent container。