I’m making the header of my website and decided to use hr between navigation links instead of border property since I didn’t wanna get into nth child pseudo. Navigation links and logo wrapper are aligned in the center and I stretched hr element.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: "martian", serif;
}
nav {
position: absolute;
top: 1em;
left: 0;
right: 0;
display: flex;
justify-content: center;
align-items: center;
}
hr {
align-self: stretch;
color: #fff;
}
<header>
<nav>
<a href="" class="nav-link">Classroom</a>
<hr>
<a href="" class="nav-link">Manual</a>
<hr>
<div class="logo-wrapper">
<a href="homepage.html" class="logo"><img src="images/tgm_logo.png" class="logo"></a>
<p class="motto">Engineering Behind Great Inventions</p>
</div>
<hr>
<a href="" class="nav-link">About</a>
<hr>
<a href="" class="nav-link">Contact</a>
</nav>
</header>
I want to shorten the hr instead of strecthing it through the whole container, maybe third of the height. I would appreciate your help.
How it looks like
I tried giving a height attribute to the hr instead of streching it, however it just got vanished after doing so.
2
Answers
The
height
is working good for me.You just have to resize it absolutely, as using
px
,pt
,mm
, etc.Using
%
makes it height 0 because of 0-height parent element.Explicitly specifying
height
forhr
works fine. You probably tried to use percentages.Regarding percentages, MDN notes for
height
:Since
nav
hasn’t specified aheight
value explicitly,hr
falls back toauto
, which (for Flexbox Layout) meansmin-content
in this case I believe. This would be the reason why yourhr
seems to "vanish".If you specify an explicit height to
nav
, you can use percentages onhr
as you’d expect. Alternatively, you could also specify an explicit height tohr
directly.Or (but still with explicit values), you could use margins: Having a margin will reduce the element’s size it can stretch to.
Note: HTML5 defines the
<hr>
element as "a paragraph-level thematic break". Similarly, ARIA defines theseparator
role (i.e.<hr>
‘s implicit role) as "a divider that separates and distinguishes sections of content or groups of menuitems".Unless you deem it reasonable to divide all your navigation items into separate groups (with only one item each!), you should think about declaring some of your
<hr>
elements with thepresentation
role.Or, you could use pseudo-elements to create visual separators, e.g. like this: