简体   繁体   English

中心浮动div项目

[英]Center floated div items

I have: 我有:

 #content { width: 100%; text-align: center; } .item { float: left; } 
 <div id="content"> <div class="item"><img /><br />wordsn</div> <div class="item"><img /><br />stuff</div> <div class="item"><img /><br />asdasdasdn</div> <div class="item"><img /><br />Dhdfrhwon</div> <div class="item"><img /><br />sewfafdion</div> </div> 

I want to center these images items in the div, and have them float next to each other, and have it wrap nicely. 我想将这些图像项放在div的中心,并使它们彼此相邻漂浮,并很好地包裹起来。

I have tried everything and it works in IE and breaks in Firefox so I hack some more crap and then it breaks in IE. 我已经尝试了所有方法,并且它在IE中都可以工作,并且在Firefox中可以中断,所以我砍了一些废话,然后在IE中中断了。

.item { width: 400px; margin: auto 0; }

您需要指定宽度,以便可以计算适当的边距。

Get rid of float and start using display: inline for the item divs. 摆脱float并开始使用display: inline作为div item

Then you can give content a text-align: center - should work. 然后,您可以给content一个text-align: center应该起作用。

By the way, semantically, a structure like this might work better - depending on what those divs represent, of course. 顺便说一句,从语义上讲,这样的结构可能会更好地工作-当然,取决于这些div表示的是什么。

<ul id="content">
    <li><img />wordsn</li>
    <li><img />stuff</li>
    <li><img />asdasdasdn</li>
    <li><img />Dhdfrhwon</li>
    <li><img />sewfafdion</li>
</ul>

Okay this is what I got to work across all browsers: 好的,这是我必须在所有浏览器上使用的功能:

#content {
text-align:center;
}

.item {
     display: -moz-inline-box;
    display:inline-block;
}

* html .item { display:inline; }  /* for IE 6? */
* + html .item { display:inline; }  /* for IE 7? */

edit: width not required 编辑:不需要宽度

Is this the kind of thing you are trying to achieve? 这是您要实现的目标吗? (you will need to resize the page to see the wrap effect) (您需要调整页面大小以查看自动换行效果)

djgdesign.co.uk djgdesign.co.uk

#content .item{width:200px;margin:0 auto}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM