CSS - tutorial - 20 - display

Revision:


Content

The display property specifies the display behavior (the type of rendering box) of an element. Display values Display: choose display values Display: display block Display: display inline Display: display inline-block Display: display contents Display: display flex


The display property specifies the display behavior (the type of rendering box) of an element.

top

In HTML, the default display property value is taken from the HTML specifications or from the browser/user default style sheet. The default value in XML is inline, including SVG elements.


Display values

top
display: none
one two three,

HELLO WORLD!

, four five six seven.
                <div>one two three, <p class="ex1"> HELLO WORLD! </p>, four five six seven. </div>
                <style>
                    p.ex1 {display: none;}
                </style>  
            
Comment: the element is completely removed.
display: inline
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as an inline element; any height and width properties will have no effect.
                <div>one two three, <p class="ex2">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex2 {display: inline;}
                </style>
            
display: block
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as a block element. it starts on a new line, and takes up the whole width.
                    <div>one two three, <p class="ex3">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex3 {display: block;}
                    </style>
            
display: inline-block
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as an inline-level block container; the element itself is formatted as an inline element, but you can apply height and width values.
                    <div>one two three, <p class="ex4">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex4 {display: inline-block;}
                    </style>
            
display: contents
one two three,

HELLO WORLD!

, four five six seven.
Comment: makes the container disappear, making the child elements children of the element the next level up in the DOM
                    <div>one two three, <p class="ex5">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex5 {display: contents;}
                    </style>
            
display: flex
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as a block-level flex container.
                    <div>one two three, <p class="ex6">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex6 {display: flex;}
                    </style>
            
display: grid
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as a block-level grid container
                    <div>one two three, <p class="ex7">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex7 {display: grid;}
                    </style>
            
display: inline-flex
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as an inline-level flex container.
                    <div>one two three, <p class="ex8">HELLO WORLD!</p>, four five six seven.</div>
                    <style>
                        p.ex8 {display: inline-flex;}
                    </style>
            
display: inline-grid
one two three,

HELLO WORLD!

, four five six seven.
Comment: Displays an element as an inline-level grid container.
                <div>one two three, <p class="ex9">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex9 {display: inline-grid;}
                </style>
            
display: inline-table
one two three,

HELLO WORLD!

, four five six seven.
Comment: the element is displayed as an inline-level table.
                <div>one two three, <p class="ex10">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex10 {display: inline-table;}
                </style>
            
display: list-items
one two three,

HELLO WORLD!

, four five six seven.
Comment: let the element behave like a <li> element
                <div>one two three, <p class="ex11">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex11 {display: list-item;}
                </style>
            
display: run-in
one two three,

HELLO WORLD!

, four five six seven.
Comment: displays an element as either block or inline, depending on context.
 
                <div>one two three, <p class="ex12">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex12 {display: run-in;}
                </style>
            
display: table
one two three,

HELLO WORLD!

, four five six seven.
Comment: let the element behave like a <table> element.
                <div>one two three, <p class="ex13">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex13 {display: table;}  
                </style>
            
display: table-caption
one two three,

HELLO WORLD!

, four five six seven.
Comment: let the element behave like a <caption> element.
                <div>one two three, <p class="ex14">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex14 {display: table-caption;}
                </style>
            
display: table-column-group
one two three,

HELLO WORLD!

, four five six seven.
Comment: let the element behave like a <colgroup> element.
                <div>one two three, <p class="ex15">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex15 {display: table-column-group;}
                </style>
            
display: table-header-group
one two three,

HELLO WORLD!

, four five six seven.
Comment: Let the element behave like a <thead> element.
                <div>one two three, <p class="ex16">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex16 {display: table-header-group;}
                </style>
            
display: table-footer-group
one two three,

HELLO WORLD!

, four five six seven.
Comment: Let the element behave like a <tfoot> element.
                <div>one two three, <p class="ex17">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex17 {display: table-footer-group;}
                </style>
            
display: table-row-group
one two three,

HELLO WORLD!

, four five six seven.
Comment: let the element behave like a <tbody> element
                <div>one two three, <p class="ex18">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex18 {display: table-row-group;}
                </style>
            
display: table-cell
one two three,

HELLO WORLD!

, four five six seven.
Comment: Let the element behave like a <td> element.
                <div>one two three, <p class="ex19">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex19 {display: table-cell;}
                </style>
            
display: table-column
one two three,

HELLO WORLD!

, four five six seven.
Comment:Let the element behave like a <col> element .
code
                <div>one two three, <p class="ex20">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex20 {display: table-column;}
                </style>
            
display: table-row
one two three,

HELLO WORLD!

, four five six seven.
Comment:Let the element behave like a <tr> element.
                <div>one two three, <p class="ex21">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex21 {display: table-row;}
                </style>
            
