Top navigation bar is sticky but the dropdown menus are hidden

I am looking to create a horizontal navigation bar that stays fixed at the top of the page and includes dropdown menus. However, I have encountered an issue where the dropdown menus do not display when I make the navigation bar sticky.

To achieve the sticky effect, I used the following CSS3 code snippet. Are there alternative ways to achieve the same result, particularly with CSS?

.topnav {
    overflow: hidden;
    background-color: #333;
    position: fixed;         /* Making the navigation sticky */
    top: 0;                  /* Ensuring it stays at the top */
    width: 100%;
}

Below is a minimal example highlighting the problem:

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
}

/* Rest of the CSS properties for navigation and dropdowns */

<!-- HTML markup for the navigation menu and content -->

Edit:

Special thanks to Saeed and dkobando for your assistance. As someone new to web development, I have been gradually learning and building my website using various resources. Your prompt responses have motivated me to continue exploring and improving my skills in this field.

Answer №1

Try out this solution below, where I utilized a display flex and removed the overflow: hidden; from your .topnav class. Hopefully, this resolves the issue.

<div class="topnav">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown <b>V</b></button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>
</div>
<div class="main">
  <h1>Fixed Top Menu</h1>
  <h2>Scroll down to see the effect</h2>
  <h2>The navigation bar will remain at the top while scrolling</h2>
  <h3>Dropdown Menu within Navigation Bar</h3>
  <p>Hover over the "Dropdown" link to view the dropdown menu.</p>
</div>

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  background-color: #333;
  position: fixed;
  top: 0;
  width: 100%;
  display:flex;
}

.topnav a,
.dropdown,
.dropdown .dropbtn {
  font-size: 16px;
  color: white;
  text-align: center;
  text-decoration: none;
}

.topnav a,
.dropdown .dropbtn{
    padding: 14px 16px;
}
.dropdown .dropbtn {
  background-color: transparent;
  border: none;
}

.topnav a:hover,
.dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.main {
  padding: 16px;
  margin-top: 30px;
  height: 1500px;
  /* Used in this example to enable scrolling */
}

Answer №2

The reason for this behavior is that the dropdown menu is positioned absolutely, so it will not align with the fixed navigation bar. To fix this issue, you can add an extra wrapper with fixed positioning.


body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.fixed {
  position: fixed;
  top: 0;
  width: 100%;
}

.topnav a {
  float: left;
  display: block;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}
/* Other CSS code goes here */

Inside the <div class="fixed"> container, you can place your fixed navigation bar and dropdown menu to ensure they are aligned properly. By making these adjustments, you can create a smooth scrolling effect with a fixed top menu containing a dropdown menu inside the navigation bar.

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

Issue with Firefox translateZ causing only half of the content to be visible on initial load in Pure CSS Parallax configurations

Recently, I've been experimenting with creating a parallax website and have been following the helpful steps outlined by Keith Clark. However, I've run into an issue that seems to be specific to Firefox. Upon loading the site, I noticed that the ...

Is there a way to ensure that the background position remains stable and does not shift or collapse?

Still getting the hang of all this, but I'm making progress by learning on the go and enrolling in courses. Sometimes I face small obstacles like the one I'm encountering now. Everything looks great when the browser is at 100%, but when I minimi ...

How to implement a feature for uploading multiple files through a single form with unique input fields in a web

After searching on Stack Overflow, I couldn't find a suitable solution for my problem. I need help with my code that fetches data and sends it to a PHP file to upload files to specific folders and store their links in a database. However, I am encount ...

Combine three sets of columns with three additional sets of columns using the Bootstrap framework, without considering their sizes

Currently utilizing Boostrap, I have set up 2 rows with 3 columns each. Displaying the layout in the image below: https://i.sstatic.net/Ye08T.jpg The issue lies in the bottom row where the 3 columns are aligned in a single line. I aim for the 3 columns ...

