nested navbar doesn't align under main nav - html

Hello I'm trying to add a nested nav in my main nav. The problem is the nested nav doesn't align properly under the main nav. Please see the screenshot. The nested nav doesn't align right under the main nav. The nested nav moved around 50% to the right. I'm using css flexbox to layout the nav.
body {
background-color: red; /* to see menu */
}
.nav-wrapper {
display: flex;
align-items: center;
justify-content: space-around;
height: 8rem;
width: 100%;
background-color: transparent;
transition: height 0.5s;
}
.heading a:link,
a:visited {
font-family: 'Alata', sans-serif;
font-size: 3rem;
text-transform: capitalize;
color: white;
cursor: pointer;
display: block;
position: relative;
transition: .3s ease;
}
.heading a:hover,
a:active {
transform: translate(.3rem, -.2rem);
color: white;
}
.navbar {
display: flex;
width: 45%;
justify-content: space-between;
list-style-type: none;
z-index: 1000;
position: relative;
}
.navbar li {
position: relative;
flex: 1 1 auto;
}
.nested {
display: flex;
flex-direction: column;
list-style-type: none;
position: absolute;
width: 100%;
}
.nested li {
width: 100%;
}
.navbar li a:link,
a:visited {
padding: 5px 0px;
cursor: pointer;
color: white;
display: block;
transition: .3s;
}
.navbar li a:hover,
a:active {
transform: scale(1.2);
color: white;
background: rgba(0, 0, 0, 0.082);
}
.toggler {
display: none;
}
<div class="nav-wrapper">
<h1 class="heading">HeaderName</h1>
<div class="toggler">
<i class="fas fa-bars"></i>
</div>
<ul class="navbar">
<li> Home
<ul class="nested">
<li>
<i class="fas fa-user-alt"></i> About Us
</li>
<li><i class="fas fa-concierge-bell"></i> Services</li>
</ul>
</li>
<li>
<i class="fas fa-user-alt"></i> About Us
</li>
<li><i class="fas fa-concierge-bell"></i> Services</li>
<li><i class="fas fa-briefcase"></i> Portfolio</li>
<li><i class="fa fa-share" aria-hidden="true"></i> Contact</li>
</ul>
</div>

Finally got the got the problem. there was padding on the nested nav. had to add padding:0 property to css. My suggestion is to add margin:0 and padding:0 to ul, li ,a property.

Related

Menu bar is not visible when the screen is made smaller

