Bird
Raised Fist0
CSSmarkup~5 mins

Display property in CSS

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

The display property controls how an element appears on the page. It helps decide if an element is shown, hidden, or arranged in a certain way.

To hide or show parts of a webpage without deleting them.
To arrange elements side by side or stacked vertically.
To change a block element into an inline element or vice versa.
To create flexible layouts using grid or flexbox.
To control how elements behave inside containers.
Syntax
CSS
selector {
  display: value;
}
The value can be things like block, inline, none, flex, or grid.
Changing display affects layout and visibility of elements.
Examples
Paragraphs are block elements by default, taking full width and stacking vertically.
CSS
p {
  display: block;
}
Spans are inline elements by default, flowing inside text without line breaks.
CSS
span {
  display: inline;
}
This hides the div completely from the page.
CSS
div {
  display: none;
}
Flex display arranges child elements in a row or column easily.
CSS
nav {
  display: flex;
}
Sample Program

This page shows different display values:

  • Block Box is a block element, so it takes full width and appears on its own line.
  • Inline Boxes are inline elements, so they sit side by side and ignore width/height.
  • None Box is hidden and does not appear.
  • Flex Container arranges its children in a row with spacing.
CSS
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Display Property Example</title>
  <style>
    .block-box {
      display: block;
      width: 10rem;
      height: 5rem;
      background-color: lightblue;
      margin-bottom: 1rem;
      text-align: center;
      line-height: 5rem;
      border: 1px solid #333;
    }
    .inline-box {
      display: inline;
      width: 10rem; /* ignored for inline */
      height: 5rem; /* ignored for inline */
      background-color: lightgreen;
      border: 1px solid #333;
      padding: 0.5rem;
      margin-right: 1rem;
    }
    .none-box {
      display: none;
    }
    .flex-container {
      display: flex;
      gap: 1rem;
      margin-top: 1rem;
    }
    .flex-item {
      background-color: lightcoral;
      padding: 1rem;
      color: white;
      border-radius: 0.5rem;
    }
  </style>
</head>
<body>
  <h1>Display Property Demo</h1>
  <div class="block-box">Block Box</div>
  <span class="inline-box">Inline Box 1</span>
  <span class="inline-box">Inline Box 2</span>
  <div class="none-box">You cannot see me!</div>
  <div class="flex-container">
    <div class="flex-item">Flex 1</div>
    <div class="flex-item">Flex 2</div>
    <div class="flex-item">Flex 3</div>
  </div>
</body>
</html>
OutputSuccess
Important Notes

Using display: none hides the element and it does not take space on the page.

Inline elements ignore width and height settings.

Flex and grid displays help create modern layouts easily.

Summary

The display property controls how elements appear and behave on the page.

Common values are block, inline, none, flex, and grid.

Changing display helps with layout and visibility of webpage parts.

Practice

(1/5)
1. What does the CSS display: none; property do to an element on a webpage?
easy
A. It hides the element and removes it from the page layout.
B. It makes the element visible but transparent.
C. It changes the element to a block-level element.
D. It makes the element inline without line breaks.

Solution

  1. Step 1: Understand the effect of display: none;

    This property hides the element completely and removes it from the page layout, so it takes no space.
  2. Step 2: Compare with other display values

    Unlike visibility: hidden; which hides but keeps space, display: none; removes the element entirely.
  3. Final Answer:

    It hides the element and removes it from the page layout. -> Option A
  4. Quick Check:

    display: none; = hidden and no space [OK]
Hint: None means hidden and no space taken [OK]
Common Mistakes:
  • Confusing with visibility: hidden
  • Thinking it only makes element invisible but keeps space
  • Mixing with display: inline or block
2. Which of the following is the correct CSS syntax to make an element a flex container?
easy
A. display-flex: true;
B. display = flex;
C. display: flex;
D. flex-display: block;

Solution

  1. Step 1: Recall correct CSS property syntax

    CSS properties use a colon : to assign values, not equals =.
  2. Step 2: Identify the correct property and value

    The property is display and the value to create a flex container is flex.
  3. Final Answer:

    display: flex; -> Option C
  4. Quick Check:

    Correct CSS syntax uses colon and display: flex; [OK]
Hint: Use colon, not equals, for CSS properties [OK]
Common Mistakes:
  • Using equals sign instead of colon
  • Incorrect property names like display-flex
  • Adding extra words like true
3. Given this HTML and CSS, what will be the visible layout of the items?
<div class='container'>
  <span>A</span>
  <span>B</span>
  <span>C</span>
</div>

.container {
  display: flex;
  flex-direction: column;
}
medium
A. The items A, B, C appear as inline text separated by spaces.
B. The items A, B, C appear side by side horizontally.
C. The items A, B, C are hidden and not visible.
D. The items A, B, C appear stacked vertically in a column.

Solution

  1. Step 1: Understand display: flex; with flex-direction: column;

    Flex container arranges children in a flexible box. The column direction stacks items vertically.
  2. Step 2: Predict the layout of the spans

    Each <span> will appear one below the other in a vertical column.
  3. Final Answer:

    The items A, B, C appear stacked vertically in a column. -> Option D
  4. Quick Check:

    flex-direction: column; stacks vertically [OK]
Hint: Flex column stacks items vertically [OK]
Common Mistakes:
  • Assuming flex always arranges horizontally
  • Confusing inline elements with flex behavior
  • Ignoring flex-direction property
4. You want to hide a paragraph but keep its space on the page. Which CSS property and value should you use? The following code does not work as expected:
p {
  display: none;
}

What is the correct fix?
medium
A. Use display: inline; to hide the paragraph
B. Change display: none; to visibility: hidden;
C. Add opacity: 0; instead of display
D. Change display: none; to display: block;

Solution

  1. Step 1: Understand difference between display: none; and visibility: hidden;

    display: none; removes element and space, visibility: hidden; hides element but keeps space.
  2. Step 2: Apply the correct property to keep space

    Use visibility: hidden; to hide content but preserve layout space.
  3. Final Answer:

    Change display: none; to visibility: hidden; -> Option B
  4. Quick Check:

    Keep space by using visibility: hidden; [OK]
Hint: Use visibility hidden to keep space but hide [OK]
Common Mistakes:
  • Using display none and expecting space to remain
  • Using opacity 0 but element still clickable
  • Confusing display inline with hiding
5. You have a navigation bar with list items. You want the list items to appear horizontally with equal spacing and be centered. Which CSS display property and layout method should you use?
hard
A. Use display: flex; on the container with justify-content: space-around;.
B. Use display: block; on list items and text-align: center; on the container.
C. Use display: inline; on the container and margin: auto; on list items.
D. Use display: grid; on list items with grid-template-columns: repeat(3, 1fr);.

Solution

  1. Step 1: Identify the container and desired layout

    The container holds list items that should be horizontal, spaced equally, and centered.
  2. Step 2: Choose display and alignment properties

    display: flex; on container creates a flexible row layout. justify-content: space-around; spaces items evenly with space around them.
  3. Final Answer:

    Use display: flex; on the container with justify-content: space-around;. -> Option A
  4. Quick Check:

    Flex + justify-content space-around = horizontal equal spacing [OK]
Hint: Flex container with justify-content spaces items evenly [OK]
Common Mistakes:
  • Applying display properties to wrong elements
  • Using block display which stacks vertically
  • Confusing inline display with container layout