Open In App

CSS Display Property

Last Updated : 04 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The CSS display property specifies an element’s display behaviour (the type of rendering box). It defines how an element is rendered in the layout, determining its positioning and interaction within the document’s flow and structure.

Syntax 

display: value;

Try CSS Display Property

Display Property Values

ValueDescription
inlineUsed to display an element as an inline element.
blockUsed to display an element as a block element
contentsUsed to disappear the container.
flexUsed to display an element as a block-level flex container.
gridDisplay an element as a block-level grid container.
inline-blockDisplay an element as an inline-level block container.
inline-flexDisplay an element as an inline-level flex container.
inline-gridDisplay an element as an inline-level grid container.
inline-tableIt is used to display an inline-level table
list-itemIt is used to display all the elements in <li> element.
run-inIt is used to display an element inline or block level, depending on the context.
tableIt is used to set the behavior as <table> for all elements.
table-captionIt is used to set the behavior as <caption> for all elements.
table-column-groupSet the behavior as <column> for all elements.
table-header-groupSet the behavior as <header> for all elements.
table-footer-groupSet the behavior as <footer> for all elements.
table-row-groupIt is used to set the behavior as <row> for all elements.
table-cellIt is used to set the behavior as <td> for all elements.
table-columnIt is used to set the behavior as <col> for all elements.
table-rowTo set the behavior as <tr> for all elements.
noneUsed to remove the element.
initialUsed to set the default value.
inheritUsed to inherit property from its parents’ elements.

Example : This example uses 3 divs to demonstrate the CSS display property.


    <style>
        #geeks1 {
            height: 100px;
            width: 200px;
            background: teal;
            display: block;
        }

        #geeks2 {
            height: 100px;
            width: 200px;
            background: cyan;
            display: block;
        }

        #geeks3 {
            height: 100px;
            width: 200px;
            background: green;
            display: block;
        }

        .gfg {
            margin-left: 20px;
            font-size: 42px;
            font-weight: bold;
            color: #009900;
        }

        .geeks {
            font-size: 25px;
            margin-left: 30px;
        }

        .main {
            margin: 50px;
            text-align: center;
        }
    </style>

Understanding the Display Property

The display property defines how an HTML element should be displayed. It controls the box type generated by an element, affecting its positioning and behavior within the document flow. Let’s dive into the key values:

1. Using Display Block

This is the default property for <div> elements. It places them vertically, one after another. You can adjust the height and width of a block-level element.

Example: Use the given CSS in above example.

#geeks1 {
background: teal;
display: block;
}
#geeks2 {
background: cyan;
display: block;
}
#geeks3 {
background: green;
display: block;
}

Output: 

display block property

2. Using Inline Display

Use this property to display an element inline. It doesn’t start a new line and respects the content flow.

Example: Use the given CSS in above example.

#geeks1 {
background: teal;
display: inline;
}
#geeks2 {
background: cyan;
display: inline;
}
#geeks3 {
background: green;
display: inline;
}

Output: 

display inline property example output

3. Using Display Inline-block

Combining characteristics of both block and inline, this value allows elements to flow inline while still having block-level properties. It’s useful for creating responsive layouts.

Example: Use the given CSS in above example.

#geeks1 
{
background: teal;
display: inline-block;
}
#geeks2 {
background: cyan;
display: inline-block;
}
#geeks3 {
background: green;
display: inline-block;
}

Output: 

display inline block example output

4. Using Display None

This property hides the div or the container which use this property. Using it on one of the div it will make working clear. 

Example: Use the given CSS in above example. 

#geeks2 {
background: cyan;
display: none;
}

Output: Display none property on block 2

display none property

5. Using Display Flex and Display Grid

These values introduce powerful layout options. Flexbox (display: flex) enables flexible, one-dimensional layouts, while CSS Grid (display: grid) provides two-dimensional grid-based layouts.

CSS display Property – FAQs

What does the display property do in CSS?

The display property in CSS determines how an element is displayed on the page, defining whether it behaves as a block, inline, flex, grid, or other types of elements. It’s one of the most fundamental properties in layout design.

What are common values for the display property?

Common values include block, inline, inline-block, flex, grid, and none. Each value defines how an element and its children are rendered and laid out in the document flow.

How does display: none differ from visibility: hidden?

While both hide content, display: none removes the element entirely from the document flow, while visibility: hidden keeps the element in the flow but hides it from view, maintaining its space in the layout.

What is the difference between block and inline elements?

Block elements take up the full width available and start on a new line, while inline elements take up only as much width as needed and don’t break the flow. You can change these behaviors using display: block or display: inline.

Can I change an inline element to behave like a block element?

Yes, by setting display: block on an inline element like a span, it will take up the full width and behave like a block element.



Next Article

Similar Reads

three90RightbarBannerImg