gpt4 book ai didi

css - float 和水平边距

转载 作者:行者123 更新时间:2023-11-28 10:20:54 25 4
gpt4 key购买 nike

我有以下 CSS:

label{
float:left;
margin-left:24px;
}

button{
margin-left:24px;
}

对于这个 HTML:

<label>
<input>
</label>
<button>

我希望在距离标签 24 像素的地方找到按钮,但是直到我将按钮也向左浮动时才发生这种情况。我可以引用 CSS 规范的哪一部分来理解为什么会发生这种情况?

(拜托,对不起我的英语。)

最佳答案

只需阅读 W3C documentation for float :

Here are the precise rules that govern the behavior of floats:

  1. The left outer edge of a left-floating box may not be to the left of the left edge of its containing block. An analogous rule holds for right-floating elements.
  2. If the current box is left-floating, and there are any left-floating boxes generated by elements earlier in the source document, then for each such earlier box, either the left outer edge of the current box must be to the right of the right outer edge of the earlier box, or its top must be lower than the bottom of the earlier box. Analogous rules hold for right-floating boxes.
  3. The right outer edge of a left-floating box may not be to the right of the left outer edge of any right-floating box that is next to it. Analogous rules hold for right-floating elements.
  4. A floating box's outer top may not be higher than the top of its containing block. When the float occurs between two collapsing margins, the float is positioned as if it had an otherwise empty anonymous block parent taking part in the flow. The position of such a parent is defined by the rules in the section on margin collapsing.
  5. The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.
  6. The outer top of an element's floating box may not be higher than the top of any line-box containing a box generated by an element earlier in the source document.
  7. A left-floating box that has another left-floating box to its left may not have its right outer edge to the right of its containing block's right edge. (Loosely: a left float may not stick out at the right edge, unless it is already as far to the left as possible.) An analogous rule holds for right-floating elements.
  8. A floating box must be placed as high as possible.
  9. A left-floating box must be put as far to the left as possible, a right-floating box as far to the right as possible. A higher position is preferred over one that is further to the left/right.

因为行内元素不能有margin属性(property)。 <label>是一个内联元素,通过 float 它,你可以让它看起来像一个 inline-block。 , 允许您添加 margin

这很奇怪。尝试使用 display: inline-block;而不是 float: left;看看会发生什么。

关于css - float 和水平边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com