CSS Display

The CSS display property determines how an element is displayed in the document. It controls the layout behavior of an element, defining whether it is a block, inline, flex container, grid container, or even hidden. Proper use of the display property ensures efficient and visually appealing layouts.

Key Points on CSS Display:

Property Value
default value inline
inherited no
animation supporting no
version CSS1
JavaScript syntax object.style.display="none"

Syntax for CSS Display Property:

Syntax Example


        /* Syntax for CSS display property */
        .element {
            display: value;
                 }
        
        /* Example */
        .div {
            display: flex;
             }

Examples of CSS Display Properties:

The following examples demonstrate various values of the display property:

Code Example: Display Block


            .div {
            display: block;
            background-color: lightblue;
            padding: 10px;
            margin: 5px 0;
                 }

Output

This is a block-level element.

Code Example: Display Inline


            .span {
            display: inline;
            background-color: green;
            padding: 5px;
                  }

Output

This is inline text.

Code Example: Display Flex


            .div {
            display: flex;
            gap: 10px;
            background-color: lightgray;
            padding: 10px;
                 }
        
           .div > span {
            background-color: lightpink;
            padding: 10px;
                       }

Output

Flex Item 1 Flex Item 2

Code Example: Display None


            .div {
            display: none;
                 }

Output

This content is hidden.
The above content is hidden because of display: none;.

Common Display Values:

Use the display property strategically to create efficient and responsive layouts. For complex designs, combine it with other properties like align-items, justify-content, and position.