I am having problems aligning the dropdown/submenu under parent menu. To be clear, I want my submenu to show when I hover over Services. I will post the code and also attach a photo to be clear.
Edit: I have updated the snippet! I think it is alright right now. Thank you!
body {
font-family: 'Poppins', sans-serif;
background-color: -webkit-linear-gradient(to right, #2a5298, #1e3c72);
background-color: linear-gradient(to right, #2a5298, #1e3c72);
background-color: #1e3c72;
display: flex;
flex-direction: column;
min-height: 100vh;
}
button {
font-family: 'Poppins', sans-serif;
}
header {
display: flex;
width: 100%;
margin: auto;
align-items: center;
}
nav {
flex: 1;
height: 10vh;
}
.nav-links ul {
position: absolute;
/* display: none; */
}
.nav-links li a {
color: white;
font-weight: 400;
text-decoration: none;
display: block;
padding: 15px;
font-size: 18px;
}
.nav-links li {
transition: all .6s ease-out;
border-radius: 40px;
height: 70%;
display: flex;
justify-content: center;
align-items: center;
padding: 10px 18px;
}
.nav-links li:hover {
background-color: white;
border-radius: 40px;
height: 60%;
display: flex;
justify-content: center;
align-items: center;
color: #1e3c72;
-webkit-transition: all .6s ease;
transition: all .6s ease;
padding: 10px 18px;
}
.nav-links li:hover a {
color: #1e3c72;
}
.nav-links img {
height: 30px;
width: 30px;
filter: invert(100%);
-webkit-filter: invert(100%);
filter: brightness(0) invert(1);
cursor: pointer;
}
.nav-links li i {
padding: 0 10px;
}
.nav-links {
display: flex;
justify-content: space-around;
list-style: none;
align-items: center;
width: 50%;
height: 100%;
margin-left: auto;
}
<nav>
<ul class="nav-links">
<li><a class="nav-link" href="index.html" id="active">Home</a></li>
<li><a class="nav-link" href="#">Services</a>
<ul>
<li>Quality Management</li>
<li>Project Management</li>
<li>Auditing</li>
</ul>
</li>
<li><a class="nav-link" href="#">About</a></li>
<li><a class="nav-link" href="#">Contact</a></li>
</ul>
</nav>
On limited resource of what you shared, I assume this is what you need?
* {
margin: 0;
padding: 0;
}
nav {
height: 30px;
}
nav ul {
display: block;
position: relative;
z-index: 100;
}
nav ul li {
float: left;
list-style: none;
margin: 0;
padding: 0;
}
nav ul li ul {
display: none;
}
nav ul li a {
width: 150px;
height: 30px;
display: block;
text-decoration: none;
text-align: center;
line-height: 30px;
background-color: black;
color: white;
}
nav ul li a:hover {
background-color: red;
}
nav ul li:hover ul {
position: absolute;
top: 30px;
display: block;
width: 100px;
}
nav ul li:hover ul li {
display: block;
}
<nav>
<ul class="nav-links">
<li><a class="nav-link" href="index.html" id="active">Home</a></li>
<li><a class="nav-link" href="#">Services</a>
<ul>
<li>Quality Management</li>
<li>Project Management</li>
<li>Auditing</li>
</ul>
</li>
</ul>
</nav>
Related
the animation dosent work for some reason ( im using normalize dont know if it have an effect + a default start )
i copied the code but it didnt work for me
its second project im rly a real beginner so any advise is welcome
thanks in advance^^
*/ html */
<nav>
<a href="./index.html"
><img src="./eduford_img/logo.png" alt="Uni logo"
/></a>
<div class="nav-links">
<ul>
<li>Home</li>
<li>About</li>
<li>Course</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</div>
</nav>
*/ css */
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
align-items: center;
}
nav img {
width: 150px;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
display: inline-block;
position: relative;
padding: 8px 12px;
}
.nav-links ul li a {
color: #fff;
font-size: 16px;
text-decoration: none;
}
.nav-links ul li a ::after {
content: "";
width: 0%;
height: 2px;
background: var(--primary-400);
display: block;
margin: auto;
transition: var(--transition);
}
.nav-links ul li:hover::after {
width: 100%;
}
Since you put the ::after on the <a> element, you need .nav-links ul li:hover a::after, not .nav-links ul li:hover::after:
*/ html */
<nav>
<a href="./index.html"
><img src="./eduford_img/logo.png" alt="Uni logo"
/></a>
<div class="nav-links">
<ul>
<li>Home</li>
<li>About</li>
<li>Course</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</div>
</nav>
*/ css */
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
align-items: center;
}
nav img {
width: 150px;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
display: inline-block;
position: relative;
padding: 8px 12px;
}
.nav-links ul li a {
color: #fff;
font-size: 16px;
text-decoration: none;
}
.nav-links ul li a::after {
content: "";
width: 0%;
height: 2px;
background: var(--primary-400);
display: block;
margin: auto;
transition: var(--transition);
}
.nav-links ul li:hover a::after {
width: 100%;
}
An example with a few modifications so that it works on this site:
body {
background: black
}
nav {
display: flex;
padding: 2% 6%;
justify-content: space-between;
align-items: center;
}
nav img {
width: 150px;
}
.nav-links {
flex: 1;
text-align: right;
}
.nav-links ul li {
display: inline-block;
position: relative;
padding: 8px 12px;
}
.nav-links ul li a {
color: #fff;
font-size: 16px;
text-decoration: none;
}
.nav-links ul li a::after {
content: "";
width: 0%;
height: 2px;
background: red;
display: block;
margin: auto;
transition: width 0.5s;
}
.nav-links ul li:hover a::after {
width: 100%;
}
```
<nav>
<a href="./index.html"
><img src="./eduford_img/logo.png" alt="Uni logo"
/></a>
<div class="nav-links">
<ul>
<li>Home</li>
<li>About</li>
<li>Course</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</div>
</nav>
ul {
padding: 0;
margin: 0;
text-decoration: 0;
list-style: none;
background: #343434;
height: 20 px;
}
ul li a.home {
float: left;
color: white;
font-size: 24px;
line-height: 0px;
padding: 20px 100px;
margin: 0px 30px;
}
ul li a.home:hover {
background-color: white;
color: black;
transition: .5s;
}
ul {
list-style: none;
color: white;
}
ul li {
display: inline-block;
line-height: 80px;
}
.main ul {
display: none;
}
ul li a {
font-size: 18px;
color: white;
padding: 12px 100px;
border-radius: 2px;
}
a:hover {
text-decoration: none;
background: #A179C9;
color: black;
transition: .7s;
}
.main li:hover>ul {
display: block;
position: absolute;
}
<ul class="main">
<li><a class="home" href="index.html">blank</a></li>
<li>listen
<ul>
<li><a id="spotify" href="https://open.spotify.com/">spotify</a></li>
<li><a id="apple" href="https://open.spotify.com/">apple music</a></li>
</ul>
</li>
<li>gallery</li>
<li>download</li>
<li>store</li>
</ul>
The "spotify" and "apple music" block elements display in one line next to each other as opposed to below each other.
I also use bootstrap on the page. Not sure if it's got anything to do with the issue because when I remove the script it still works the same.
Sorry if my code is hard to see through.
It's the inline-block on ul li in your CSS. inline-block won't break the line. See MDN Inline Elements.
in css I marked my edits.
was it necessary?
ul {
padding: 0;
margin: 0;
text-decoration: 0;
list-style: none;
background: #343434;
height: 20 px;
}
ul li a.home {
float: left;
color: white;
font-size: 24px;
line-height: 0px;
padding: 20px 100px;
margin: 0px 30px;
}
ul li a.home:hover {
background-color: white;
color: black;
transition: .5s;
}
ul {
list-style: none;
color: white;
}
ul li {
display: inline-block;
line-height: 80px;
}
.main ul {
display: none;
flex-direction: column; /*add this it*/
position: absolute; /*add this it*/
}
ul li a {
font-size: 18px;
color: white;
padding: 12px 100px;
border-radius: 2px;
}
a:hover {
text-decoration: none;
background: #A179C9;
color: black;
transition: .7s;
}
.main li:hover>ul {
display: flex; /*add this it*/
position: absolute; /*add this it*/
}
<ul class="main">
<li><a class="home" href="index.html">blank</a></li>
<li>listen
<ul class="sub_main">
<li><a id="spotify" href="https://open.spotify.com/">spotify</a></li>
<li><a id="apple" href="https://open.spotify.com/">apple music</a></li>
</ul>
</li>
<li>gallery</li>
<li>download</li>
<li>store</li>
</ul>
I work on a new website, and now I am styling the navbar
I have created a dropdown menu with hover but the menu does not take the list's height as I think so when I hover to the next list in the dropdown menu, the menu disappears.
* {
box-sizing: border-box
}
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
}
nav {
display: grid;
grid-template-columns: 5rem 1fr;
justify-items: center;
align-items: center;
background-color: #000000;
}
.logo h1 {
color: #FFF;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 5px;
}
.menu {
display: block;
position: relative;
}
.menu li {
color: #d3d3d3;
font-weight: 300;
text-transform: uppercase;
list-style: none;
display: inline-block;
}
.menu ul {
width: 100%;
height: 0;
opacity: 0;
position: absolute;
left: 0;
top: 35px;
background: #00f;
transition: all .5s ease;
}
.menu li:nth-of-type(4) {
height: 150%;
}
.menu>li>a {
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #d3d3d3;
}
.menu ul li {
display: block;
}
.menu li:hover ul {
height: 150%;
opacity: 1;
transform: translateY(0);
overflow: hidden;
}
<nav>
<div class="logo">
<!-- Logo -->
<h1>Hexa</h1>
</div>
<ul class="menu">
<!-- Nav Menu -->
<li class="active">Home</li>
<li>About</li>
<li>Projects</li>
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
I tried to change the parent list height and the menu inside it but still now working, do you have any idea about this?
Thank you all for help :D
I just added the z-index property to the menu and it is fixed
I work on a new website, and now I am styling the navbar
I have created a dropdown menu with hover but the menu does not take the list's height as I think so when I hover to the next list in the dropdown menu, the menu disappears.
* {
box-sizing: border-box
}
body,
html {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
}
nav {
display: grid;
grid-template-columns: 5rem 1fr;
justify-items: center;
align-items: center;
background-color: #000000;
}
.logo h1 {
color: #FFF;
font-weight: 400;
text-transform: uppercase;
letter-spacing: 5px;
}
.menu {
display: block;
position: relative;
}
.menu li {
color: #d3d3d3;
font-weight: 300;
text-transform: uppercase;
list-style: none;
display: inline-block;
}
.menu ul {
width: 100%;
height: 0;
opacity: 0;
position: absolute;
left: 0;
top: 35px;
background: #00f;
transition: all .5s ease;
}
.menu li:nth-of-type(4) {
height: 150%;
}
.menu>li>a {
display: block;
padding: 10px 20px;
text-align: center;
text-decoration: none;
color: #d3d3d3;
}
.menu ul li {
display: block;
}
.menu li:nth-child(4):hover ul{
height: 150%;
padding: 15px 0px 15px 0px;
opacity: 1;
overflow: hidden;
}
.menu li:nth-child(4) ul{
width: 130px;
padding: 0px;
height: 0%;
opacity: 1;
transform: translateX(250px);
overflow: hidden;
}
.menu li:nth-child(4) ul li{
padding: 0px 12px 0px 12px;
}
.menu li:nth-child(4) ul li a{
color: #fff;
text-decoration: none;
}
<nav>
<div class="logo">
<!-- Logo -->
<h1>Hexa</h1>
</div>
<ul class="menu">
<!-- Nav Menu -->
<li class="active">Home</li>
<li>About</li>
<li>Projects</li>
<li>
Parent Link
<ul>
<li>Child Link</li>
<li>Child Link</li>
<li>Child Link</li>
</ul>
</li>
<li>Blog</li>
<li>Contact</li>
</ul>
</nav>
Please can someone help, I've been at this for 2 days and just can't get it right! I'm trying to create a dropdown menu to adhere to my existing navbar. Here's my code below. I have it set that the navbar style changes for smaller windows/mobile and still need to figure that part out wrt the sub menu.. HELP :(
nav {
position: fixed;
width: 100%;
z-index: 10;
}
nav ul {
list-style: none;
background: rgba(0, 0, 0, 0);
overflow: hidden;
color: #000;
padding: 0;
text-align: center;
margin: 0;
transition: 1s;
}
nav ul li {
display: inline-block;
padding-left: 20px;
padding-right: 20px;
padding-top: 5px;
padding-bottom: 5px;
}
nav ul li a {
text-decoration: none;
color: #000;
font-size: 20px;
padding: 5px 5px;
}
nav ul li ul {
display: none;
}
nav ul li:hover ul {
display: block;
position: absolute;
}
nav.black ul {
background: rgba(255, 255, 255, 1);
color: #666;
}
nav.black ul li a {
text-decoration: none;
font-size: 20px;
transition: 1s;
filter: invert(50%);
}
.menu-icon {
width: 100%;
background: #000;
text-align: right;
box-sizing: border-box;
padding: 15px 24px;
cursor: pointer;
color: #00b4ff;
display: none;
}
#media(max-width: 900px) {
.nav-logo {
position: fixed;
top: 0;
margin-top: 16px;
}
nav ul {
max-height: 0px;
background: #000;
}
nav.black ul {
background: #000;
}
.showing {
max-height: 45em;
}
nav ul li {
box-sizing: border-box;
width: 100%;
padding: 24px 0;
text-align: center;
}
.menu-icon {
display: block;
}
}
<div class="nav-wrapper">
<nav>
<div class="menu-icon">
<i class=" fa fa-bars fa-2x"></i>
</div>
<div class="menu">
<ul>
<li>Home</li>
<li>About</li>
<li>Courses
<ul>
<li>PADI Experiences</li>
<li>PADI Basic Courses</li>
<li>PADI Speciality Courses</li>
<li>PADI Pro</li>
</ul>
</li>
<li class="small-nav-logo"><a id="nav-africa" href ="index.html"><img src="img/logo-icon.gif" alt="Home" width="80" height="68"></a></li>
<li>Dives
<ul>
<li>Guided Packages</li>
</ul>
</li>
<li>Nature</li>
<li>Contact</li>
</ul>
</div>
</nav>
</div>
Add this to your css.
nav ul li ul li{
display: block;
text-align: left;
margin: 20px 0px;
}
All you needed to do was target the li inside the li.
How can i center the float element that you can see in this photo? I want to bring it from the left of the web page to the middle, but keeping it at the top of the page?
Here is the code of the HTML:
<html>
<head>
<title>Batch File Generator | Home</title>
</head>
<link href="style.css" rel="stylesheet" >
<ul>
<li><a>Home</a></li>
<li><a>Download</a>
<ul>
<li>32-bit version</li>
<li>64-bit version</li>
</ul>
</li>
<li><a>Discussion</a>
<ul>
<li><a>Community forums</li></a>
<li><a>Ask the developers</li></a>
</ul>
</li>
<li><a>News</a></li>
</ul>
</html>
And here is the code of the CSS:
body{
background: url("background.jpg") no-repeat;
background-size: cover;
font-family: Arial;
color: white;
}
ul{
margin: 0px;
padding: 0px;
list-style: none;
}
ul li {
float: left;
width: 200px;
height: 40px;
background-color: black;
opacity: .8;
line-height: 40px;
text-align: center;
font-size: 20px;
}
ul li a {
text-decoration: none;
color: white;
display: block;
}
ul li a:hover {
background-color: green;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
For HTML Code, use this:
<div class="wrapper">
<div class="middle-content">
<ul>
<li><a>Home</a></li>
<li><a>Download</a>
<ul>
<li>32-bit version</li>
<li>64-bit version</li>
</ul>
</li>
<li><a>Discussion</a>
<ul>
<li><a>Community forums</a></li>
<li><a>Ask the developers</a></li>
</ul>
</li>
<li><a>News</a></li>
</ul>
</div>
</div>
For css code:
html{
height: 100%;
}
body{
background: url("background.jpg") no-repeat;
background-size: cover;
font-family: Arial;
color: white;
height: 100%;
}
.wrapper{
display: table;
width: 100%;
height: 100%;
}
.middle-content{
display: table-cell;
vertical-align:middle;
width: 100%;
}
ul{
margin: 0px;
padding: 0px;
list-style: none;
}
ul li {
float: left;
width: 200px;
height: 40px;
background-color: black;
opacity: .8;
line-height: 40px;
text-align: center;
font-size: 20px;
}
ul li a {
text-decoration: none;
color: white;
display: block;
}
ul li a:hover {
background-color: green;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
Solution with flexbox.
* {
box-sizing: border-box;
}
html,
body,
ul {
padding: 0;
margin: 0;
}
nav {
width: 100%;
background-color: darkgray;
}
li {
list-style: none;
padding: 15px;
border: thin solid lightgray;
position: relative;
background-color: white;
}
li>a {
display: inline-block;
text-decoration: none;
}
.main-menu {
display: flex;
justify-content: center;
}
.sub-menu {
display: none;
position: absolute;
left: 0px;
width: 100%;
margin-top: 15px;
}
.main-menu li:hover {
background-color: lightgreen;
}
.sub-menu li:hover {
background-color: lightblue;
}
.main-menu li:hover>.sub-menu {
display: block;
}
<nav>
<ul class="main-menu">
<li><a>Home</a></li>
<li><a>Download</a>
<ul class="sub-menu">
<li>32-bit version</li>
<li>64-bit version</li>
</ul>
</li>
<li><a>Discussion</a>
<ul class="sub-menu">
<li>Community forums</li>
<li>Ask the developers</li>
</ul>
</li>
<li><a>News</a></li>
</ul>
</nav>