为什么内联元素在浮动时的行为像块级元素? [英] Why do inline elements behave like block level elements when floated?
问题描述
在CSS规范中哪里定义了这种行为?
正如这两篇文章所述...
当你浮动一个元素时,它变成一个块状框 浮动的元素会自动 示例: https://jsfiddle.net/kennethcss/y6cmgubt/ 影响框生成和布局的三个属性 - 在显示级别3中,此过程称为阻止: 某些布局效果需要 blockification 或 inilification ,其中设置框的外部显示类型,如果它不 这样的一些示例包括: Where in the CSS spec does it define this behavior? As stated in these two articles... When you float an element it becomes a block box An element that is floated is automatically
Example: https://jsfiddle.net/kennethcss/y6cmgubt/
This behavior is defined in the point 3 of this CSS2.1 section: The three properties that affect box generation and layout —
In Display Level 3, this process is called blockification: Some layout effects require blockification or inlinification of the box type, which sets the box’s outer display type, if it is not Some examples of this include:
这篇关于为什么内联元素在浮动时的行为像块级元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
display:block; code>
a {/ * for looks * / background-color:#e1e1e1; line-height:30px; text-align:center; / *注释float:left出来测试。一旦浮点被删除,*高度或宽度都不会对锚点产生任何影响,因为它的默认*显示是内联的。 * / height:30px; float:left; width:100px;}
< nav& < a> Nav Item 1< / a> < a> Nav Item 2< / a> < a> Nav Item 3< / a>< / nav>
这个行为定义在CSS2.1部分的第3点: >
9.7
display
, position
和 float
显示
, position
和 float
- 交互如下:
显示
的值为 none
,则 position
和 float
不适用。在这种情况下,元素不生成框。
position
的值为 absolute
或固定
,该框是绝对定位的,计算的值 float
是 none
,并根据下表设置显示。框的位置将由 top确定
, 右
, bottom
和 <
float
具有除 none
之外的值,则将浮动框,并根据下表设置 display
/ li>
display
根据下表设置,除了它在CSS 2.1中未定义 list-item
的指定值是否为块
或列表项的计算值
。
display
属性值将按照指定应用。
┏━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│inline-table│表│
├────────────── ────────────────────────────────────────────────────────────────────────────────── ────┤
│inline,table-row-group,table-column,table-column-group│block│
│table-header-group,table-footer-group,table-row ││
│表格单元格,表格,inline-block││
├─────────────── ──────────────────────────────────────────────────────────────────── ──┤
│其他│与指定相同│
└────────────────────── ─────────────────────────
2.7。自动框类型转换
none
或 contents
到 block
或 inline
(分别)。
display: block;
a {
/* for looks */
background-color: #e1e1e1;
line-height: 30px;
text-align: center;
/* Comment "float: left" out to test. Once the float is removed, neither
* the height or width have any effect on the anchor because its default
* display is inline.
*/
height: 30px;
float: left;
width: 100px;
}
<nav>
<a>Nav Item 1</a>
<a>Nav Item 2</a>
<a>Nav Item 3</a>
</nav>
9.7 Relationships between
display
, position
, and float
display
, position
, and float
— interact as follows:
display
has the value none
, then position
and float
do not apply. In this case, the element generates no box.position
has the value absolute
or fixed
, the box is absolutely positioned, the computed value of float
is none
, and display is set according to the table below. The position of the box will be determined by the top
, right
, bottom
and left
properties and the box's containing block.float
has a value other than none
, the box is floated and display
is set according to the table below.display
is set according to the table below, except that it is undefined in CSS 2.1 whether a specified value of list-item
becomes a computed value of block
or list-item
.display
property values apply as specified.┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┓
┃ #Specified value# ┃ #Computed value# ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━┩
│ inline-table │ table │
├──────────────────────────────────────────────────────────┼──────────────────┤
│ inline, table-row-group, table-column, table-column-group│ block │
│ table-header-group, table-footer-group, table-row │ │
│ table-cell, table-caption, inline-block │ │
├──────────────────────────────────────────────────────────┼──────────────────┤
│ others │ same as specified│
└──────────────────────────────────────────────────────────┴──────────────────┘
2.7. Automatic Box Type Transformations
none
or contents
, to block
or inline
(respectively).