The CSS display Property
The display property is an important CSS property for controlling layout. It specifies whether an HTML element is treated as a block or an inline element.
Every HTML element has a default display value, depending on what type of element it is. The default display value for most elements is block or inline.
The display property is used to change the default display behavior of HTML elements.
Block-level Elements
A block-level element ALWAYS starts on a new line and takes up the full width available (stretches out to the left and right as far as it can).
![]()
Examples of block-level elements:
- <div>
- <h1> – <h6>
- <p>
- <form>
- <header>
- <footer>
- <section>
Inline Elements
An inline element DOES NOT start on a new line and only takes up as much width as necessary.
![]()
Examples of inline elements:
- <span>
- <a>
- <img>
Example of More Display Values
The following example demonstrates some more display values:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<!DOCTYPE html> <html> <head> <style> p { background-color: blue; padding: 10px; } span { background-color: red; color: white; padding: 10px; } p.ex1 {display: none;} p.ex2 {display: inline;} p.ex3 {display: block;} p.ex4 {display: inline-block;} p.ex5 {display: flex;} p.ex6 {display: grid;} </style> </head> <body> <h1>The display Property</h1> <h2>display: none:</h2> <p class="ex1"> <span>Text 1</span> <span>Text 2</span> <span>Text 3</span> </p> <h2>display: inline:</h2> <p class="ex2"> <span>Text 1</span> <span>Text 2</span> <span>Text 3</span> </p> <h2>display: block:</h2> <p class="ex3"> <span>Text 1</span> <span>Text 2</span> <span>Text 3</span> </p> <h2>display: inline-block:</h2> <p class="ex4"> <span>Text 1</span> <span>Text 2</span> <span>Text 3</span> </p> </body> </html> |

CSS Hide Elements
CSS display: none;
When using display: none; the element is completely hidden from the document flow and does not take up any space.
It is commonly used with JavaScript to hide or show elements without deleting and recreating them.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <style> h1.hidden { display: none; } </style> </head> <body> <h1>This is a visible heading</h1> <h1 class="hidden">This is a hidden heading</h1> <p>Notice that the h1 element with display: none; does not take up any space.</p> </body> </html> |

You can also use visibility:hidden; to hide an element.
However, with this property, the element will be hidden, but it will still take up the same space as if it was visible:
Example
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <style> h1.hidden { visibility: hidden; } </style> </head> <body> <h1>This is a visible heading</h1> <h1 class="hidden">This is a hidden heading</h1> <p>Notice that the hidden heading still takes up space.</p> </body> </html> |

CSS Display/Visibility Properties
