CSS 块元素在一行上
处理需要在一行上的一系列块元素的最常见方法是什么(例如,如果 javascript 需要能够修改它们的宽度)? 与使用定位来放置它们相比,对每个它们应用 float:left 有何优缺点?
What's the most common way to deal with a series of block elements that need to be on a line (if javascript needs to be able to modify their widths, for example)? What are the pros and cons to applying float:left to each of them vs. using positioning to place them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为我不会显式定位元素,而是指示浏览器使用 display:inline 对元素使用内联布局,并让浏览器处理定位。
关于浮动与定位,我认为使用定位排列它们的唯一方法是使用绝对定位,这意味着您需要处理(浏览器视图端口)的重新大小以将元素保持在适当的位置。
我认为通过使用 float 属性,浏览器可以为您处理重新调整大小的问题,并将元素重新呈现在正确的位置。
I think i wouldn't explicitly position the elements but rather instruct the browser to use an inline layout for the elements using display:inline and let the browser handle the positioning.
regarding float vs positioning i think the only way to line them up using positioning is by using absolute positioning, and that means you need to handle re-sizes(of the browser view port) in order to keep the elements in place.
I think that by using the float property the browser handles the re-sizing issues for you and re-renders the element in the correct place.
对我来说,在这种情况下浮动的唯一缺点是你要么需要向左对齐,要么向右对齐——居中不是一个选择。 然而,您已经提到您使用宽度的绝对值,因此您可以将所有浮动元素嵌套在 DIV 元素中,并向父 DIV 添加 margin-right 或 margin-left 来模拟中心对齐。
Only disadvantage to float in situations like this for me has been that you'll either need to left justify them or right justify them -- centering is not an option. However you've mentioned you're using absolute values for widths, so you could just nest all the floated elements in a DIV element and add either margin-right or margin-left to the parent DIV to simulate center alignment.
好吧,如果您不太关心旧版浏览器(我正在看您,IE6),这里最好的方法是使用
基本上,它创建一个盒模型元素,而不在其之前或之后清除,因此它保留在线。 每个现代浏览器都能很好地解释它。
Well, if you're not too concerned with older browsers (I'm looking at you, IE6) the best way here is to go with
Basically, it creates a box-model element without clearing before or after it, so it remains in the line. Every modern browser interprets it well.
浮动将是我的选择,但这实际上取决于您想要实现的目标。 如果您可以提供一个更具体的示例,我将能够给您一个明确的理由,说明我认为您应该使用什么以及为什么。 然而,这里是我遇到的一组简短的优点和缺点(我假设通过定位你意味着绝对定位):
定位优点:
。 定位缺点:
浮动优点:
浮动缺点:
至于塞巴斯蒂安提到的clear:both元素,有一个简单的方法可以解决这个问题。 假设您有一个容器 div 和 2 个浮动 div 。
Html:
CSS:
如果您要运行此代码,您会注意到容器 div(洋红色的)只有一个像素高,而浮动 div 是正确的 - 这就是 Sebastian 提到的问题。 现在你可以采纳他的建议并添加一个 br 或浮动容器,这不太语义 - 所以这里有一个稍微更优雅的解决方案。 只需添加溢出:隐藏; 像这样的容器 div:
现在你的容器应该正确地包裹浮动的 div。
Floating would be my choice, but it really depends on what you wish to achieve. If you can provide a more specific example I would be able to give you a clear reason as to what and why I think you should use. However here is a brief set of pros and cons that I have come accross (I'm assuming that by positioning you mean absolute positioning):
Positioning pros:
Positioning cons:
Float pros:
Float cons:
As to the clear:both element that Sebastian mentioned, There's a simple way around that. Lets say you have a container div and 2 floated divs inside.
Html:
CSS:
if you were to run this code you would notice that the container div (the magenta coloured one) is only a single pixel high whereas the floated divs were correct - which is the problem Sebastian mentioned. Now you could take his advice and add a br or float the container which would not be very semantic - so here is a slightly more elegant solution. Just add overflow:hidden; to the container div like so:
Now your container should wrap the floated divs properly.
父容器不会随它们一起拉伸,除非它也被分配了一个 float 标签或者有一个带有clear:both; 的 br 。 在底部。
我会选择 float:left 而不是定位。 当一个对象拉伸时,浏览器会执行所有对齐操作。 所以你需要关心的事情就更少了。
The parent container does not stretch with them unless it is also assigned a float tag or there is a br with clear:both; at the bottom.
I would go with the float:left instead of the positioning. The browser does all the aligning when one object stretches. So there is less for you to care about.