display: initial
one two three,

HELLO WORLD!

, four five six seven.
Comment: sets this property to its default value.
                <div>one two three, <p class="ex22">HELLO WORLD!</p>, four five six seven.</div>
                <style>
                    p.ex22 {display: initial;}
                </style>
            

Display: choose display values

top
First Second Third
First Second Third
            <div>
                <label for="display">Choose a display value:</label>
                <select id="display">
                  <option selected>block</option>
                  <option>initial</option>
                  <option>inline</option>
                  <option>inline-block</option>
                  <option>none</option>
                  <option>flex</option>
                  <option>inline-flex</option>
                  <option>grid</option>
                  <option>inline-grid</option>
                  <option>table</option>
                  <option>list-item</option>
                </select>
            </div>
            <style>
                article {background-color: burlywood;}
                article span{background-color: red; color: white; margin: 1px;}
                article, span {padding: 1vw;border-radius: 1.5vw;}
                article, div { margin: 2vw;}
                label{font-size: 1.5vw;}
                select{width: 12vw; height: 2.5vw;font-size: 1.25vw; border-radius: 5%;}
                option{font-size: 1.25vw;}
            </style>                    
            <script>
                const articles = document.querySelectorAll('.container');
                const select = document.querySelector('select');
                function updateDisplay() {
                    articles.forEach((article) => {
                    article.style.display = select.value;
                    });
                }
                select.addEventListener('change', updateDisplay);
                updateDisplay();
          </script>
        

Display: display block

top
display: block 1 2 3
            <div class="ex1">display: block
                <span class="block">1</span>                  
                <span class="block">2</span>
                <span class="block">3</span>
            </div>                                                
            <style>
                .ex1{display: block; color: darkred; font-weight: bold; text-align: center; border:
                    3px solid lightblue; background-color: lightgrey; height: 20vw;}
                .block{display: block; width: 80%; border: 1px solid AliceBlue; background-color:
                     darkgrey; height: 4vw; font-weight: normal; text-align: center; padding-top: 2vw;}
                </style>
        

Display: display inline

top
display: inline
            <ul>
                <li class="in">Menu.1</li>  | 
                <li class="in">Menu.2</li>  | 
                <li class="in">Menu.3</li>  | 
                <li class="in">Menu.4</li> 
            </ul>
            <style>
                .ex2{margin-top: 1vw; color: darkred; font-weight: bold; text-align: center; 
                    border:3px solid lightblue; background-color: lightgrey; height: 5vw;}
                .in{display: inline; font-weight: normal;} 
            </style>
        

Display: display inline-block

top
display; inline-block

First block paragraph

Another block paragraph

            <p>First block paragraph</p>
            <ul>
                <li class="inb">This is block menu.1</li>
                <li class="inb">This is block menu.2</li>
            </ul>
            <p>Another block paragraph</p>
                <ul>
                    <li class="inb">This is block menu.1</li>
                    <li class="inb">This is block menu.2</li>
                </ul>
            <style>
                .ex3{margin-top: 1vw; color: darkred; text-align: center; font-weight: bold; 
                    border:3px solid lightblue; background-color: lightgrey; height: 10vw;}
                .inb{display: inline-block; font-weight: normal;} 
            </style>
        

Display: display contents

top
display: contents

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit. HELLO WORLD! Vestibulum volutpat tellus diam, consequat gravida libero rhoncus ut.

            <p class="con">
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                Etiam semper diam at erat pulvinar, at pulvinar felis blandit. 
                <span class="b">HELLO WORLD!</span> Vestibulum volutpat tellus diam,
                 consequat gravida libero rhoncus ut.
            </p>
            <style>
                .ex4{margin-top: 1vw; color: darkred; text-align: center; font-weight: bold; 
                    border:3px solid lightblue; background-color: lightgrey; height: 10vw;} 
                .con{display: contents; border: 1px solid red; background-color: 
                    lightgrey; padding: 10px; width: 200px;}
                .b {border: 1px solid blue; background-color: lightblue; padding: 10px;} 
            </style>
        

Display: display flex

top
display: flex
A
B
C
D
E
F

Codes:

            <div id="main">
                <div style="background-color:coral;">A</div>
                <div style="background-color:lightblue;">B</div>
                <div style="background-color:khaki;">C</div>
                <div style="background-color:pink;">D</div>
                <div style="background-color:darkgrey;">E</div>
                <div style="background-color:lightgreen;">F</div>
            </div>
            <style>
                .ex5{margin-top: 1vw; color: darkred; text-align: center; font-weight: bold; border:3px solid lightblue; 
                    background-color: lightgrey; height: 12vw;}
                #main{display: flex; flex-direction: row-reverse;}
                #main div {width: 1vw; height: 10vw;}
            </style>