CSS Layout - The display Property
display property is the most important CSS property for controlling layout.
display property affects the most basic presentation of an element, effectively classing the element as a certain type of element.
The display Property
display property specifies if/how an element is displayed.
Every HTML element has a default display value depending on what type
of element it is. The default display value for most elements is
Click to show panel
This panel contains a <div> element, which is hidden by default (
A block-level element always starts on a new line and takes up the full width available .
The display property determines how that rectangular box behaves
Examples of block-level elements:
- <h1> - <h6>
Setting the display property of an element only changes how the element is displayed, not what kind of element it is. So, an inline element with display:block is not allowed to have other block elements inside it.
An inline element only takes up as much width as necessary, and does not force line breaks. .
This isa paragraph.
Examples of inline elements:
display:none hides an element, so it does not take up any space. The element will be hidden, and the page will be displayed as if the element is not there.
Override The Default Display Value
Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow the web standards.
A common example is making inline
<li> elements for horizontal menus:
The following example displays <span> elements as block elements:
Hide an Element - display:none or visibility:hidden?
Hiding an element can be done by setting the
display property to
The element will be hidden, and the page will be displayed as if the element is not
visibility:hidden; also hides an element.
However, the element will still take up the same space as before. The element will be hidden, but still affect the layout: