Display Property: CSS/HTML Crash Course (Free Tutorial)

The topic of today’s article is the display property in CSS. It lets you customize your website to look exactly how you want it to. If you are a beginner and want to learn the basics of coding, check out our FREE 30-minute intro course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

In HTML, each element is treated like its own box. For example, if you create a div and put some paragraphs or lists in it, that container and every item in the container is its own box.

This is important to know because, when styling your page, you will want to know what the boxes are and move them around. For instance, you will want to be able to say things like “put this beside this” and “put this on a new line”. These are important properties of HTML that CSS manipulates with the display property. This is known as the CSS Box Model.

To follow along with this tutorial, visit jsbin.com, which allows you to test your code in real time. In JS Bin, click on the HTML tab to open a blank HTML file.

In the body tag, we will make three separate divs and then do some styling for the display property of those divs. A div is an empty container into which you can put items.  Let’s put one list into each div. First create a list of dogs:

  <div class="dogs">
    <ul>
      <li>Golden Retriever</li>
      <li>Labradoodle</li>
      <li>Beagle</li>
    </ul>
</div>

You don’t have to have a class, but since we are manipulating different divs, classes allow us to stylize each list separately. <ul> makes the list unordered. Below that list, create one for cats:

  <div class="cats">
    <ul>
      <li>Domestic Shorthair</li>
      <li>Calico</li>
      <li>Tabby</li>
    </ul>
</div>

Finally, let’s make a list for a navigation bar. Use a tags to insert links into the menu.

  <div class="links">
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About me</a></li>
    </ul>
</div>

The web address to which you want the list item to link goes in the quotation marks. Do not forget to close your tags.

Now open the CSS tab so that you can modify the display properties. First we will target the dogs div. Because we gave it the class name “dogs”, we can target the class name to apply styling to the items within the class. To select the dogs div, code the following in CSS:

.dogs {

}

To make each element in the three lists a block box, use the display property by coding the following:

.dogs {
display: block;
}
.cats {
 display: block;
 }
 .links {
 display: block;
 }

Within the parent container, the body, each list will sit on its own line. In the Output, you won’t notice a difference because display: block is the default behavior of the types of lists we created.

However, you don’t always want to use this type of display. For instance, you may want to have the dogs and cats lists on the same line. To achieve this, use the inline-block property, which is shown in the following box of code.

.dogs {
display: inline-block;
}
.cats {
display: inline-block;
}
.links {
display: block;
}

Now the dogs and cats lists will be side by side in the Output, as long as there is enough room in the Output tab. This intelligent behavior is good for responsive web design because nowadays, a lot of traffic to sites is from mobile browsers.

There are other displays. One example is none, which makes a class disappear from the page:

.dogs {
display: none;
}
.cats {
display: inline-block;
}
.links {
display: block;
}

This comes in handy if, for instance, you use JavaScript to create a button and you want a list to appear only once a button is pressed.

Furthermore, the display inline makes elements sit on the same line as other elements without formatting them like a block:

.dogs {
display: none;
}
.cats {
display: inline-block;
}
.links {
display: block;
}

To apply inline on all three divs, use the format shown in the following code.

div * {
display:inline;
}
.dogs {
display: none;
}
.cats {
display: inline-block;
}
.links {
display: block;
}

You can use display on any CSS selector element, such as an image. A large part of learning to code is experimentation, so play around with creating more classes and IDs. For more FREE tutorials, check out our 30-minute beginners course on coding: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com