它在 CSS 规范的哪个位置定义了这种行为?
如这两篇文章所述...
When you float an element it becomes a block box
An element that is floated is automatically
display: block;
示例: https://jsfiddle.net/kennethcss/y6cmgubt/
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>
最佳答案
此行为在本 CSS2.1 部分的第 3 点中定义:
9.7 Relationships between
display
,position
, andfloat
The three properties that affect box generation and layout —
display
,position
, andfloat
— interact as follows:
- If
display
has the valuenone
, thenposition
andfloat
do not apply. In this case, the element generates no box.- Otherwise, if
position
has the valueabsolute
orfixed
, the box is absolutely positioned, the computed value offloat
isnone
, and display is set according to the table below. The position of the box will be determined by thetop
,right
,bottom
andleft
properties and the box's containing block.- Otherwise, if
float
has a value other thannone
, the box is floated anddisplay
is set according to the table below.- Otherwise, if the element is the root element,
display
is set according to the table below, except that it is undefined in CSS 2.1 whether a specified value oflist-item
becomes a computed value ofblock
orlist-item
.- Otherwise, the remaining
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│ └──────────────────────────────────────────────────────────┴──────────────────┘
在显示级别 3 中,此过程称为 block 化:
2.7. Automatic Box Type Transformations
Some layout effects require blockification or inlinification of the box type, which sets the box’s outer display type, if it is not
none
orcontents
, toblock
orinline
(respectively).Some examples of this include:
- Absolute positioning or floating an element blockifies the box’s display type. [CSS2]
关于css - 为什么内联元素在 float 时表现得像 block 级元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38464859/