使用CSS将居中div居中 [英] Problem centering div with css
问题描述
我正在尝试在页面中居中放置div,但是我有一个非常特殊的布局,该布局不允许我使用常规边距将其居中:auto属性.我所拥有的基本上是这样的:
I'm trying to center a div in my page but I have a very particular layout that doesn't let me center it with regular margin: auto properties. What I have is, basically this:
HTML:
<div id="container">
<div id="sidebar">
some text
</div>
</div id="content">
some text
</div>
</div>
CSS:
#container{
margin: 0 auto;
width: 900px;
}
#sidebar{
float: left;
width: 300px;
}
#content{
float: left;
width: 600px;
}
这使我的容器居中没有问题,但是我需要相对于屏幕宽度将内容" div居中,这是我正在研究的站点,因此您可以更清楚地看到它: http://dirtymind.jvsoftware.com/和为更好地解释我的问题而制作的图像: http://dirtymind.jvsoftware.com/img/csserror.png
This centers my container with no problems but what I need is to center the "content" div relative to the width of the screen, here's the site that I'm working on so you can see it more clearly: http://dirtymind.jvsoftware.com/ and an image I made in an effort to better explain my problem: http://dirtymind.jvsoftware.com/img/csserror.png
有人知道如何做到这一点吗?谢谢大家.
Does anyone know how to accomplish this? Thanks all in advance.
推荐答案
通过边栏的宽度增加容器的宽度,并在内容块中添加相同量的右边界.
Increase the width of the container by the width of the sidebar, and add a right margin to the content block by the same amount.
这篇关于使用CSS将居中div居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!