Where in the CSS spec does it define this behavior?
As stated in these two articles...
Smashing Magazine
When you float an element it becomes a block box
CSS Tricks
An element that is floated is automatically
display: block;
Example: 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>
Answer. When using the float property on display: inline; elements, the display value will be computed to block instead ( display: block; ). This means that display: inline; and display: block; elements will behave the same way when a float property is added to the element.
You can change the visual presentation of an element using the CSS display property. For example, by changing the value of display from "inline" to "block" , you can tell the browser to render the inline element in a block box rather than an inline box, and vice versa.
An inline-block elements will respect a width . People used to¹ build column layout systems with inline-block , because it basically can do what floats could do here, except without the need to worry about clearing the float², allowing people to take advantage of wrapping which happens a bit more elegantly than float.
This behavior is defined in the point 3 of this CSS2.1 section:
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│ └──────────────────────────────────────────────────────────┴──────────────────┘
In Display Level 3, this process is called blockification:
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]
It's defined in the Visual Formatting Model section 9.5.1
This property specifies whether a box should float to the left, right, or not at all. It may be set for any element, but only applies to elements that generate boxes that are not absolutely positioned. The values of this property have the following meanings:
left
The element generates a block box that is floated to the left. Content flows on the right side of the box, starting at the top (subject to the 'clear' property).
right
Similar to 'left', except the box is floated to the right, and content flows on the left side of the box, starting at the top.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With