The following figure points to the current UI:
The given HTML and CSS code:
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Flexbox example page</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<ul class="site-nav">
<li><a href="#">Home</a></li>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Support</a></li>
<li class="nav-right"><a href="#">About</a></li>
</ul>
</nav>
</body>
</html>
*,
::before,
::after {
box-sizing: border-box;
}
body {
margin: unset;
background-color: #709b90;
font-family: Helvetica, Arial, sans-serif;
}
.site-nav {
display: flex;
padding: 0.5rem;
list-style-type: none;
background: #5f4b44;
}
.site-nav > li > a {
text-decoration: none;
color: white;
background: #cc6b5a;
padding: 0.5em 1em;
}
The original problem arises after applying the following CSS:
.site-nav > li > a {
display: block;
}
The resulting UI:
The resulting UI is expected for me, but I can’t figure out how the CSS works here.
NOTE I tried dev tooling, and it appears that the block
value causes the element to take the entire width of its parent, introducing a line break. This increases the height of the nav item’s content to 1px
.
Now, my concern is,
- How does the
display: block
property affect the UI? (i.e. After adding theblock
value, the padding of theli
element increases automatically!) - What’s the underlying idea here that I couldn’t figure out?
- Is there any deep understanding that I’m missing?
3
Answers
display: block vs. inline
In the snippet I created below, I think you’ll get a better picture of what is happening when you add
display: block;
. I have added inline styling to only a few of the<a>
tags to better illustrate the difference between thedisplay: block;
and the defaultdisplay: inline;
.The first, third and fifth
<a>
tags are showing thedisplay: block;
behaviour, which is to take up the space they occupy as though they have rigid containers.The second and fourth
<a>
tags are showing thedisplay: inline;
behaviour, which is that they don’t take up an entire space, but rather, live within the space they have been placed within.I think this summary generally answers all three of your questions, but let me know if further clarification is needed.
TL;DR: Only inline-axis sizes are respected for inline boxes in inline formatting contexts; block-axis sizes are ignored.
CSS Display 3 includes an informative table for
display
, where it lists:display
display
inline
inline flow
Such an inline box is defined as:
Where ‘flow’ is defined as:
The linked "block-and-inline layout" definition is a bit outdated, but it reads (in 9.4.2 Inline formatting contexts of CSS 2.1):
A more up-to-date resource would be 2. Inline Layout Model of CSS Inline 3, though it basically states the same:
This effectively means that block-axis sizes are ignored in these cases.
Example:
By changing from
inline
toblock
, the block-axis margins, borders and paddings will be respected as well: Thea
elements will push out the block-axis size of the ancestor.site-nav
, instead of overlapping the ancestor’s padding.I believe the extra
1px
at the bottom comesvertical-align
beingbaseline
by default, incidentally overlapping the top. (Read more aboutvertical-align
in this answer.)Example comparing
vertical-align: baseline
andmiddle
: