You give a <span> a width: 120px, but its width doesn't change at all. A <div> with the same value becomes 120px wide right away. The cause of the difference is the display property.
display decides how an element takes up space on the page: it takes a whole line, it flows inside a line of text, or it doesn't show at all. Every tag already has a default display value. <div>, <p> and <h1> are block by default, and <li> behaves like block. <span>, <a> and <strong> are inline by default.
block, inline and inline-block
This example has three <div> elements with display: inline-block:
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
<style>
.item {
display: inline-block;
width: 120px;
height: 44px;
background: lightblue;
}
</style>
.itemis the selector: the declarations inside{ }apply to all three elements withclass="item".display: inline-blockreplaces the default value of a<div>, which isblock.width: 120pxandheight: 44pxare the size of each box.
The result: three blue boxes, 120 × 44px each, side by side. Compare these three display values:
block: the element takes a whole line, so the next element moves down to a new line.widthandheightapply. Withblock, the three boxes in the example are stacked one below the other.inline: the element flows inside a line of text, and it is only as wide as its content. The browser ignoreswidthandheight, and top and bottom margins too. Withinline, the three boxes shrink to the size of the letters A, B and C.inline-block: the element sits side by side likeinline, butwidth,height, and top and bottom margins and padding apply, as they do withblock.
That is why the <span> at the start of this lesson didn't get wider: a <span> is an inline element, so the browser ignores its width and height. To make the size apply, give the <span> display: inline-block or block.
inline-block is also how you make a menu whose items sit side by side. List items <li> also take a whole line, so they are stacked one below the other. With display: inline-block, the items sit side by side, and their padding still applies.
Hiding with display: none
This rule hides every element that has the class ad:
.ad {
display: none;
}
display: none makes the element disappear completely, as if it were not on the page. The element below it moves up into its place right away. Screen readers don't read it either. .ad is written with a dot in front because ad is a class name.
visibility: hidden is different. The element can't be seen either, but its place is kept, so the page has an empty space the size of the element.
Ways to hide an element:
| Way | Result | When to pick it |
|---|---|---|
display: none, or the hidden attribute on the HTML tag |
Gone for everyone, with no space | A part you really don't use, like the ads in this example |
visibility: hidden |
Can't be seen, but keeps its space | So the page layout doesn't shift |
opacity: 0 |
See-through, but still clickable and still read by screen readers | Only during a fade animation, together with pointer-events: none so it can't be clicked |
the .sr-only class below |
Can't be seen, but still read by screen readers | Text for a button that only shows an icon |
The .sr-only class makes its box only 1 pixel wide and 1 pixel tall, and cuts off the text that doesn't fit. Some of its properties are covered in later lessons:
.sr-only {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
<button>✕ <span class="sr-only">Close menu</span></button> shows only ✕, but a screen reader reads it as "✕ Close menu".
A value for the parent: flex
This example puts display: flex on a menu:
.menu {
display: flex;
}
The values block, inline, inline-block and none change the element they are written on. display: flex is different: you put it on the parent, and all the children of that parent are placed in one row automatically. The row can be changed into a column. The value is written flex, not flexbox. There is also display: grid, for a layout with rows and columns at the same time. Flex and grid have lessons of their own.
⚠️ Elements that won't hide, and gaps that appear by themselves
secret { display: none; }selects a<secret>tag, not the element withclass="secret". There is no such tag, so the element still shows. Write.secret, with a dot in front.- Spaces or line breaks between the HTML tags show as small gaps between inline-block boxes, just like the space between two words.
- The browser doesn't recognise
display: inline block(no hyphen) ordisplay: hidden, so it quietly skips them.
TL;DR: The display property decides how an element takes up space on the page. block takes a whole line, and width/height apply. inline flows inside a line of text and ignores width/height. inline-block sits side by side but its size still applies, which suits a menu with items side by side. none removes the element and its space, while visibility: hidden hides the element but leaves an empty space. display: flex goes on a parent and places its children in one row.
⚡ Side by side, with a size
Write:
.item {
display: inline-block;
width: 120px;
height: 44px;
background: lightblue;
}
Guess first: this rule is applied to three <div> elements. Without display: inline-block, how are the three arranged?
One below the other. The default display of a <div> is block, so each box takes a whole line.
Meaning: The display property decides how an element takes up space on the page. block takes a whole line. inline flows inside a line of text and ignores width and height: that is why a <span>, which is inline by default, can't be made wider. inline-block sits side by side like inline, but width and height still apply.
⚡ Gone completely, or a space left
Write:
.ad {
display: none;
}
Guess first: if display: none is replaced with visibility: hidden, is the result different?
Yes. Both make the element impossible to see. With visibility: hidden, the element's place is kept, so there is an empty space the size of the element. With display: none, its place is gone too.
Meaning: display: none makes the element disappear completely, and the element below it moves up into its place.
⚡ flex goes on the parent
Meaning: display: flex is written on the parent, not on the elements you want to arrange. All the children of that parent are then placed in one row. The value is written flex, not flexbox.