Building a Sleek Hover Effect Dropdown Menu Using HTML and CSS

I've been following a tutorial on YouTube that showcases how to create a hoverable dropdown menu using HTML and CSS. The video link can be found below:

Youtube Video Tutorial: https://www.youtube.com/watch?v=9Qrs8p7WgCc

After replicating the code and watching the tutorial multiple times, I noticed discrepancies in the output compared to the creator's version. It seems that there might be differences due to the video being from 2017, particularly noticeable when adding {display:flex} under "nav ul" in our CSS.

Output After Applying {display: flex}

https://i.stack.imgur.com/zQeyp.jpg

Final Result Shown in the Video

https://i.stack.imgur.com/WiINQ.jpg

My Output Post {display:flex}

https://i.stack.imgur.com/MbavT.jpg

My Final Results

https://i.stack.imgur.com/0LcPb.jpg

My index.html Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="menu">
        <a href="#" class="brand"><img src="#" alt=""></a>
        <nav>
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                    <ul>
                        <li><a href="#">List 1</a></li>
                        <li><a href="#">List 2</a></li>
                        <li><a href="#">List 3</a></li>
                    </ul>
                <li><a href="#">Services</a></li>
                    <ul>
                        <li><a href="#">List 1</a></li>
                        <li><a href="#">List 2</a></li>
                    </ul>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Pages</a></li>
                    <ul>
                        <li><a href="#">List 1</a></li>
                        <li><a href="#">List 2</a></li>
                        <li><a href="#">List 3</a></li>
                        <li><a href="#">List 4</a></li>
                </ul>
            <li><a href="#">Contact</a></li>
            </ul>
        </nav>
    </div>
    <section></section>
</body>
</html>

My styles.css

body {
margin: 0;
padding: 0;
}

section {
    width: 100%;
    height: 100vh;
    background: url(bg.jpg);
    background-size: cover;
}

.menu {
    position: absolute;
    width: 100%;
    background: rgba(255, 255, 255, .5);
    padding: 0 100px;
    box-sizing: border-box;
    border-bottom: 1px solid rgba(0, 0, 0, .2);
}

.menu img {
    float: left;
    width: 84px;
}

nav {
    position: relative;
    float: right;
}

nav ul {
margin: 0;
padding: 0;
display: flex;
}

nav ul li { 
    list-style: none;
}

nav ul li a {
    display: block;
    text-transform: uppercase;
    font-weight: bold;
    padding: 30px 20px;
    text-decoration: none;
}

nav ul li ul {
    display: block;
    border-bottom: 1px solid rgba(0, 0, 0, .5);
    min-width: 250px;
    position: absolute;
    margin-top: 1px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, .5);
    opacity: 0;
    visibility: hidden;
    transition: .5s;
    transform: translateY(40px);
}

nav ul li:hover ul {
    opacity: 1;
    visibility: visible;
    transform: translateY(0px);
}

nav ul li ul li a {
padding: 10px;
}

The source code provided in the tutorial should ideally yield similar results. Feel free to refer to the short video mentioned above for guidance. Thank you!

Answer №1

There is a CSS rule in place that requires a specific nested relationship: nav > ul > li > ul

nav ul li:hover ul {
    opacity: 1;
    visibility: visible;
    transform: translateY(0px);
}

However, the HTML structure does not match this relationship:

<ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <ul>
        <li><a href="#">List 1</a></li>
        <li><a href="#">List 2</a></li>
        <li><a href="#">List 3</a></li>
    </ul>

The nested <ul> appears as a sibling of the <li>, rather than a child. To fix this issue, consider using the next sibling selector +:

nav ul li:hover + ul {
    opacity: 1;
    visibility: visible;
    transform: translateY(0px);
}

Answer №2

Upon reviewing the video, it appears that there are several issues in your code. Specifically, there seems to be a problem with how you wrapped the list items and applied CSS styles.

body {
  margin: 0;
  padding: 0;
}

section {
  width: 100%;
  height: 100vh;
  background: url(bg.jpg);
  background-size: cover;
}

.menu {
  position: absolute;
  width: 100%;
  background: rgba(255, 255, 255, 0.5);
  padding: 0 100px;
  box-sizing: border-box;
  border-bottom: 1px solid rgba(0, 0, 0, 0.2);
}

.menu img {
  float: left;
  width: 84px;
}

nav {
  position: relative;
  float: right;
}

nav ul {
  margin: 0;
  padding: 0;
  display: flex;
}

nav ul li {
  list-style: none;
}

nav ul li a {
  display: block;
  text-transform: uppercase;
  font-weight: bold;
  padding: 33px 20px;
  text-decoration: none;
}

nav ul li a:hover {
  background-color: #184771;
  color: #fff;
}

nav ul li ul {
  display: block;
  background: rgba(255, 255, 255, 0.5);
  min-width: 250px;
  position: absolute;
  margin-top: 1px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
  transition: 0.5s;
  opacity: 0;
  visibility: hidden;
}

nav ul li:hover ul {
  opacity: 1;
  visibility: visible;
  transform: translateY(0px);
}

