Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css display property when a float is applied

Tags:

css

css-float

When an element is floated, how do different display properties affect the layout? Or, what are the differences, if any, between these classes:

div.foo {
    display: block;
    float: left;
}

div.foo2 {
    display: inline;
    float: left;
}

div.foo3 {
    display: inline-block;
    float: left;
}

EDIT:

If there are no differences according to the spec, then do certain antiquated versions of browsers (ahem, IE) render them differently?

like image 829
Jonathan Swinney Avatar asked Jan 27 '26 18:01

Jonathan Swinney


1 Answers

If I read the spec correctly, and practice confirms this, setting float: left or right overrides the display property anyway and forces display: block on the element (although with peculiarities, see below), so there will no difference between your three examples:

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.

none The box is not floated.

Differently from normal display: block though, setting float dictates its own behaviour in regards to width that override the display property: if no width was explicitly specified, the floated element will take up as much width as it needs, as opposed to standard block element behaviour of taking up 100% width automatically.

like image 90
Pekka Avatar answered Jan 30 '26 12:01

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!