Reputation: 397
I have a flex nav and it's wrapping to the left and partially off of the screen. Does anyone with more experience know what could be causing this? I can't figure out why it's doing this.
header {
background-color: rgba(255, 165, 0, .8);
border-bottom: 12px solid black;
display: flex;
flex-flow: row wrap;
align-content: center;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
border: 8px solid black;
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
text-align: center;
margin: 1px;
border: 1px solid white;
border-radius: 15px;
}
a {
text-decoration: none;
}
a:visited,
a,
h1 {
color: white;
}
@media screen and (min-width: 400px) {
header {
height: 120px;
display: flex;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 8%;
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
}
header nav {
display: block;
align-self: flex-end;
}
nav ul {
display: flex;
justify-content: flex-end;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
display: none;
top: 38px;
white-space: nowrap;
left: 0;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
}
.characters:hover .drop-menu {
display: block;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item"><a href="index.html">Home</a></li>
<li class="main-nav-item"><a href="about.html">About</a></li>
<li class="main-nav-item characters">
<a href="characters.html">Characters</a>
<ul class="drop-menu">
<li><a href="ethanClarke.html">Ethan Clarke</a></li>
<li><a href="serenaKiriaga.html">Serena Kiriaga</a></li>
<li><a href="MarcusFlynn.html">Marcus Flynn</a></li>
<li><a href="EmilyAshdown.html">Emily Ashdown</a></li>
<li><a href="MilesWest.html">Director Miles West</a></li>
</ul>
</li>
<li class="main-nav-item"><a href="auther.html">Author</a></li>
</ul>
</nav>
</header>
https://jsfiddle.net/ca75sqzc/17/
Upvotes: 2
Views: 2676
Reputation: 371013
Don't use percentages on flex item margins. Use another unit, such as px
or em
.
When you make the primary container (.header
) a flex container, its children become flex items.
These are the two children: h1
and nav
(red borders added below)
Each nav item (li
) has a horizontal margin (1% on each side).
nav ul li{
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
This causes them to overflow the container.
Then, because the container has justify-content: flex-end
, the items are aligned to the container's right edge. This means the overflow occurs on the left side (see image above).
On smaller screens, the nav element wraps to the left edge of the header, and the overflowing items go out of view:
If you switch to justify-content: flex-start
, the items overflow on the right side.
But the real problem is actually this:
Why isn't the
ul
container expanding to accommodate theli
children?
The answer appears to be the use of percentages for the horizontal margins.
nav ul {
display: flex;
justify-content: flex-end;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
The container is not recognizing this unit on the margins and, therefore, not expanding.
Note that the flexbox spec recommends against using percentage margins and padding on flex items.
4.2. Flex Item Margins and Paddings
Authors should avoid using percentages in paddings or margins on flex items entirely, as they will get different behavior in different browsers.
Once you switch to non-percentage units on your margins, everything seems to work.
Upvotes: 3
Reputation: 67738
Just erase justify-content: flex-end;
from the rule for nav ul
in the media query section:
header {
background-color: rgba(255, 165, 0, .8);
border-bottom: 12px solid black;
display: flex;
flex-flow: row wrap;
align-content: center;
}
header h1 {
text-align: center;
margin: 0;
padding: 15px;
text-shadow: 5px 5px 5px rgba(0, 0, 0, .3);
border: 8px solid black;
}
header h1,
nav a {
font-weight: 700;
font-family: arial;
}
header nav {
display: none;
}
nav ul {
list-style: none;
padding: 0;
}
nav ul li {
text-align: center;
margin: 1px;
border: 1px solid white;
border-radius: 15px;
}
a {
text-decoration: none;
}
a:visited,
a,
h1 {
color: white;
}
@media screen and (min-width: 400px) {
header {
height: 120px;
display: flex;
justify-content: space-between;
}
header h1 {
margin: 0 0 0 8%;
font-size: 2em;
align-self: flex-start;
white-space: nowrap;
}
header nav {
display: block;
align-self: flex-end;
}
nav ul {
display: flex;
margin: 0 8% 0 0;
}
nav ul li {
border-radius: 8px;
padding: 5px;
margin: 4px 1%;
font-size: 1.5rem;
}
.characters:hover {
position: relative;
border-radius: 8px 8px 0 0;
}
.drop-menu {
position: absolute;
display: none;
top: 38px;
white-space: nowrap;
left: 0;
background-color: rgba(255, 165, 0, .8);
border: 1px solid rgba(0, 0, 0, .02);
box-shadow: 5px 5px 5px 2px rgba(0, 0, 0, .3);
}
.characters:hover .drop-menu {
display: block;
}
.drop-menu li {
margin: 0;
border-radius: 0;
}
footer nav {
display: none;
}
}
<header>
<h1>Seraph Chronicles</h1>
<nav>
<ul class="main-nav">
<li class="main-nav-item"><a href="index.html">Home</a></li>
<li class="main-nav-item"><a href="about.html">About</a></li>
<li class="main-nav-item characters">
<a href="characters.html">Characters</a>
<ul class="drop-menu">
<li><a href="ethanClarke.html">Ethan Clarke</a></li>
<li><a href="serenaKiriaga.html">Serena Kiriaga</a></li>
<li><a href="MarcusFlynn.html">Marcus Flynn</a></li>
<li><a href="EmilyAshdown.html">Emily Ashdown</a></li>
<li><a href="MilesWest.html">Director Miles West</a></li>
</ul>
</li>
<li class="main-nav-item"><a href="auther.html">Author</a></li>
</ul>
</nav>
</header>
https://jsfiddle.net/7dubc44L/
Upvotes: 0
Reputation: 15786
Change the following in your CSS:
nav ul{
display: flex;
/* justify-content: flex-end; */
margin: 0 8% 0 0;
}
Upvotes: 0