CSS 块元素在一行上

发布于 2024-07-08 14:27:47 字数 102 浏览 5 评论 0原文

处理需要在一行上的一系列块元素的最常见方法是什么(例如,如果 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 技术交流群。

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

发布评论

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

评论(5

念三年u 2024-07-15 14:27:48

我认为我不会显式定位元素,而是指示浏览器使用 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.

笑梦风尘 2024-07-15 14:27:48

对我来说,在这种情况下浮动的唯一缺点是你要么需要向左对齐,要么向右对齐——居中不是一个选择。 然而,您已经提到您使用宽度的绝对值,因此您可以将所有浮动元素嵌套在 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.

菊凝晚露 2024-07-15 14:27:47

好吧,如果您不太关心旧版浏览器(我正在看您,IE6),这里最好的方法是使用

display:inline-block;

基本上,它创建一个盒模型元素,而不在其之前或之后清除,因此它保留在线。 每个现代浏览器都能很好地解释它。

Well, if you're not too concerned with older browsers (I'm looking at you, IE6) the best way here is to go with

display:inline-block;

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.

合约呢 2024-07-15 14:27:47

浮动将是我的选择,但这实际上取决于您想要实现的目标。 如果您可以提供一个更具体的示例,我将能够给您一个明确的理由,说明我认为您应该使用什么以及为什么。 然而,这里是我遇到的一组简短的优点和缺点(我假设通过定位你意味着绝对定位):

定位优点:

  • 相对于标记为相对位置的下一个祖先的非常精确的定位 - 允许很大的灵活性
  • 允许元素在视觉上的顺序与 DOM 中的顺序不同

。 定位缺点:

  • 由于定位的元素不再位于文档流中以及所需的精度级别,因此更难与其他元素对齐。
  • 其他元素会忽略绝对定位元素,这意味着您可能存在潜在的重叠,除非您考虑了定位元素的最小和最大尺寸,
  • 如果您对列使用绝对定位,则很难实现页脚。

浮动优点:

  • 非常容易构建简单和高级的布局,
  • 没有页脚问题,
  • 不用担心精度,浏览器会为您处理
  • 父容器的延伸

浮动缺点:

  • 对于那些对浮动布局经验不足的人来说,有很多陷阱,可能会导致提出许多问题关于SO :)

至于塞巴斯蒂安提到的clear:both元素,有一个简单的方法可以解决这个问题。 假设您有一个容器 div 和 2 个浮动 div 。

Html:

<div id="con">
    <div class="float"></div>
    <div class="float"></div>
</div>

CSS:

#con { background:#f0f; }
.float { float:left; width:100px; height:100px; background:#0ff; }

如果您要运行此代码,您会注意到容器 div(洋红色的)只有一个像素高,而浮动 div 是正确的 - 这就是 Sebastian 提到的问题。 现在你可以采纳他的建议并添加一个 br 或浮动容器,这不太语义 - 所以这里有一个稍微更优雅的解决方案。 只需添加溢出:隐藏; 像这样的容器 div:

#con { background:#f0f; overflow:hidden; }

现在你的容器应该正确地包裹浮动的 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:

  • very precise positioning in relation to the next ancestor marked as position relative - allows for great flexibility
  • allows for elements to be in a different order visually than they are in the DOM

Positioning cons:

  • Harder to line up with other elements since the positioned element is no longer in the document flow and also because of the level of precision required.
  • Other elements ignore the absolutely positioned element which means you could have a potential overlap, unless you account for the minimum and maximum size of the positioned element
  • harder to implement a footer if you are using absolute positioning for your columns.

Float pros:

  • really easy to construct simple and advanced layouts
  • no footer problem
  • no worrying about precision, browser takes care of that for you
  • parent container stretches

Float cons:

  • Lots of pitfalls for those less experienced with float layouts that may lead to many questions being asked on SO :)

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:

<div id="con">
    <div class="float"></div>
    <div class="float"></div>
</div>

CSS:

#con { background:#f0f; }
.float { float:left; width:100px; height:100px; background:#0ff; }

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:

#con { background:#f0f; overflow:hidden; }

Now your container should wrap the floated divs properly.

累赘 2024-07-15 14:27:47

父容器不会随它们一起拉伸,除非它也被分配了一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文