Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Navigation drop down menu stuck to the left of links

Josh47391

New Coder
Trying to make a drop down on a nav like this:

kdvC8.png



Here's the HTML and CSS - it just stays to the left of the nav links

HTML:
<header>
        <img src="logo.png" alt="" class="logo">
        <nav class="right">
            <ul class="nav-links">
                <li><a href="#">Home</a></li>
                <li><a href="#">Food</a></li>
                  <ul>
                     *4 <li> items*
                  </ul>
                <li><a href="#">Housing</a></li>
                   <ul>
                     *4 <li> items*
                   </ul>
                <li><a href="#">Cleaning</a></li>
                   <ul>
                     *4 <li> items*
                   </ul>
                <li><a href="#">Decor</a></li>
                   <ul>
                     *4 <li> items*
                   </ul>
            </ul>
        </nav>
 </header>


CSS:
header, ul, a{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #6B8E23;
    list-style: none;
    text-decoration: none;
    color: white;
    padding-right: 50px;
    font-weight: 600;
}


header{
    display: flex;
    justify-content: space-between;
    align-items: center;
}


.right a{
    padding: 0 20px 0 20px;
    border-right: 2px solid white;
    font-size: large;
}

.right > a:last-of-type{
    border-right: none;
}

.nav.links a:hover{
    background-color: #000;
}

.nav-links ul {
    display: none;
    position: absolute;
}

.nav-links:hover > ul{
    display: block;
    border: none;
}

Here’s what it’s doing:

ps1mb.png


Whenever I hover over the links (housing, cleaning etc), the drop-down sticks to the left of the navigation links, and doesn't actually "drop down" below the original headings. How can this be fixed?
 
Josh, I'm giving you a start. Go read up on the various subjects I introduce and try to finish things.
In your HTML I closed your </li> in the sub-menu and moved the end tags that encompass the dropdowns I added the headings to the html - look at that.
In the CSS I added something you should put in all css - the wildcard padding/margin/box-sizing section.

CSS
Code:
*{
    margin: 0;
    padding: 0;
    box-sizing: content-box;
}
header, ul{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #6B8E23;
    padding-right: 50px;
    font-weight: 600;
}
li{
    padding: 20px 20px;
    border-right: 2px solid white;
    font-size: large;
    list-style: none;
    background-color: #000;
}
li:last-of-type{
    border-right: none;
}
li:hover{
    cursor: pointer;
    background-color: red;
}
a{
    color:white;
    text-decoration: none;
}
li ul li{
    background-color: pink;
}
li ul {
    display: none;
    position: absolute;
    top:60px;
    background-color: pink;
}
li:hover ul{
    display: block;
}


HTML
Code:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>website</title>
</head>

<body>
<header>
    <img src="logo.png" alt="MISSING" class="logo">
    <nav class="right">
        <ul class="nav-links">
            <li><a href="#">Home</a></li>
            <li><a href="#">Food</a>
              <ul>
                 <li> items
              </ul>
            </li>
            <li><a href="#">Housing</a>
               <ul>
                  <li> items</li>
               </ul>
            </li>
            <li><a href="#">Cleaning</a>
               <ul>
                 <li> items</li>
               </ul>
            </li>
            <li><a href="#">Decor</a>
               <ul>
                  <li> items</li>
                  <li> items</li>
                  <li> items</li>
                  <li> items</li>
               </ul>
            </li>
        </ul>
    </nav>
</header>
</body>
</html>
 
In line 18 of the above HTML, the <li> is not being closed. It's not a problem, browsers accept it and the W3C Online validator does not consider it an error. I find that strange but this is HTML for you. Nonetheless it is wrong and should be corrected - nowhere it says that you may leave this tag unterminated.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom