CSS The display Property
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.
This is an inline <span> element inside a paragraph.
Examples of inline elements:
- <span>
- <a>
- <img>
Common display Values
The CSS display
property has many values.
The following table lists the most commonly used:
Value | Description |
---|---|
inline | Displays an element as an inline element |
block | Displays an element as a block element |
contents | Makes the container disappear, making its child elements children of the element the next level up in the DOM |
flex | Displays an element as a block-level flex container |
grid | Displays an element as a block-level grid container |
inline-block | Displays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height, width, padding, and margin values |
none | The element is completely hidden from the document flow (does not take up any space). |
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.
Click to show hidden panel
This panel contains a <div> element, which is hidden by default, with display: none.
We use JavaScript to show it (change it to display: block).
Example
How to use CSS and JavaScript to show a hidden element on a click event:
<style>
#panel {
display:
none;
}
</style>
<script>
function myFunction() {
document.getElementById("panel").style.display = "block";
}
</script>
Try it Yourself »
Example
How to use CSS and JavaScript to toggle between show and hide on a click event:
<style>
#panel {
display:
none;
}
</style>
<script>
function myFunction() {
var x = document.getElementById("panel");
if (x.style.display ===
"none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>
Try it Yourself »
Override the Default Display Value
The display
property is used to change the default display behavior of HTML elements.
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 to change <li>
elements
to inline, to create a horizontal menu:
Note: 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.
The following example displays <span> elements as block elements:
The following example displays <a> elements as block elements:
Example of More Display Values
The following example demonstrates some more display values:
Example
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;}
Try it Yourself »
Hide an Element - Use display:none or visibility:hidden?
display:none

visibility:hidden

Reset

Hiding an element can be done by setting the
display
property to
none
.
The element will be hidden, and the page will be displayed as if the element is not
there:
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:
CSS Display/Visibility Properties
Property | Description |
---|---|
display | Specifies how an element should be displayed |
visibility | Specifies whether or not an element should be visible |