A mysterious problem encountered when designing a drop-down menu using HTML and CSS

Before you proceed: please be mindful of the fact that I am a novice in this area and seeking some practice. Kindly highlight my errors only if they are pertinent to the discussion or genuinely beneficial.

I've extensively reviewed my code and scoured stackoverflow for answers, but none seem to address my specific issue. My goal is to implement a dropdown menu (where hovering over a button reveals multiple links in a vertical arrangement below it). However, I've encountered difficulty as the links appear horizontally instead of vertically upon hover. The confusion stems from the fact that despite using id's in my CSS, the navbar classes seem to override them. Any assistance would be greatly appreciated. Below is the relevant code snippet (yes, Django is being utilized):

<style>
#dropdown {display: none;position: relative;background-color: #dde000; padding: 0px;z-index: 1; left: 970px; top:45px;}
#drop:hover #dropdown {display:block;}

div.navbar {border: 2px outset gray; position: relative; margin-left:-21px; height:45px; background: #dddddd; margin-top:-6px; left: 15px;}
.navbar a {height: 23px; display:inline; text-decoration: none; color: black; padding: 10px; float: left; background: #dddddd; padding-top:12px;}
.navbar a:hover {background: #eeeeee; transition: all 0.4s;}
</style>

<div class="navbar">
    <a href="{% url 'blog_main:index' %}">Home</a>
    <a href="{% url 'blog_main:create_post' %}">Create Post</a>
    {% if user.is_authenticated %}
    <a href="{% url 'blog_main:profile' user.id %}">My Profile</a>
    {% else %}
    <a href="{% url 'blog_main:login' %}">My Profile</a>
    {% endif %}
    {% if not user.is_authenticated %}
    <a href="{% url 'blog_main:login' %}" style="float:right; margin-right:50px;">Login</a>
    <a href="{% url 'blog_main:register' %}" style="float:right; ">Register</a>
    {% else %}
                
    <a href="{% url 'blog_main:logout' %}" style="float:right; margin-right:50px;">Logout</a>
    <div id="drop">
    <a style="float:right; margin-right: 50px;" >{{ user.username }}</a>
    <div id="dropdown">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
    </div>
    </div>
    {% endif %}
</div>

Edit: Would dedicating a few weeks to learning JavaScript be the most effective solution for this issue?

Answer №1

You currently have two issues to address:

The horizontal links are popping up on hover because the a tag is being floated to the right without fixing the positioning of the #dropdown element, causing it to be left behind.

To display the submenus below properly, there are various methods available, with one common approach being to use display: flex. The revised code snippet would look something like this:

<style>
#dropdown {display: none;background-color: #dde000; padding: 0px;z-index: 1; left: 970px; top:45px;}
#drop {float: right; padding: 10px}
#drop:hover #dropdown {display:flex; ;
  flex-direction: column;}

div.navbar {border: 2px outset gray; position: relative; margin-left:-21px; height:45px; background: #dddddd; margin-top:-6px; left: 15px;}
.navbar a {height: 23px; display:inline; text-decoration: none; color: black; padding: 10px; float: left; background: #dddddd; padding-top:12px;}
.navbar a:hover {background: #eeeeee; transition: all 0.4s;}
</style>

<div class="navbar">
    <a href="{% url 'blog_main:index' %}">Home</a>
    <a href="{% url 'blog_main:create_post' %}">Create Post</a>
    {% if user.is_authenticated %}
    <a href="{% url 'blog_main:profile' user.id %}">My Profile</a>
    {% else %}
    <a href="{% url 'blog_main:login' %}">My Profile</a>
    {% endif %}
    {% if not user.is_authenticated %}
    <a href="{% url 'blog_main:login' %}" style="float:right; margin-right:50px;">Login</a>
    <a href="{% url 'blog_main:register' %}" style="float:right; ">Register</a>
    {% else %}
                
    <a href="{% url 'blog_main:logout' %}" style="float:right; margin-right:50px;">Logout</a>
    <div id="drop">
    <a style="margin-right: 50px;" >{{ user.username }}</a>
    <div id="dropdown">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
    </div>
    </div>
    {% endif %}
</div>

For more information on CSS flexbox, you can visit this resource.

Edited Code

<style>
.navbar{display:flex;
justify-content: flex-end;
   width: 100%;
}
.navbar > a, .navbar> div{ 
align-content: flex-end
}
#dropdown {display: none;background-color: #dde000; padding: 0px;z-index: 1;}
#drop{display:flex; ;
  flex-direction: column;
}
#drop:hover #dropdown {display:flex;
  flex-direction: column; z-index: 1;  margin-top: 8px
}
div.navbar {border: 2px outset gray; height:45px; background: #dddddd}
.navbar a {height: 23px; text-decoration: none; color: black; padding: 10px;  background: #dddddd; padding-top:12px;}
.navbar a:hover , .navbar div:hover{background: #eeeeee; transition: all 0.4s;}
</style>

<div class="navbar">
    <a href="{% url 'blog_main:index' %}">Home</a>
    <a href="{% url 'blog_main:create_post' %}">Create Post</a>
    {% if user.is_authenticated %}
    <a href="{% url 'blog_main:profile' user.id %}">My Profile</a>
    {% else %}
    <a href="{% url 'blog_main:login' %}">My Profile</a>
    {% endif %}
    {% if not user.is_authenticated %}
    <a href="{% url 'blog_main:login' %}">Login</a>
    <a href="{% url 'blog_main:register' %}">Register</a>
    {% else %}
                
    <a href="{% url 'blog_main:logout' %}">Logout</a>
    <div id="drop">
    <a style="margin-right: 8px;" >{{ user.username }}</a>
    <div id="dropdown">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
    </div>
    </div>
    {% endif %}
</div>

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

Is it possible to align an entire column to the right in the Material UI Data Grid?

I'm currently exploring Material UI and grappling with a specific challenge. Is there a method within Material UI to create a Data Grid with two columns, one left-aligned and the other right-aligned? I've managed to align the headers as desired, ...

What is the best way to contain my content within a bordered box?

As a beginner in HTML and CSS, I have successfully created a basic website with a simple menu at the top that includes links to different pages like "Home," "About," and "Contact." The website will mainly feature information and images, but I believe it wo ...

Loading Images in Advance with jQuery, Native JavaScript, and CSS

After successfully implementing a method to hover on a small image and load an additional image to the right side of the page, I am now looking to enhance user experience by preloading images. Is there a way to preload an image using JQuery, allowing it t ...

Error encountered during celerybeat initialization due to a problem with unpickling

My experience with supervisord running celery for my django project has been smooth until now. Suddenly, I encountered an issue where celerybeat refuses to start and throws the following traceback: Traceback (most recent call last): File "[...]celery/ap ...

I encounter an issue where I am unable to apply the identical CSS style to two instances of the "row-fluid" element

This is my unique HTML and CSS code. HTML <section id="content"> <article id="areaRegister"> <div class="row-fluid"> <div class="span6"> <?php include 'formulario.php'; ?> ...

Updating the state of a Vuex store does not automatically trigger an update to a computed property in Vue3

I am currently working on an ecommerce website built with Vue.js 3 and Django, but I am facing some issues with the cart functionality. My goal is to update the number of items in the cart without reloading the entire page, just by updating the cart compon ...

Assigning numerical ratings to a web-based questionnaire in HTML

In my questionnaire, I have radio buttons and checkboxes that need to be graded. The format of the input elements looks like this: <input type="radio" name="1" value="Yes" onclick="document.getElementById('pls').setAttribute('requi ...

What is the best way to trigger a jQuery animation when a section becomes visible using Fullpage.js?

Within my website, I've incorporated Fullpage.js and am interested in triggering jQuery animate functions as users scroll to specific sections. Although I tested methods like mouseenter, mouseover, and mouseleave from the prior section, they did not d ...

Improving the layout of the title page in R Markdown ioslides

I am looking to customize the appearance of the initial title page that appears when converting my Rmd file to html ioslides. The current header in my test Rmd file looks like this: --- title: This test subtitle: that test author: jake ...

Prevent one individual cell in a table from dictating the overall table width

A B A B A X A B In a contained block with a fixed width of 400px, there is a table. When the browser width drops below 421px, the width of the containing block changes to 95%. The cells labeled "A" and "B" contain simple text. There is one cel ...

Using jQuery, upon the page loading, the script will automatically trigger a

I am attempting to create an HTML file that automatically logs into my bank account. Below is the code I currently have: <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> ...

What is the best way to define the maximum width for responsive images in Bootstrap 4?

Query: How can I control the max-width of an image in Bootstrap 4 without losing its responsiveness? Context: Bootstrap 4's .img-fluid class ensures images remain responsive. The image is set to scale with the parent element by utilizing ma ...

String representation of opacity value shown instead of numerical value

I recently implemented an opacity slider using React hooks in my project. Here is a snippet of the code: https://i.sstatic.net/m0JtM.png Within my table, I set the opacity like this: <td> <img src="link" opacity={data.socialOpaci ...

When accessing my custom Heroku domain, I encounter a 400 error (Bad Request), but everything runs smoothly on foo.herokuapp.com

After successfully deploying a Django project to Heroku, I encountered an issue. The project works perfectly fine at , but when I try to access it using , I receive a 400 error: Bad Request. Despite setting up the CNAME correctly, I was notified during th ...

Issue with Bootstrap: Navbar elements shifting when page width is decreased

I am facing an issue with my two navbars, one above the other. The top bar is thinner and consists of two navigation items aligned to the right of the page. Despite setting it not to collapse when the page width is reduced, the items suddenly disappear whe ...

The small device screen in Bootstrap 4.5 is experiencing an issue where two rows are overlapping

I recently started learning Bootstrap and encountered an issue. When resizing the browser screen to a smaller size, I noticed that the rows within separate containers were overlapping. Screenshots have been provided for reference. Can someone please guide ...

Menu dropdown feature for mobile site

Recently, I was working on a website and encountered an issue with the dropdown menu. Despite making the layout responsive and adding a media query for mobile devices, the dropdown menu would activate the first link automatically on mobile, giving users ba ...

Utilizing the Query Results from get_queryset Method for Context Data in Django ListView CBVs

Is there a way to pass a Tag object to a template without making an additional database query in the get_context_data method? Can you suggest a more elegant solution for retrieving the value from the get_queryset method and is it acceptable to declare cu ...

What is the best way to horizontally align three animated progress bars alongside images?

Check out this jsfiddle that shows a vertical alignment of the progress bars. How can I modify it to align them horizontally and centered? https://jsfiddle.net/bLe0jLar/ .wrapper { width: 100px; height: 100px; } .wrapper > #bar, #bar2, #bar3 { ...

The AngularJS request.POST method is returning an empty QueryDict: {}

Currently, I have an AngularJS code snippet where I am making a POST request: 'use strict'; angular.module('app').controller('RegisterController', function($scope, $http) { $scope.login = [] $scope.registrar ...