水平居中浮动框

发布于 2024-08-13 17:50:37 字数 853 浏览 3 评论 0原文

我想显示一些浮动框(包含缩略图的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴梦一场 2024-08-20 17:50:37

我认为使元素居中的唯一方法是不使用浮动,而是设置图像 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's text-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.

扛起拖把扫天下 2024-08-20 17:50:37

抱歉,但无法直接使用 CSS + HTML 完成您想要的操作。 (看看@Pekka作为替代方案,尽管如果缩略图行没有填满整行,它们将在最后一行上自行居中)

您需要在父对象上有固定的宽度.containermargin-left: automargin-right:auto 你不能这样做,因为它是一个流体宽度页面。

以下是我的做法,尽管它肯定存在一些需要解决的错误:

  1. 将 javascript 事件绑定到 window.resize 事件
  2. 获取新文档宽度并查看如何操作许多缩略图适合一行
  3. div.container 的宽度设置为该宽度加上右侧的一点边距。此 div 始终具有 margin-leftmargin-rightauto

这将使缩略图最佳居中尽可能。根据您的视觉显示,您可能需要额外的包装 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 with margin-left: auto and margin-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:

  1. Bind a javascript event to the window.resize event
  2. Get the new document width and see how many thumbnails would fit on one row
  3. Set the width of the div.container to be that width plus the little bit of margin on the right. This div would always have a margin-left and margin-right of auto

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.

素食主义者 2024-08-20 17:50:37

使用带有内联列表元素的无序列表:

HTML:

<ul>
    <li><img src="image1.jpg" /></li>
    <li><img src="image2.jpg" /></li>
    <li><img src="image3.jpg" /></li>
    <li><img src="image4.jpg" /></li>
</ul>

CSS:

ul {
    width: 960px;
    text-align: center;
}

ul li {
    display: inline;
}

只要 LI 元素内没有块级元素就可以。如果您有不止一行图像,也可以使用;)您也可以使用 div,但使用列表在语义上更好。

Use an unordered list with inline list elements:

HTML:

<ul>
    <li><img src="image1.jpg" /></li>
    <li><img src="image2.jpg" /></li>
    <li><img src="image3.jpg" /></li>
    <li><img src="image4.jpg" /></li>
</ul>

CSS:

ul {
    width: 960px;
    text-align: center;
}

ul li {
    display: inline;
}

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.

难忘№最初的完美 2024-08-20 17:50:37

.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 ;-)

花之痕靓丽 2024-08-20 17:50:37

样式边距:0 自动;将使元素居中。但需要指定父容器的宽度。

<div style="width:100%; float:left; margin: 0 auto;">
   /* Your Child Elements */
</div>

Style margin: 0 auto; will center the element. But you need to specify the width of the parent container.

<div style="width:100%; float:left; margin: 0 auto;">
   /* Your Child Elements */
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文