I am trying to make the following custom navbar responsive. I created it using only div blocks because it was easier for me to understand, and the code is shorter, simpler, and pure CSS. Here’s a link to the Codepen: https://codepen.io/familias/pen/WNYraNy.
Here is the CSS:
.table-cell {
display: table-cell;
margin: 0;
padding: 0;
border: 0;
outline: 0;
border-spacing: 0;
border-collapse: collapse;
width: 100%;
}
.superior {
position: relative;
display: inline-block;
}
.superior a {
position: relative;
display: inline-block;
margin: none;
padding: none;
border: none;
outline: none;
font-size: 16px;
color: #000000;
text-transform: uppercase;
text-decoration: none;
height: 30px;
line-height: 30px;
text-align: center;
padding: 8px 20px 8px 20px;
}
.superior a:hover {
text-decoration: none;
color: #e5633f;
font-weight: 400;
}
.inferior {
display: none;
position: absolute;
width: auto;
left: 50%;
transform: translateX(-50%);
text-align: center;
white-space: nowrap;
border-left: 1px solid #000;
border-right: 1px solid #000;
border-bottom: 0px solid #000;
}
.inferior a {
margin: none;
padding: none;
border: none;
outline: none;
display: block;
font-size: 14px;
font-weight: 400;
padding-right: 30px;
padding-left: 30px;
color: #000000;
text-transform: uppercase;
text-decoration: none;
height: 20px;
line-height: 20px;
text-align: center;
border-bottom: 1px solid #000;
}
.inferior a:hover {
font-weight: 400;
text-decoration: none;
color: #fff;
background-color: #000;
}
.superior:hover .inferior {
display: block;
}
<div id="table">
<div id="table-row">
<div class="table-cell">
<div class="superior"><a href="#">News</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior"><a href="#">Politics</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior">
<a href="#">Economy</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior"><a href="#">Culture</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior"><a href="#">Health</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior"><a href="#">Education</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
<div class="superior"><a href="#">Live</a>
<div class="inferior">
<a href="#">This is the first link</a>
<a href="#">This is the second link</a>
</div>
</div>
</div>
</div>
</div>
What I need is for the menu to display vertically when viewed on a mobile phone. How can I achieve this?
2
Answers
You can try this, " @media only screen and (max-width: 600px)" means include a block of CSS properties only if a certain condition is true in this case if screen is smaller than 600px. It will create vertically menu and you can add style as you need.
Here is an option using some grid style with a checkbox label for the hamburger menu: Super ugly but just to show what is where. Note the hover on the menu show the associated
.inferior
but that could be fancier; more just functional here than super pretty just to show how it can be done.