CSS Display

The CSS display property is crucial for controlling how elements are displayed on a web page. It determines the layout and visibility of elements, influencing how they interact with each other.

Syntax

selector {
  display: value;
}

Try It Now

Common display Values

  1. block: The element is displayed as a block-level element. It starts on a new line and takes up the full width available.
    div {
      display: block;
    }
    

    Try It Now

  2. inline: The element is displayed as an inline element, which means it does not start on a new line and only takes up as much width as necessary.
    span {
      display: inline;
    }
    

    Try It Now

  3. inline-block: Like inline, but allows setting width and height.
    button {
      display: inline-block;
    }
    

    Try It Now

  4. none: The element is not displayed on the page at all (hidden).
    p {
      display: none;
    }
    

    Try It Now

  5. flex: The element behaves as a flex container, allowing its children to align and distribute space dynamically. Flexbox is highly useful for creating responsive layouts.
    .container {
      display: flex;
      justify-content: space-between; /* Distributes space between flex items */
    }
    

    Try It Now

  6. griddisplay: grid turns an element into a grid container. CSS Grid allows you to define rows and columns and place items into them. It provides powerful layout control over a two-dimensional space (both horizontally and vertically).
    .grid-container {
      display: grid;
      grid-template-columns: repeat(3, 1fr); /* Creates 3 equal columns */
    }
    

    Try It Now

  7. inline-flex: Similar to flex, but displayed as an inline element. This is useful when you want the behavior of a flexbox but still want the element to align inline with other content.
    .inline-flex-container {
      display: inline-flex;
      align-items: center;
    }
    

    Try It Now

  8. inline-grid: display: inline-grid is similar to inline-flex, but it makes the element behave like an inline element while also being a grid container.
    .inline-grid-container {
      display: inline-grid;
      grid-template-columns: 1fr 1fr;
    }
    

    Try It Now

  9. table: The element behaves like a table.
    .table {
      display: table;
    }
    

    Try It Now

Example Usage

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS Display Example</title>
  <style>
    .block-element {
      display: block;
      background-color: lightblue;
      margin: 10px 0;
    }

    .inline-element {
      display: inline;
      background-color: lightcoral;
      margin: 10px;
    }

    .none-element {
      display: none;
    }

    .flex-container {
      display: flex;
      background-color: lightgray;
      padding: 10px;
    }

    .flex-item {
      background-color: lightgreen;
      margin: 5px;
      padding: 10px;
    }
  </style>
</head>
<body>
  <div class="block-element">This is a block element.</div>
  <span class="inline-element">This is an inline element.</span>
  <span class="inline-element">Another inline element.</span>

  <div class="none-element">This element is hidden.</div>

  <div class="flex-container">
    <div class="flex-item">Flex Item 1</div>
    <div class="flex-item">Flex Item 2</div>
    <div class="flex-item">Flex Item 3</div>
  </div>
</body>
</html>

Try It Now

Tips for Using display

  1. Understanding Layout: Knowing the difference between block and inline elements helps manage layout and spacing.
  2. Visibility vs. Display: Use display: none to completely hide an element, as it removes it from the document flow, unlike visibility: hidden, which hides the element but keeps the space occupied.
  3. Advanced Layouts: Use flex and grid for creating complex layouts that are responsive and flexible.

By mastering the display property, you can significantly control the layout and presentation of elements on a webpage, making it a fundamental concept for web design and development.