好吧,我有一个网站,整个网站都包装在一个容器中。该容器以 margin:auto 居中。我想将一段内容 float 到这个居中容器的右侧,并让它粘在它的一侧,无论用户是否调整浏览器窗口的大小等。我想知道是否有一种真正简单的方法执行此操作,而不是添加另一个巨大的 div,为其提供宽度并将居中部分 float 到左侧,将内容 block float 到右侧。谢谢!
最佳答案
搭载@NickAllen,您想要使用绝对定位,以便侧边栏的宽度不包含在主容器的居中中。
#container {
position: relative;
width: 500px;
border: 1px solid #f00;
margin: 0px auto;
}
#sidebar {
position: absolute;
width: 200px;
right: -200px;
border: 1px solid #0f0;
}
<div id="container">
<div id="sidebar">
[ sidebar content ]<br>
[ sidebar content ]<br>
</div>
[content]<br>
[content]<br>
[content]<br>
</div>
关于css - 在主居中的 div 旁边 float 另一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/840542/