Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the Display Property in CSS?

In this blog post, we will explore the Display property in CSS and learn how it can be used to control the layout and presentation of HTML elements on a web page. We'll start by discussing the basics of what the Display property does, and then we'll look at some practical examples to help illustrate its usage. So, let's dive in!

What is the Display Property?

The Display property in CSS determines how an HTML element is rendered on the web page. In other words, it specifies the layout mode for the element and how it interacts with other elements on the page. By default, every HTML element has a display value assigned to it, which can be changed using CSS to create different layouts and appearances.

There are several values that the Display property can take, but the most common ones are:

  • block
  • inline
  • inline-block
  • none

We will now explain each of these values and provide some examples to help you better understand how they work. Remember, our goal is to make this as simple as possible for someone who is new to programming, so we will use analogies and intuitions to help you grasp the concepts.

Block Display

When an HTML element is set to display: block;, it behaves like a "box" that takes up the entire width of its parent container. It also creates a new line before and after it, separating it from other elements. This is the default display value for elements like <div>, <p>, and <h1>.

Imagine you're stacking shoeboxes on a shelf. Each shoebox takes up the entire width of the shelf, and you can only place one shoebox per row. This is similar to how block elements work on a web page.

Here's an example to see how block display works:

<!DOCTYPE html>
<html>
<head>
<style>
  .block-element {
    display: block;
    background-color: lightblue;
    padding: 20px;
    margin: 10px;
  }
</style>
</head>
<body>

<div class="block-element">I am a block element.</div>
<div class="block-element">I am another block element.</div>

</body>
</html>

In this example, we have two <div> elements with the class block-element. Since the <div> element has a default display value of block, these elements will take up the entire width of their parent container and be placed on separate lines.

Inline Display

The display: inline; value makes an element behave like an "inline" element, which means it only takes up as much width as its content and does not create new lines before or after it. This is the default display value for elements like <span>, <a>, and <img>.

Think of inline elements as words in a sentence. They flow from left to right and only take up the space they need without forcing a new line.

Here's an example to see how inline display works:

<!DOCTYPE html>
<html>
<head>
<style>
  .inline-element {
    display: inline;
    background-color: lightblue;
    padding: 20px;
    margin: 10px;
  }
</style>
</head>
<body>

<div class="inline-element">I am an inline element.</div>
<div class="inline-element">I am another inline element.</div>

</body>
</html>

In this example, we have two <div> elements with the class inline-element. By setting their display value to inline, they will only take up as much space as their content and will be placed side by side, without creating new lines.

Inline-Block Display

The display: inline-block; value is a combination of both block and inline display. It behaves like an inline element in that it does not create new lines, but it also behaves like a block element in that it allows you to set its width and height.

Imagine you have a row of small boxes that you want to align next to each other. They don't force a new line like the shoeboxes we mentioned earlier, but you can still set their width and height. This is how inline-block elements work.

Here's an example to see how inline-block display works:

<!DOCTYPE html>
<html>
<head>
<style>
  .inline-block-element {
    display: inline-block;
    background-color: lightblue;
    width: 200px;
    height: 100px;
    padding: 20px;
    margin: 10px;
  }
</style>
</head>
<body>

<div class="inline-block-element">I am an inline-block element.</div>
<div class="inline-block-element">I am another inline-block element.</div>

</body>
</html>

In this example, we have two <div> elements with the class inline-block-element. By setting their display value to inline-block, they will be placed side by side like inline elements, but we can also set their width and height like block elements.

None Display

Finally, the display: none; value completely hides an element from the web page. It not only makes the element invisible but also removes it from the normal flow of the page, so other elements will not be affected by its presence.

This can be useful for hiding elements that should only be displayed under certain conditions, such as a modal pop-up or a drop-down menu.

Here's an example to see how none display works:

<!DOCTYPE html>
<html>
<head>
<style>
  .hidden-element {
    display: none;
  }
</style>
</head>
<body>

<div class="hidden-element">I am a hidden element.</div>
<div>I am a visible element.</div>

</body>
</html>

In this example, we have a <div> element with the class hidden-element. By setting its display value to none, it will be completely hidden from the web page, and the visible element below it will be unaffected.

Conclusion

The Display property in CSS is a powerful tool for controlling the layout and presentation of HTML elements on a web page. By understanding the different display values and how they affect element behavior, you can create a wide variety of layouts and designs for your web projects.

Remember, the most common display values are block, inline, inline-block, and none, each with its own unique behavior and use cases. With practice and experimentation, you'll become more comfortable using the Display property and harness its full potential in your web development journey.