I was trying to write the header of the page with a video as background. The navigation links are available when the screen is above 1200px. But the menu bar is not visible when the screen is made smaller than 1200px. How to fix the invisible menu bar?
This is the html code which includes the video as background and the related navigation links. The menu bars and times icons are also inserted.
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
}
nav img {
width: 140px;
position: relative;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
list-style: none;
display: inline-block;
padding: 20px 15px;
position: relative;
}
.nav-links ul li a {
color: #fff;
text-decoration: none;
font-size: 14px;
}
.header video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
nav .fa {
display: none;
}
#media (max-width: 1200px) {
.nav-links ul li {
display: block;
padding: 15px 10px;
}
.nav-links {
position: absolute;
background: #fff;
height: 100vh;
width: 200px;
top: 0;
right: -200px;
text-align: left;
z-index: 2;
transition: 1s;
}
nav .fa {
display: block;
cursor: pointer;
margin: 10px;
font-size: 24px;
}
}
<section class="header">
<video src="images/Helicopters9.mp4" muted loop autoplay></video>
<nav>
<img src="images/logo.png" alt="helicopter" />
<div class="nav-links" id="navlinks">
<i class="fa fa-times" onclick="hideMenu()"></i>
<ul>
<li>HOME</li>
<li>FAQ</li>
<li>MEMBER AREA</li>
<li>COMMUNITY GROUP</li>
<li>LATEST PLACES TO FLY</li>
<li>TRANING ONLINE</li>
<li>REVIEW</li>
</ul>
<i class="fa fa-bars" onclick="showMenu()"></i>
</div>
</nav>
</section>
Your menu is visible on smaller than 1200ox screens too. Just change the background-color of link such that white color is visible on it or change color other than white.
On smaller screen a scroll bar is created and your menu aligns to the right-most part of screen
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
}
nav img {
width: 140px;
position: relative;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
list-style: none;
display: inline-block;
padding: 20px 15px;
position: relative;
}
.nav-links ul li a {
color: red;
text-decoration: none;
font-size: 14px;
}
.header video {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
nav .fa {
display: none;
}
#media (max-width: 1200px) {
.nav-links ul li {
display: block;
padding: 15px 10px;
}
.nav-links {
position: absolute;
background: #fff;
height: 100vh;
width: 200px;
top: 0;
right: -200px;
text-align: left;
z-index: 2;
transition: 1s;
}
nav .fa {
display: block;
cursor: pointer;
margin: 10px;
font-size: 24px;
}
}
<section class="header">
<video src="images/Helicopters9.mp4" muted loop autoplay></video>
<nav>
<img src="images/logo.png" alt="helicopter" />
<div class="nav-links" id="navlinks">
<i class="fa fa-times" onclick="hideMenu()"></i>
<ul>
<li>HOME</li>
<li>FAQ</li>
<li>MEMBER AREA</li>
<li>COMMUNITY GROUP</li>
<li>LATEST PLACES TO FLY</li>
<li>TRANING ONLINE</li>
<li>REVIEW</li>
</ul>
<i class="fa fa-bars" onclick="showMenu()"></i>
</div>
</nav>
</section>

CSS Overlaps Menu Navigation Links

On media query with a max-width of 768px, the sub-menu under the About Menu overlaps to the Course Menu, I will want it pushed down on-click to display the sub-menu. I have tried a lot of options but is not working. Any help will be much appreciated.
var navLinks = document.getElementById("navLinks");
function displayMenu() {
navLinks.style.right = "0";
}
function hideMenu() {
navLinks.style.right = "-200px";
}
nav{
display: flex;
align-items: center;
padding: 2% 6%;
justify-content: space-between;
}
.nav-links{
flex: 1;
text-align: right;
}
.nav-links ul li{
display: inline-block;
padding: 8px 12px;
position: relative;
list-style: none;
}
.nav-links ul li a{
color: #000;
text-decoration: none;
font-size: 15px;
}
.nav-links ul li::after{
content: "";
width: 0%;
height: 2px;
background: #ff5733;
display: block;
margin: auto;
}
.nav-links ul li:hover::after{
width: 100%;
transition: 0.5s;
}
.nav-links ul li:nth-child(2):hover::after{
background: none;
width: 0;
}
nav .fa{
display: none;
}
nav .fa-chevron-down{
display: inline;
margin-right: 6px;
}
.sub-menu-1{
display: none;
}
.nav-links ul li:hover .sub-menu-1{
display: block;
position: absolute;
background-color: #ff5733;
margin-top: 15px;
margin-left: -15PX;
}
.nav-links ul li:hover .sub-menu-1 ul{
display: block;
margin: 10px;
}
.nav-links ul li:hover .sub-menu-1 ul li{
width: 150px;
padding: 10px;
border-bottom: 1px dotted #fff;
background: transparent;
border-radius: 0;
text-align: left;
text-transform: uppercase;
font-weight: 500;
}
.nav-links ul li:hover .sub-menu-1 ul li:last-child{
border-bottom: none;
}
.nav-links ul li:hover .sub-menu-1 ul li a:hover{
color: bisque;
}
<html>
<body>
<section>
<nav>
<!-- Navigation Links -->
<div class="nav-links" id="navLinks">
<i class="fa fa-times" onclick="hideMenu()"></i>
<ul>
<li>HOME</li>
<li><i class="fa fa-chevron-down"></i>ABOUT
<div class="sub-menu-1">
<ul>
<li>Principal Message</li>
<li>School History</li>
<li>Mission & Vision</li>
</ul>
</div>
</li>
<li>COURSE</li>
<li>NEWS</li>
<li>GALLERY</li>
<li>CONTACT</li>
</ul>
</div>
<i class="fa fa-bars" onclick="displayMenu()"></i>
</nav>
</section>
</body>
</html>
Fiddle demo
I added these two rules and it seemed to behave better.
This sets the defaults for everything (not required), so that we know what we are changing from
.nav-links ul, .nav-links ul li{
position:relative;
z-index:0;
}
This sets things to be on top when you hover them. I like to adjust z-index by at least 10 at a time, because it gives you room to insert between them at a later date if needed, without having to adjust everything else.
.nav-links ul li:hover{
z-index:10;
}

