CSS Display

CSS Display controls how HTML elements appear on a webpage. It determines whether an element is displayed as a block, inline, inline-block, flex container, grid container, or hidden completely.

1. Introduction to CSS Display

Every HTML element has a default display value. The display property changes how an element is rendered on the page.

Common Display Values
  • block
  • inline
  • inline-block
  • none
  • flex
  • grid

div{

    display:block;

}
The display property is one of the most important CSS properties for controlling layouts.

2. Block Elements

A block element always starts on a new line and occupies the full available width by default.


div{

    display:block;

}
Block Element Description
<div> Container for grouping elements.
<p> Paragraph element.
<h1> to <h6> Heading elements.
<section> Defines a document section.

p{

    display:block;

}
Block elements automatically begin on a new line and stretch across the available width.

3. Inline Elements

Inline elements do not start on a new line. They occupy only the width required by their content.


span{

    display:inline;

}
Inline Element Description
<span> Inline container.
<a> Hyperlink.
<strong> Important text.
<em> Emphasized text.

a{

    display:inline;

}
Width and height properties generally do not affect inline elements.

4. display: inline-block

The inline-block value combines the characteristics of both inline and block elements. It allows elements to stay on the same line while accepting width and height.


.box{

    display:inline-block;

    width:150px;

    height:80px;

}
Feature Supported
Appears on same line ✔ Yes
Width & Height ✔ Yes
Padding & Margin ✔ Yes
inline-block is commonly used for navigation items, buttons, and image galleries.

5. display: none

The display: none; property completely hides an element from the webpage. The hidden element occupies no space in the layout.


.hide{

    display:none;

}
Display Value Result
display:none Completely hides the element.
display:block Shows the element as a block.
display:inline Shows the element inline.

button:hover + p{

    display:block;

}
display: none; removes the element completely from the page layout.

6. display: flex

The display: flex; property creates a flexible layout where child elements are arranged in a row or column. Flexbox makes it easy to align and distribute space between items.


.container{

    display:flex;

}
Property Purpose
display:flex Creates a flex container.
justify-content Aligns items horizontally.
align-items Aligns items vertically.
gap Adds spacing between items.

.container{

    display:flex;

    justify-content:center;

    align-items:center;

    gap:20px;

}
Flexbox is ideal for navigation bars, cards, galleries, and responsive layouts.

7. display: grid

The display: grid; property creates a two-dimensional layout with rows and columns.


.container{

    display:grid;

    grid-template-columns:1fr 1fr 1fr;

}
Property Description
display:grid Creates a grid container.
grid-template-columns Defines the number of columns.
grid-template-rows Defines row sizes.
gap Sets spacing between rows and columns.

.container{

    display:grid;

    grid-template-columns:repeat(3,1fr);

    gap:15px;

}
CSS Grid is perfect for dashboards, image galleries, and complex page layouts.

8. visibility: hidden vs display: none

Both properties hide elements, but they behave differently.

Property Behavior
visibility:hidden Hides the element but keeps its space.
display:none Hides the element and removes its space.

.box1{

    visibility:hidden;

}

.box2{

    display:none;

}
Use visibility:hidden when you want to preserve layout space, and display:none when you want to remove the element completely.

9. Changing Display Property

You can change the display value dynamically using CSS pseudo-classes or JavaScript to create interactive webpages.


.menu{

    display:none;

}

.menu:hover{

    display:block;

}

.box{

    display:inline-block;

}

@media(max-width:768px){

    .box{

        display:block;

    }

}
Display Value Common Use
block Sections and containers.
inline Text elements.
inline-block Buttons and navigation items.
none Hide elements.
Changing the display property is useful for responsive design and interactive menus.

10. Practical Display Examples

Example 1: Flex Layout

.container{

    display:flex;

    gap:20px;

}

Example 2: Grid Layout

.gallery{

    display:grid;

    grid-template-columns:repeat(4,1fr);

}

Example 3: Hide an Element

.notice{

    display:none;

}

Example 4: Inline Block Buttons

.btn{

    display:inline-block;

    padding:10px 20px;

}
By using block, inline, flex, grid, and inline-block together, you can create modern and responsive website layouts.

11. CSS Display Best Practices

Using the appropriate display value makes your webpage more organized, responsive, and easier to maintain.

  • Use display:block; for sections and containers.
  • Use display:inline; for small text elements.
  • Use display:inline-block; for buttons and navigation items.
  • Use display:flex; for one-dimensional layouts.
  • Use display:grid; for two-dimensional layouts.
  • Use display:none; only when elements should be completely hidden.
  • Prefer Flexbox or Grid instead of using excessive floats.
  • Test layouts on desktop, tablet, and mobile devices.
Choosing the correct display value improves website performance and user experience.

12. Common Display Mistakes

Mistake Correct Practice
Using block instead of inline. Select the correct display value.
Using inline when width is required. Use inline-block instead.
Overusing display:none. Hide elements only when necessary.
Using floats for layouts. Use Flexbox or Grid.
Ignoring responsive layouts. Test display properties on different screen sizes.

❌ Incorrect

span{

    display:block;

}

✔ Better

span{

    display:inline;

}
Selecting the wrong display value can break your webpage layout.

13. Browser Support

CSS display properties such as block, inline, flex, and grid are supported by all modern browsers.

Browser Support
Google Chrome ✔ Supported
Mozilla Firefox ✔ Supported
Microsoft Edge ✔ Supported
Safari ✔ Supported
Opera ✔ Supported
Modern browsers fully support CSS display properties including Flexbox and Grid.

14. CSS Display Interview Questions

  1. What is the purpose of the CSS display property?
  2. What is the difference between block and inline elements?
  3. What is display:inline-block?
  4. What does display:none do?
  5. What is the difference between display:none and visibility:hidden?
  6. When should you use display:flex?
  7. When should you use display:grid?
  8. Why is Flexbox preferred for modern layouts?
  9. How do you change the display property using CSS?
  10. Which display value removes an element from the document flow?
These questions are frequently asked in HTML & CSS interviews and practical examinations.

15. Lesson Summary

In this lesson, you learned:
  • ✔ Introduction to CSS Display.
  • ✔ Block Elements.
  • ✔ Inline Elements.
  • ✔ display: inline-block.
  • ✔ display: none.
  • ✔ display: flex.
  • ✔ display: grid.
  • ✔ visibility: hidden vs display: none.
  • ✔ Changing Display Property.
  • ✔ Practical Display Examples.
  • ✔ Best Practices and Common Mistakes.
Congratulations! You have successfully completed the CSS Display lesson. You can now use display properties to build responsive, flexible, and professional webpage layouts.

Quick Quiz

Which CSS display value completely hides an element and removes it from the page layout?