nav ul li ul li a {
  padding: 10px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="test.css" />
  </head>
  <body>
    <div class="menu">
      <a href="#" class="brand"><img src="#" alt="" /></a>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li>
            <a href="#">About</a>
            <ul>
              <li><a href="#">List 1</a></li>
              <li><a href="#">List 2</a></li>
              <li><a href="#">List 3</a></li>
            </ul>
          </li>
          <li>
            <a href="#">Services</a>
            <ul>
              <li><a href="#">List 1</a></li>
              <li><a href="#">List 2</a></li>
            </ul>
          </li>
          <li><a href="#">Portfolio</a></li>
          <li>
            <a href="#">Pages</a>
            <ul>
              <li><a href="#">List 1</a></li>
              <li><a href="#">List 2</a></li>
              <li><a href="#">List 3</a></li>
              <li><a href="#">List 4</a></li>
            </ul>
          </li>
          <li><a href="#">Contact</a></li>
        </ul>
      </nav>
    </div>
    <section></section>
  </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a sticky section with Tailwind CSS utility classes

I have a page in my Next.js web app, and I want to make the section highlighted in blue (wrapped in <aside> tag) sticky so that it stays in place while scrolling, with only the main section containing the chart scrolling. The code snippet below is f ...

Sort the images before uploading them

Hey everyone, I'm having an issue with the sortable feature. I am currently using AJAX to temporarily save images and display them without uploading. Now, I would like to be able to sort those images and have a button to save the sorted version. The ...

Creating a dynamic nested list in HTML using JavaScript with data retrieved from a JSON source

I'm trying to generate a dynamic nested ul\li list from a JSON array. IMPORTANT! While I can use jQuery for this transformation, it's in a node.js environment where DOM access is restricted and I must work with a string. The depth of the a ...

Upon selecting the hamburger menu, the menu pops up; however, the homepage text is visible overlapping the menu

I'm in the process of developing a responsive portfolio page using ReactJS and Tailwind CSS. When the screen size is smaller than the medium breakpoint, a hamburger menu icon appears. However, when I click on the hamburger icon to view the menu items, ...

The ngbDatepicker within the Bootstrap angular framework is designed to seamlessly integrate with ngbPanelContent without causing overflow issues

I've integrated the ngbDatepicker into a form as shown below <ngb-panel> <ng-template ngbPanelTitle> <div class="row"> <ui-switch (change)="onChange($event,2)" [checked]="profession ...

a non-breaking space within a hyperlink

While working on the subnavigation of a website, I encountered an issue with the link Suite «Mont Blanc», which was pulled from the backend. The desired format for this link was:       Suite «Mont Blanc» However, t ...

Utilize data retrieved from an API to customize the appearance of buttons through the combination of React and Sass styling techniques

I retrieved data from an API and used it to create a list of buttons. The data I received includes names and color codes. { name: "bob", colorCode: "#DC7472" }, { name: "ben", colorCode: "#69DCD1" }, { name: &q ...

Unchecking random checkboxes within a div using jQuery after checking them all

Once a link is clicked on, all checkboxes within that particular div will be checked. function initSelectAll() { $("form").find("a.selectAll").click(function() { var cb = $(this).closest("div").find("input[type=checkbox]"); cb.not(":checked" ...

What are the specific constraints for CSS within web components?

<html> <style> body { color: blue; } </style> <body> <h1>Styles!</h1> <p>someone created a very general selector</p> <isolated-stuff></isolated-stuff> </body> <s ...

Creating an image rollover effect when hovering between two images

I am currently working with React and trying to change 2 images by hovering over them. Here is what I have accomplished so far: Card.js <div className="image-wrapper"> <img src={sprites.front_default} className="image" a ...

Trouble with the display:none attribute in Firefox and Chrome

<tr style="height:5px" id="TRRSHeaderTrialBar" name="TRRSHeaderTrialBar" style='display:none'> <tr id="TREmail" name="TREmail" style="height:1px;" nowrap style='display:none'> Using the code snippet above to hide the bar w ...

React - Obtain User Login Details and Verify

I am working on a React project that includes a Login Form. The code has been organized into small components for reusability, but I am unsure of how to retrieve and validate user credentials (username and password). Is there a method available to validate ...

Angular form array inputs

Currently delving into the world of Angular.js, I have encountered a simple problem that has left me baffled. My objective is to generate form inputs with the value of "connectedTeams" in HTML: <input type="text" name="connectedTeam[]"> <input ...

How can you achieve the effect of "hovering over an image to automatically start playing a muted video within the image itself"?

[![enter image description here][1]][1]I am working with WordPress and Elementor, and I want to create a hover effect where an image triggers a muted video to play within the image area on mouseover. The video should stop playing when the mouse is not hove ...

Tips for embedding HTML content onto a clipboard for easy pasting into Gmail

Our application generates two URLs that users often want to copy and paste into Gmail. To make these links more visually appealing than the lengthy URLs, we implemented HTML code. We included a feature that places the HTML on the Windows clipboard for easy ...

Calculate the total sum of values in a MySQL column and then group the results

I'm currently working on a quiz website and looking to create a leaderboard. The user scores are stored in a database table named 'user_record' with the following structure: 'user_id' - varchar(254) -holds the user id for a score ...

Tips for scrolling a div that has too much content within a webpage

Is there a method to manipulate the scrollbar within a div on a webpage? Specifically, I am attempting to automate scrolling up and down on an Instagram post like . However, since the scrollbar may be hidden using CSS properties, detecting it can be challe ...

tips on locating the next immediate downloadable cell in a table

My table includes cells with colspan and rowspan attributes. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <table border=1 id="mytable" > <tr><td>A1</td> <td> A2 < ...

Is it possible to combine the output of a loop using Liquid?

My goal is to display something like: data-tags="[tag1, tag2, tag3]" but I'm currently seeing data-tags:[tag1tag2tag3]. Is my usage of join incorrect? Here is the code: data-tags="{% for tag in subtask.tags %}{{tag.title | slugify | join ', &ap ...

Embedding CSS stylesheets in a Django template

I'm currently working on a large Django website and in the process of adding the HTML templates. Here is the file tree for the main part: C:. +---forums | | admin.py etc | | | +---migrations | | | ... | | | +---templa ...