Social media bar not aligning center

Does anyone know why the icons are just aligning left? I've tried using flexboxes as well, but the icons are too far away from one another. I've also tried using inline-block as the display.
.social-media {
align-self: center !important;
}
.social-media ul {
display: flex;
align-self: center;
}
.social-media ul li {
list-style: none;
margin: 0 10px;
align-content: center !important;
}
.social-media ul li .fa {
color: #000000;
font-size: 25px;
line-height: 50px;
}
.social-menu ul li .fa:hover {
color: #ffffff;
}
.social-media ul li a {
display: block;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: white;
text-align: center;
box-shadow: 0px 7px 5px rgba(0, 0, 0, 0.5);
}
<div class="social-media">
<ul>
<li><i class="fa fa-facebook"></i></li>
<li><i class="fa fa-twitter"></i></li>
<li><i class="fa fa-instagram"></i></li>
<li><i class="fa fa-linkedin"></i></li>
</ul>
</div>
I think this should work.
.social-media {
display:flex;
justify-content:center;
}
To position all the icons to the center you can use the below css
.social-media {
align-self: center !important;
width: fit-content;
display: block;
margin: auto;
}

when I set height to auto it doesnt work on li a

I want to set height of navbar to auto so when someone resizes it height to increase or decrease depends on screen or browser resize but it doesn't work i meant when i resize the ul li a goes down of navbar
#navbar {
width:100%;
height:45px;
background: black;
box-shadow: 2px 5px rgb(252,227,0,.5);
text-align: center;
position: fixed;
top:0;
z-index: 100;
float:top;
}
#navbar ul li {
display: inline;
}
#navbar ul {
margin-top:7px;
display: inline-block;
color:rgb(252,227,0);
font-size:1.2rem;
right:0;
position: fixed;
}
#navbar ul li .a2,.a3,.a4 {
padding-left:105px;
cursor: pointer;
color:rgb(252,227,0);
font-size:1.2rem;
text-decoration: none;
transition: .5s;
}
<div id="navbar">
<ul>
<li><i class="fas fa-bars"></i>Menu</li>
<li><i class="fas fa-shopping-cart"></i>Merchandise</li>
<li><i class="fas fa-info-circle"></i>About band</li>
<li class="drop"><i class="fas fa-music"></i>Listen now...</li>
</ul>
</div>
Solution: -
If you want to set height of navbar auto...So you have to remove height and background from #navbar..
*#navbar {
height:45px;
background: black;
}*
- > Add Background and some padding on ul also set the width 100%.
#navbar ul{
background: black;
padding: 12px 10px;
width: 100%;
}
#navbar {
width: 100%;
min-height: 45px;
background: black;
box-shadow: 2px 5px rgb(252, 227, 0, .5);
text-align: center;
position: fixed;
top: 0;
z-index: 100;
}
#navbar ul li {
display: inline;
}
#navbar ul {
display: block;
margin-top: 7px;
margin-bottom: 7px;
color: rgb(252, 227, 0);
font-size: 1.2rem;
}
#navbar ul li .a2,
#navbar ul li .a3,
#navbar ul li .a4 {
display: inline-block;
padding-left: 105px;
cursor: pointer;
color: rgb(252, 227, 0);
font-size: 1.2rem;
text-decoration: none;
transition: .5s;
}
<div id="navbar">
<ul>
<li><i class="fas fa-bars"></i>Menu</li>
<li><i class="fas fa-shopping-cart"></i>Merchandise</li>
<li><i class="fas fa-info-circle"></i>About band</li>
<li class="drop"><i class="fas fa-music"></i>Listen now...</li>
</ul>
</div>