Positioning a designated div directly above a designated spot on the canvas

I'm grappling with a challenge in my game where the canvas handles most of the animation, but the timer for the game resides outside the canvas in a separate div. I've been struggling to center the timer around the same focal point as the squares ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

When utilizing the HTML select element, the <option> tag may only display the initial letter of the chosen option

Here is a snippet of my PHP code: <?php if($_POST['post'] == 'Post') { $cat = $_POST['cat']; $update = "UPDATE table SET category='$cat' WHERE id = '$id' "; $result = mys ...

Creating a vertical menu from a horizontal navbar can be achieved by adjusting the CSS properties of the glyphs

I am looking to display the symbols like in scenario A for desktop screens and as in scenario B for mobile devices when the menu is expanded. (The code is identical in both scenarios, with the exception of the CSS). Scenario A: http://jsfiddle.net/bs773m8 ...

Slowly introduce a sequence of divs, then smoothly fade out the div that came before

After successfully using jQuery to create a smooth fade-in effect for a series of divs, I am now looking to incorporate a fade-out transition for the previous div. My plan is to include a fade out function as a callback within the existing fade in function ...

Basic Jquery CSS style requests

Can you help me troubleshoot this issue? var hover = $('<img />').attr('src', hovers[i]).css('position', 'absolute') I'm having trouble getting th ...

What is the best way to make the div inside the table cells expand to fill the available space?

I'm having trouble making the inner divs within table cell divs expand and fit their parent div without overlapping the next cell below it. Check out the sandbox for reference I've outlined the areas in grey where I want the cells to be filled. ...

Creating a visually appealing table in HTML or experimenting with a tableless design can greatly enhance your website's layout

Presenting data with headers in HTML has been a challenge for me. Below is the portion of the header I'm having difficulty with. Although I've successfully rotated the text, the issue I'm facing now is text overlap. Here's the code st ...

What is the best way to obtain the true dimensions of an HTML element?

Is there a way to determine the dimensions of a <div> element in order to accurately position it at the center of the browser window? Additionally, which browsers are compatible with this method? ...

Customize Your AJAX POST URL with Radio Buttons - A Step-by-Step Guide

I'm looking for some guidance on how to use AJAX to change the post URL based on a radio button selection. Do I need to use an if statement or is there another way? var barray = []; function cbutton() { $('input:radio[name="cheking"]:checke ...

What is the best method to achieve a width of 100% for an element (textarea) that has unspecified borders and padding, while also including the borders and padding

Native borders for input controls in various browsers often look more visually appealing compared to those set with CSS. However, the thickness of these native borders and padding can vary across different browsers, making it difficult to predict beforehan ...

jQuery Mobile is experiencing page height inaccuracies

My web application, built with jQuery Mobile 1.2.0, is encountering an issue with page height calculations on Windows Phone. While iOS and Android display correctly, there is a gap at the bottom of the page on Windows Phone. Is there a CSS-only solution t ...

What is the process for saving an SVG element from an HTML page as a downloadable text file?

I've been practicing creating SVG elements from scratch on an HTML page using Javascript. The texts within the SVG element are styled with typefaces fetched from the server through a "@font-face" declaration in the CSS file. It's been quite succe ...

Styling images side by side with CSS in Internet Explorer

I've encountered a CSS dilemma. I have a collection of images that I want to present side by side using an unordered list within a fixed width container. The goal is to have 3 images per row before moving on to the next set of 3 images. Each list ite ...

Navigating a Dynamic entity using a button in AFrame

I've inquired about this topic previously, but I feel my question wasn't clear. My goal is to construct a plinko-style aframe world with a ball that resets to its starting position when clicked. While I prefer to use a button to reset the ball, c ...

I want to learn how to change the background image in React based on different components

Hey there! I've been learning and came across the Tomorrowland website. I really love the background scrolling effect on the homepage, where it changes depending on the component. I'm not sure how to code it myself and could really use some assis ...