Css Display Property Block Inline Inline Block None Complete Guide

 Last Update:2025-06-22T00:00:00     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    7 mins read      Difficulty-Level: beginner

Online Code run

🔔 Note: Select your programming language to check or run code at

💻 Run Code Compiler

Step-by-Step Guide: How to Implement CSS Display Property block, inline, inline block, none

CSS display Property

The display property in CSS controls the layout and visibility of HTML elements. We will explore four common values: block, inline, inline-block, and none.

1. display: block

  • Behavior: By default, HTML elements like <div>, <p>, and headings (<h1>, <h2>, etc.) are blocks.
  • Properties:
    • Takes up the full width available.
    • Starts on a new line.
    • Height and width can be set.
    • Margins and paddings can be applied.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  .block-example {
    background-color: lightblue;
    height: 50px;
    width: 200px;
    margin: 5px;
    padding: 5px;
    display: block;
  }
</style>
</head>
<body>

<h2>Block Element Example</h2>
<div class="block-example">I am a block element.</div>
<div class="block-example">I am another block element.</div>

</body>
</html>

In this example, each div takes up its own line and occupies the full width available by default.

2. display: inline

  • Behavior: Elements like <span>, <a>, and <strong> are inline by default.
  • Properties:
    • Takes up only as much width as necessary.
    • Does not start on a new line.
    • Height and width cannot be set.
    • Vertical margins and paddings cannot be applied (only horizontal).

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  .inline-example {
    background-color: lightcoral;
    margin: 5px;
    padding: 5px;
    display: inline;
  }
</style>
</head>
<body>

<h2>Inline Element Example</h2>
<span class="inline-example">I am an inline element.</span>
<span class="inline-example">I am another inline element.</span>

</body>
</html>

In this example, the span elements appear on the same line and only take as much width as necessary.

3. display: inline-block

  • Behavior: The element is treated as an inline element, but you can set its width and height.
  • Properties:
    • Takes up only as much width as necessary (unless specified).
    • Does not start on a new line.
    • Height and width can be set.
    • Margins and paddings (both vertical and horizontal) can be applied.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  .inline-block-example {
    background-color: lightgreen;
    height: 50px;
    width: 150px;
    margin: 5px;
    padding: 5px;
    display: inline-block;
  }
</style>
</head>
<body>

<h2>Inline-Block Element Example</h2>
<div class="inline-block-example">I am an inline-block element.</div>
<div class="inline-block-example">I am another inline-block element.</div>

</body>
</html>

In this example, the div elements appear on the same line but maintain their specified width and height.

4. display: none

  • Behavior: The element is completely removed from the layout.
  • Properties:
    • Does not take up any space.
    • The element is not visible and does not affect the layout at all.

Example:

<!DOCTYPE html>
<html>
<head>
<style>
  .none-example {
    background-color: lightgoldenrodyellow;
    height: 50px;
    width: 200px;
    margin: 5px;
    padding: 5px;
    display: none;
  }
</style>
</head>
<body>

<h2>None Element Example</h2>
<div class="none-example">I will not be displayed.</div>
<div>This div takes up the space that would be occupied by the previous div if it were visible.</div>

</body>
</html>

In this example, the first div is not visible or space is reserved for it. The second div takes up the full space.

Summary

  • block: Full width, new line, height and width can be set.
  • inline: Only as much width as necessary, no new line, cannot set height and width.
  • inline-block: Only as much width as necessary, no new line, height and width can be set.
  • none: Not visible, no space reserved.

Top 10 Interview Questions & Answers on CSS Display Property block, inline, inline block, none

Top 10 Questions and Answers About the CSS Display Property: block, inline, inline-block, and none

1. What is the CSS display property, and why is it essential?

2. What happens when an element is set to display: block;?

When an element is set to display: block;, it appears on the page as a rectangular box taking up the full width available to it, pushing any subsequent block-level elements to a new line. It also respects the height, width, margins, and padding properties fully.

Example:

div {
    display: block;
    width: 100px;
    height: 50px;
    background-color: lightblue;
    margin: 10px;
    padding: 10px;
}

3. How does display: inline; differ from display: block;?

Inline elements (display: inline;) appear in a line, taking up only as much width as necessary and are not forced to new lines. They do not respond to the width and height properties, and vertical margin and padding have no effect. They sit next to each other, not on top of one another like block-level elements.

Example:

span {
    display: inline;
    padding: 5px;
    background-color: yellow;
}

4. Can you explain how display: inline-block; works?

Display: inline-block; provides the best of both worlds by allowing elements to sit side by side like inline elements, but still respect the width and height, margin, and padding properties like block elements.

Example:

button {
    display: inline-block;
    padding: 10px 20px;
    background-color: lightgreen;
    border: none;
    cursor: pointer;
}

5. What is the purpose of display: none;?

The display: none; property hides an element from both the page and the layout flow. It’s a useful technique for dynamically showing or hiding elements using JavaScript. An element with this property will not take up any space.

Example:

.hidden {
    display: none;
}

6. Should I use display: none; or visibility: hidden; to hide elements?

While both properties hide elements from view, they serve different purposes. Display: none; removes the element from the HTML document's flow entirely, so it doesn't take up space. Visibility: hidden; simply makes the element invisible but still occupies space on the page.

Example:

.invisible {
    visibility: hidden;
}

.gone {
    display: none;
}

7. How do block-level elements behave in different screen sizes?

Block-level elements will span the entire width of their parent container unless specified otherwise. This behavior makes them inherently responsive, as they adapt to the full width of their container, making them suitable for creating responsive layouts.

8. Can elements with display: block; or inline-block; overlap each other?

By default, block-level and inline-block elements do not overlap because they are managed by the layout flow of the document. However, you can use CSS properties like position, z-index, or transform to create overlaps.

Example:

.box {
    position: relative;
    z-index: 1;
}
.overlap {
    position: absolute;
    left: 100px;
    top: 100px;
    z-index: 2;
}

9. Which display property should I use for navigation menus?

For navigation menus, display: inline-block; or display: flex; is often preferred. Inline-block allows the items to sit side by side while handling spacing and alignment, making it suitable for horizontal menus.

Example:

nav a {
    display: inline-block;
    padding: 10px;
    margin: 5px;
    background-color: #f3f3f3;
}

10. When should I use display: flex; instead of specifying block, inline, or inline-block?

Flexbox (display: flex; or display: inline-flex;) is ideal for flexible layouts and aligning elements in one dimension (row or column) while accommodating different screen sizes. It simplifies spacing and alignment compared to traditional block, inline, or inline-block properties.

Example:

You May Like This Related .NET Topic

Login to post a comment.