CSS - visibility: visible; The menubar is visible before the navbar collapse

* {
margin: 0;
padding: 0;
border: 0;
}
.navBar {
background-color: #2A2A2A;
min-width: 100%;
}
.wrapper {
max-width: 100%;
padding: 0 10px;
}
nav ul {
display: flex;
justify-content: space-between;
list-style-type: none;
padding-top: 15px;
}
nav ul a {
color: #7f7f7f;
text-decoration: none;
transition: color .4s ease;
font-size: 18px;
}
nav ul li ul li a{
font-size: 15px;
}
nav ul a:hover {
color: #afafaf;
}
nav li {
display: inline-block;
}
nav #menu-toggle {
display: none;
}
nav .label-toggle {
display: none;
}
nav .wrapper {
align-items: center;
display: flex;
}
#media screen and (max-width: 1366px) {
nav nav ul li ul li {
color: #7f7f7f;
padding-right: 20px;
}
nav ul {
display: block;
height: 0;
list-style-type: none;
opacity: 0;
text-align: left;
padding-left: 0;
transition: height 1s ease;
width: 50%;
visibility: hidden;
}
nav li {
color: #53354A;
display: block;
font-size: 19px;
}
nav #menu-toggle:checked ~ ul {
opacity: 1;
height: 150px;
visibility: visible;
}
nav .label-toggle {
background: linear-gradient(to bottom, #fff 0%, #fff 20%, transparent 20%, transparent 40%, #fff 40%, #fff 60%, transparent 60%, transparent 80%, #fff 80%, #fff 100%);
cursor: pointer;
display: block;
float: right;
height: 35px;
margin-top: 35px;
width: 35px;
}
nav .wrapper {
display: block;
}
}
<nav class="navBar">
<nav class="wrapper">
<img width="215" height="85" src="https://www.dsgfs.com/wp-content/uploads/2015/09/Test-Logo-250x60.png" alt="">
<input type="checkbox" id="menu-toggle" />
<label for="menu-toggle" class="label-toggle"></label>
<ul class="ulDropMenu">
<li>
Menu 1
</li>
<li>
<a href="#">Menu 2
<i class="fa fa-caret-down"></i>
</a>
<ul>
<li>
1
</li>
<li>
2
</li>
</ul>
</li>
<li>
<a href="#">Menu 3
<i class="fa fa-caret-down"></i>
</a>
<ul>
<li>
<i class="fab fa-facebook-square"></i> Facebook
<i class="fab fa-twitter-square"></i> Twitter
<i class="fab fa-instagram"></i> Instagram
</li>
</ul>
</li>
</ul>
</nav>
</nav>
The menubar is appearing outside of the navbar before the navbar collapse (see image bellow), this should not happen, this should look like this: https://codepen.io/user236945896/pen/EdaaEe
Unfortunately i can't use JavaScript on this, so i'm trying to make a collapsible menu using only CSS for some purposes.
How can i fix it?
You need to set overflow: hidden on your ul element. This will hide any child elements that overlap its parent's borders. Additionally this means you can remove the visibility: hidden and opacity: 0 from your ul and nav #menu-toggle:checked ~ ul element as all its contents will be hidden when the height is 0.
#media screen and (max-width: 1366px) {
...
nav ul {
display: block;
height: 0;
list-style-type: none;
text-align: left;
padding-left: 0;
transition: height 1s ease;
width: 50%;
overflow: hidden;
}
...
nav #menu-toggle:checked ~ ul {
height: 150px;
}
...