CSS Display
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;
}
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;
}
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;
}
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 |
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;
}
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;
}
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;
}
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;
}
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. |
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;
}
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.
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;
}
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 |
14. CSS Display Interview Questions
- What is the purpose of the CSS
displayproperty? - What is the difference between block and inline elements?
- What is
display:inline-block? - What does
display:nonedo? - What is the difference between
display:noneandvisibility:hidden? - When should you use
display:flex? - When should you use
display:grid? - Why is Flexbox preferred for modern layouts?
- How do you change the display property using CSS?
- Which display value removes an element from the document flow?
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.
Quick Quiz
Which CSS display value completely hides an element and removes it from the page layout?