The display
property in CSS is like the foundation of how elements behave on your web page. Whether you're placing a button, text, navbar, or card β the display
property decides how it will appear and interact.
Letβs break it down using the 5W1H method for easy learning and interview prep.
β What is the display
property?
The display
property defines how an HTML element should behave in the layout β as a block, inline, grid, flex, etc. It controls whether elements stack, sit side-by-side, take full width, or just wrap their content.
π When do you use display
?
Use it whenever you want to:
- Control layout behavior (horizontal/vertical)
- Change how elements appear (e.g., from block to inline)
- Build responsive UIs (with
flex
orgrid
) - Hide/show elements (
display: none
)
π‘ Why is display
important?
- It determines spacing, alignment, and structure
- Helps build layouts without using JavaScript
- Powers modern layouts like navbars, cards, galleries, forms, etc.
- Essential for mobile responsiveness
π Where is it applied?
You can apply display
to any HTML element, either via:
.classname {
display: value;
}
Or inline:
<div style="display: flex;">...</div>
βοΈ How does display
work?
There are different display
values. Here are the most common ones with behavior:
πΉ block
- Takes full width
- Starts on a new line
display: block;
π§± Example elements: <div>
, <p>
, <h1>
(default)
πΉ inline
- Doesnβt start a new line
- Width and height cannot be set
display: inline;
π Example elements: <span>
, <a>
πΉ inline-block
- Like inline, but allows setting width and height
display: inline-block;
π¦ Use case: buttons, icons
πΉ none
- Hides the element completely (no space taken)
display: none;
π« Use case: hide elements conditionally
πΉ flex
- One-dimensional layout system (row or column)
- Easy alignment, spacing, and responsiveness
display: flex;
π§© Use case: navbars, form layouts, cards
πΉ grid
- Two-dimensional layout system (rows + columns)
display: grid;
π§± Use case: dashboards, image galleries, advanced layouts
π Which to use? (Quick Guide)
Goal | Use this |
---|---|
Full-width stacking | block |
Inline element like link/text | inline |
Button with width/height | inline-block |
Hide element | none |
Responsive rows/columns | flex |
Full layout with rows/columns | grid |
π Practical Example
<div style="display: flex;">
<div style="margin: 10px; padding: 20px; background: lightblue;">Box 1</div>
<div style="margin: 10px; padding: 20px; background: lightgreen;">Box 2</div>
</div>
π Two boxes side-by-side using Flexbox!
π§ Differences Between Display Types
Property | block |
inline |
inline-block |
flex |
grid |
---|---|---|---|---|---|
New Line | β Yes | β No | β No | β No | β No |
Set width/height | β Yes | β No | β Yes | β Yes | β Yes |
Use for layout | β Basic | β Text only | β Small components | β One-dimensional | β Two-dimensional |
Here are interview questions and answers based on the CSS display
property, ideal for freshers and junior developers. This is a great way to prepare for interviews or even include in your blog.
π― CSS display
Property β Interview Questions & Answers
β
1. What is the display
property in CSS?
Answer:
The display
property defines how an HTML element should behave in the document layout. It controls whether an element appears as a block, inline, flex, grid, etc.
β
2. What is the default display
value for a <div>
and a <span>
?
Answer:
-
<div>
has a defaultdisplay: block
-
<span>
has a defaultdisplay: inline
β
3. What is the difference between block
and inline
elements?
Answer:
Feature | block |
inline |
---|---|---|
Starts on new line | β Yes | β No |
Takes full width | β Yes | β Only required width |
Set width/height | β Yes | β No |
β
4. What is inline-block
and when would you use it?
Answer:
inline-block
behaves like an inline element (doesn't start on a new line) but allows you to set width
, height
, margin
, and padding
.
Use it for components like buttons or badges.
β
5. What does display: none
do?
Answer:
It removes the element from the layout completely. It won't be visible, and it won't occupy any space on the page.
β
6. What is the difference between visibility: hidden
and display: none
?
Answer:
-
visibility: hidden
hides the element but reserves space. -
display: none
hides the element and removes it from layout.
β
7. What is Flexbox and how is it related to display
?
Answer:
Flexbox is a 1D layout model used to arrange items in rows or columns. To use it, you set:
.container {
display: flex;
}
Then you can control item alignment using justify-content
, align-items
, etc.
β
8. When would you use display: grid
over display: flex
?
Answer:
- Use Flexbox for one-dimensional layouts (row OR column).
- Use Grid for two-dimensional layouts (row AND column).
β
9. Can you list some HTML tags and their default display
values?
Answer:
Tag | Default Display |
---|---|
<div> |
block |
<span> |
inline |
<p> |
block |
<img> |
inline-block |
<a> |
inline |
<ul> /<ol>
|
block |
<li> |
list-item |
β
10. How can you center a div
using display
property?
Answer:
Using Flexbox:
.parent {
display: flex;
justify-content: center;
align-items: center;
}
This centers the child both horizontally and vertically.
β
11. What happens when you apply display: flex
to a parent element?
Answer:
All direct children become flex items, and you can align, space, and order them using Flexbox properties like gap
, flex-direction
, justify-content
, etc.
β
12. What is the difference between display: block
and display: flex
?
Answer:
Feature | block |
flex |
---|---|---|
Layout type | Vertical stack | One-dimensional layout |
Alignment control | β Not flexible | β Easily aligns items |
Responsive | β Manual | β Flexible/responsive |
Top comments (0)