CSS Display Visibility: The display property is the most important CSS property for controlling layout.
In this tutorial, you will learn-
In this article, you will learn-
The display Property
The display property specifies if/how an element is shown.
Every HTML element has a default show value depending upon what type of element it is. The default show value for most elements is block or inline.
Block-level Elements
A block-level element consistently begins another line and takes up the full width accessible (stretches out to the left and right as far as it can)
The <div> element is a block-level element.
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.
This is an inline <span> element inside a paragraph.
Examples of inline elements:
- <span>
- <a>
- <img>
Display: none;
show: none; is normally used with JavaScript to hide and show elements without erasing and recreating them. Take a look at our last example on this page if you want to know how this can be achieved.
The <script>
element uses display: none;
as default.
Override The Default Display Value
As referenced, every element has a default show value. Be that as it may, you can override this.
Changing an inline element to a block element, or the other way around, can be useful for making the page look a particular way, and still follow the web standards.
A typical example is making inline <li> elements for horizontal menus:
Example
<!DOCTYPE html> <html> <head> <style> li { display: inline; } </style> </head> <body> <p>Display a list of links as a horizontal menu:</p> <ul> <li><a href="/html" target="_blank">HTML</a></li> <li><a href="/css-tutorial" target="_blank">CSS</a></li> <li><a href="/javascript" target="_blank">JavaScript</a></li> </ul> </body> </html>
Note: Setting the display property of an element just changes how the element is displayed, NOT what sort of element it is. Along these lines, an inline element with display: block; isn’t allowed to have other block elements inside it.
The accompanying example design <span> elements as block elements:
Example
<!DOCTYPE html> <html> <head> <style> span { display: block; } </style> </head> <body> <span>A display property with a value of "block" results in</span> <span>a line break between the two elements.</span> </body> </html>
The following example displays <a> elements as block elements:
<!DOCTYPE html> <html> <head> <style> a { display: block; } </style> </head> <body> <p>Display links as block elements:</p> <a href="/html" target="_blank">HTML</a> <a href="/css-tutorial" target="_blank">CSS</a> <a href="/javascript" target="_blank">JavaScript</a> </body> </html>
Hiding an element can be done by setting the display property to none. The element will be hidden, and the page will be shown as if the element isn’t there:
Example
<!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>
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:
<!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
Property | Description |
display | Specifies how an element should be displayed |
visibility | Specifies whether or not an element should be visible |
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your projects with us.