水平居中浮动框
我想显示一些浮动框(包含缩略图的div),缩略图的数量取决于当前页面的宽度。例如:
<div class="container">
<div class="box1" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box2" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box3" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box4" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
.......... ETC
</div>
问题是,对于给定的宽度,它在每行上显示 4 个框,但它们都是左对齐的,并且右侧有一些空白,我如何才能使每行水平居中?
像这样的东西: http://realworldstyle.com/thumbs_3.html 但盒子水平居中页...
提前致谢,
I want to display some floating boxes (divs containing thumbnails) and the number of thumbnails depends on the current page width. For example:
<div class="container">
<div class="box1" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box2" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box3" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
<div class="box4" style="float:left;width:120px;height:120px;margin-right:10px;">Thumbnail image here</div>
.......... ETC
</div>
the problem is that for a given width it shows for example 4 boxes on each row, but they are all left aligned and there is some white space to the right, how can i center horizontally for each row??
Something like this: http://realworldstyle.com/thumbs_3.html but with boxes centered horizontally on the page...
thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为使元素居中的唯一方法是不使用浮动,而是设置图像
display: inline
。这样,您就可以使用父容器的text-align
属性随意对齐它们。但这会给您带来垂直边距和设置高度的额外问题。但据我所知,只要
inline-block
没有得到广泛支持,它就是唯一可靠的跨浏览器方式。I think the only way to get the elements centered is to work not with float, but setting the images
display: inline
. That way, you can align them at will using the parent container'stext-align
property.But that will give you additional issues with vertical margins and setting height. But as far as I know, it's the only reliable cross-browser way as long as
inline-block
is not widely supported.抱歉,但无法直接使用 CSS + HTML 完成您想要的操作。 (看看@Pekka作为替代方案,尽管如果缩略图行没有填满整行,它们将在最后一行上自行居中)
您需要在父对象上有固定的宽度
.container
与margin-left: auto
和margin-right:auto
你不能这样做,因为它是一个流体宽度页面。以下是我的做法,尽管它肯定存在一些需要解决的错误:
window.resize
事件div.container
的宽度设置为该宽度加上右侧的一点边距。此div
始终具有margin-left
和margin-right
为auto
这将使缩略图最佳居中尽可能。根据您的视觉显示,您可能需要额外的包装 div 来提供
100% 宽度
背景。Sorry, but will not be able to do what you are wanting with straight CSS + HTML. (Take a look at @Pekka for an alternative, though if the row of thumbnails don't fill the whole row, they would be centered by themselves on the last row)
You would need to have a fixed width on the parent object
.container
withmargin-left: auto
andmargin-right:auto
which you cannot do since it is a fluid width page.Here is how I would go about doing it, though it is sure to have some bugs you will need to work around:
window.resize
eventdiv.container
to be that width plus the little bit of margin on the right. Thisdiv
would always have amargin-left
andmargin-right
ofauto
This will center the thumbnails as best as possible. Depending on your visual display, you may need an additional wrapping div to provide the
100% width
background.使用带有内联列表元素的无序列表:
HTML:
CSS:
只要 LI 元素内没有块级元素就可以。如果您有不止一行图像,也可以使用;)您也可以使用 div,但使用列表在语义上更好。
Use an unordered list with inline list elements:
HTML:
CSS:
That'll do as long as you don't have block-level elements inside the LI elements. Works also if you have more than one row of images ;) You can also use div's but using a list is semantically much more nicer.
.container 上的内联块和自动边距对于大多数浏览器来说应该可以解决问题,也许正文上的 text-align:center 也可以。
如果 IE6 和 IE7 不能很好地发挥作用,您可以随时使用 javascript / jquery 将整个内容封装在一个表格中,供它们使用。
请注意,我不敢为普通浏览器建议一个表格解决方案,尽管它显然可以完美地工作;-)
inline-block and auto margins on .container should do the trick for most browsers with perhaps text-align:center on body as well.
And if IE6 and IE7 don't play nice, you can always use javascript / jquery to wrap the whole thing in a table just for them.
Mind you, I wouldn't dare suggest a table solution for normal browsers, although it obviously works flawlessly ;-)
样式边距:0 自动;将使元素居中。但需要指定父容器的宽度。
Style margin: 0 auto; will center the element. But you need to specify the width of the parent container.