将多个浮动 div 置于容器内居中
好的,这是我正在开发的网站的链接。 http://danberinger.com/preview.html
我目前在其自己的 div(# messagebox)和其自己的 div 中的图片(#picture)。这两个 div 都向左浮动。我将它们放在一个名为 #intro_container 的容器 div 中。我想将这个包含 div 居中,但遇到了麻烦。我尝试将边距设置为 0 和自动,但这不起作用。这一定是我尝试使用的不同级别的 div 数量存在某种问题......
任何帮助将不胜感激!
Alright, here is a link to the site I am working on. http://danberinger.com/preview.html
I currently have the message inside its own div(#messagebox) and the picture in its own div (#picture). Both of these divs are floated to the left. I put them inside a container div called #intro_container. I would like to center this containing div, but am having trouble with it. I have tried setting the margins to 0 and auto but that did not work. This must be some sort of issue with the amount of different levels of divs that I am trying to work with....
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
设置
#intro_container
容器的宽度,否则margin: 0 auto;
将不起作用。Set a width on your
#intro_container
container, otherwisemargin: 0 auto;
won't work.要使 0 auto 工作,您必须指定宽度。
您可以将所有内容放入容器 div 中,例如 width 900px margin 0 auto,或者您可以简单地将这些样式应用于介绍容器。
现在你的 CSS 正在将所有内容缩放到视口宽度,因此 float left 将图片发送到 intro_container 的宽度,而 intro_container 没有定义宽度。
另请注意,您可以将您的 ID 直接应用于图像!无需将其包装在 div 中。按语义设置元素样式是一种很好的做法。
For 0 auto to work, you must specify a width.
You can put everything within a container div, with something like width 900px margin 0 auto, or you can simply apply these styles to intro container.
Right now your css is scaling everything to the viewport width, so float left sends the pictures to the width of intro_container, which has no defined width.
Also note, you can apply your id directly to the image! no need to wrap that in a div. It is good practice to style elements semantically.
有时将事情集中在这样的中心是好的:
始终工作
sometimes it's good to center things like that:
always working