浮动一堆 div 居中,同时保留动态宽度和高度
我有一个大 div。它包含一堆缩略图。我希望 div 内的缩略图在 div 内居中,并且 div 在页面上居中。但我还希望 div 的宽度是动态的,这样您就可以看到每个分辨率上的所有缩略图,并居中。我怎样才能做到这一点?
到目前为止,这是我所拥有的:
<div id="container">
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
</div>
我尝试使用此 css 将其居中: 问题是我需要一个 float:left 的替代方案,它可以居中,这样它们就可以彼此浮动。我目前所拥有的只是将它们向左浮动,因此在错误的浏览器宽度上,它们没有居中。
#container{margin:auto;width:80%}
.thumb{padding:60px;float:left}
关于如何做类似 float:center 的事情有什么想法吗?
I've got a large div. It contains a bunch of thumbnails. I'd like the thumbnails within the div to be centered inside the div, and the div to be centered on the page. But I'd also like the width of the div to be dynamic so you can see all the thumbnails on every resolution, centered. How could I achieve this?
here's what I have so far:
<div id="container">
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
<div class="thumb"><img /></div>
</div>
I've tried centering it with this css:
The problem is I need an alternative to float:left that works to center so they all float around each other. What I currently have just floats them left so on the wrong browser width, they're not centered.
#container{margin:auto;width:80%}
.thumb{padding:60px;float:left}
Any ideas on how to do something like float:center?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您不能为所有子级
提供相同的ID;
;这是无效的 HTML。我会做这样的事情:
或者,您可以将所有
更改为
并删除
display:inline
CSS 指令。您还可以完全删除或
想法,并将
padding
直接应用于img< /代码> 标签。
编辑:看到另一个答案上发布的评论,看来我上面的答案仍然不能完全帮助你。这是您想要实现的行为吗? http://jsfiddle.net/9KqQN/1
Firstly, you cannot give the same ID to all the children
<div>
; That is invalid HTML.I'd do something like this:
Alternatively, you could just change all the
<div>
s to<span>
s and remove thedisplay:inline
CSS directive. You could also completely remove the<div>
or<span>
idea and just apply thepadding
directly to theimg
tag.EDIT: Seeing comments posted on another answer, it looks like my answer above still won't exactly help you. Is this the behavior you're trying to accomplish? http://jsfiddle.net/9KqQN/1
在拇指 div 上使用
display: inline-block
。这使您可以像对待任何其他文本一样对待它们,但保留其固有的“盒子”大小:Use
display: inline-block
on the thumb divs. This lets you treat them like any other piece of text, but retains their inherent "boxiness" for sizing: