I have the following HTML code:
<header>
<h1>Title</h1>
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</header>
I am looking to turn this into a single line on the top of my web page by floating right the <nav>
.
However, because the <h1>
has a much bigger font size than the <li>
elements the text gets misaligned. I want the elements to be aligned on the baseline. In a picture:
Note the red line for the baseline. This is what I have right now:
#float, #nofloat { width: 300px; }
ul, h1, nav, p { margin: 0; padding: 0; }
h1 {
display: inline-block;
font-size: 3rem;
}
nav { display: inline-block; }
ul { list-style-type: none; }
ul > li { display: inline; }
ul > li:after { content: " | "; }
ul > li:last-child:after { content: ""; }
#float nav { float: right; }
<p>Everything is aligned to baseline before floating:</p>
<div id="nofloat">
<header>
<h1>Title</h1>
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</header>
</div>
<p>But after floating it's messed up:</p>
<div id="float">
<header>
<h1>Title</h1>
<nav>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</nav>
</header>
</div>
Your HTML mark-up lends itself to a solution using CSS tables.
Apply
display: table
toheader
, and set the width to 100% which will force it to take on the width of the parent container.Apply
display: table-cell
tonav
and setwidth: 1%
to force it to have a shrink-to-fit width. To prevent text wrapping, setwhite-space: nowrap
.This will work fine as long as your title can fit in the remaining space, but you can probably adjust the details based on your layout.
You can simply add
text-align:right
to yournav
if you have a fixedwidth
Plus since
inline-block
has gap issues (fixable) you can use usedisplay:table/table-cell