Encountering spacing problems among elements in the Dropdown Menu

I'm currently experiencing an issue with the Drop-Down Navigation menu that I created using HTML and CSS. When I tried to add padding between the list of UL elements, I noticed that the padding was inconsistent across the elements. For example, the student box has a 1px gap on all sides (visible when zoomed in), while the other items have varying amounts of padding.

Below, I've included snippets of both the CSS and HTML code:

* {
    margin: 0;
    padding: 0;
}

body {
    background: #34495e;
}

nav {
    width: 100%;
    height: 60px;
    background-color: rgba(49, 45, 45, 0.8);
    text-align: center;
}

nav ul {
    float: left;
}

nav ul li {
    float: left;
    list-style: none;
    position: relative;
}

nav ul li a {
    display: block;
    font-family: sans-serif;
    color: #222;
    font-size: 20px;
    padding: 18px 14px;
    text-decoration: none;
}

nav ul li ul {
    display: block;
    position: absolute;
    background-color: rgba(49, 45, 45, 0.8);
    padding: 1px;
}

nav ul li ul li a {
    padding: 8px 32px;
}

nav ul li a:hover {
    background-color: #2ecc71;
}

nav ul li ul li a:hover {
    background-color: #2ecc71;
}
<html>
    <head>
        <title>Dropdown Menu</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <nav>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="#">Registration</a>
                    <ul>
                        <li><a href="#">Student</a></li>
                        <li><a href="#">Bus</a></li>
                        <li><a href="#">Route</a></li>
                        <li><a href="#">Driver</a></li>
                    </ul>
                </li>
                <li><a href="#">Contact Us</a></li>
                <li><a href="#">About Us</a></li>
            </ul>
        </nav>
    </body>
</html>

If anyone could lend a hand, it would be greatly appreciated.

Answer №1

Remember to include the CSS rule width: 100%; for dropdown menu items. For example:

* {
    margin: 0;
    padding: 0;
}

body {
    background: #34495e;
}

nav {
    width: 100%;
    height: 60px;
    background-color: rgba(49, 45, 45, 0.8);
    text-align: center;
}

nav ul {
    float: left;
}

nav ul li {
    float: left;
    list-style: none;
    position: relative;
}

nav ul li a {
    display: block;
    font-family: sans-serif;
    color: #222;
    font-size: 20px;
    padding: 18px 14px;
    text-decoration: none;
}

nav ul li ul {
    display: block;
    position: absolute;
    background-color: rgba(49, 45, 45, 0.8);
    padding: 1px;
}
 

nav ul li ul li {
  width: 100%;
}
nav ul li ul li a {
    padding: 8px 32px;
}

nav ul li a:hover {
    background-color: #2ecc71;
}

nav ul li ul li a:hover {
    background-color: #2ecc71;
}
<html>
    <head>
        <title>Dropdown Menu</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <nav>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="#">Registration</a>
                    <ul>
                        <li><a href="#">Student</a></li>
                        <li><a href="#">Bus</a></li>
                        <li><a href="#">Route</a></li>
                        <li><a href="#">Driver</a></li>
                    </ul>
                </li>
                <li><a href="#">Contact Us</a></li>
                <li><a href="#">About Us</a></li>
            </ul>
        </nav>
    </body>
</html>

Answer №2

Adjust the styling of nav ul li ul by removing padding and setting width:100% for nav ul li ul li.

* {
    margin: 0;
    padding: 0;
}

body {
    background: #34495e;
}

nav {
    width: 100%;
    height: 60px;
    background-color: rgba(49, 45, 45, 0.8);
    text-align: center;
}

nav ul {
    float: left;
}

nav ul li {
    float: left;
    list-style: none;
    position: relative;
}

nav ul li a {
    display: block;
    font-family: sans-serif;
    color: #222;
    font-size: 20px;
    padding: 18px 14px;
    text-decoration: none;
}

nav ul li ul {
    display: block;
    position: absolute;
    background-color: rgba(49, 45, 45, 0.8);
}
nav ul li ul li {
    width:100%;
}

nav ul li ul li a {
    padding: 8px 32px;
}

nav ul li a:hover {
    background-color: #2ecc71;
}

nav ul li ul li a:hover {
    background-color: #2ecc71;
}
<html>
    <head>
        <title>Dropdown Menu</title>
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <nav>
            <ul>
                <li><a href="index.html">Home</a></li>
                <li><a href="#">Registration</a>
                    <ul>
                        <li><a href="#">Student</a></li>
                        <li><a href="#">Bus</a></li>
                        <li><a href="#">Route</a></li>
                        <li><a href="#">Driver</a></li>
                    </ul>
                </li>
                <li><a href="#">Contact Us</a></li>
                <li><a href="#">About Us</a></li>
            </ul>
        </nav>
    </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

The .wrapper CSS class is effective for adjusting the width of my bootstrap card, but it doesn't impact the

body { padding-right: 30px; padding-left: 30px; padding-bottom: 30px; background-color: #228B9D; } div.tron { padding-bottom: 40px; padding-right: 250px; padding-left: 250px; } .wrapper{ width: 1000px; margin: 0 auto; } div. ...

Struggling to vertically align with the "align-items-center" class in Bootstrap

I am a newcomer to Bootstrap and React JS, and I am facing the challenge of centering my app both vertically and horizontally. Currently, the app is only centered horizontally and seems to be attached to the top. function App() { return ( <GlobalC ...

Creating a customized file input using CSS

I am currently utilizing Bootstrap4 to craft my webpage and I am incorporating a custom file input bar. To retrieve the three labels of the input bar ("Choose file", "Browse", "Upload") from my stylesheet, I have created three custom classes with their co ...

How can you use CSS to style an image's title attribute?

I am trying to customize the text displayed when hovering over an image. Despite my efforts, the code I used doesn't seem to be working properly: <body> <img src="img.jpg" title="<span class='title'>title</span>" &g ...

I need help with customizing the progress bar in HTML and CSS

How can I create a progress bar like the one shown below: https://i.sstatic.net/BFv87.png .detail-load { height: 42px; border: 1px solid #A2B2C5; padding: 1px; border-radius: 10px; } .detail-load > .detail-loading { ...

Ways to adjust the scroll position of a div containing an overflowing div within it

In my simple HTML document, I have the code below: <div id="doublescroll_projects"> <div class="project_icons"> <div id="project" class="project_1"></div> <div id="project" class="project_2"></div> <div ...

Using Django and Python to pass text strings in views.py

Within my template, I've implemented the following code: <span class="state-txt">{{ state }}</span> This is how it's being handled in my views.py using an if/else loop: if user is not None: if user.is_active: ...

What is the best method to verify image dimensions and size for three distinct target platforms, all of which have different image dimensions but the same size, within

I'm currently working on a feature that involves an image uploader for various platforms, each with its own set of recommended image dimensions. For example: platform 1 - dimension 920 x 300, platform 2 - 210 x 200, and platform 3 - 790 x 270. When th ...

Tips for passing an array as a value in HTML/Angular

I have created a form with two controls: MovieName and MovieCast. When the form is submitted, the values are saved in the database. MovieName : String MovieCast : Array <form class="form-horizontal" role="form" name="adduserform" id="formid"> ...

The Issue with Non-Functional Links in Nivo Slider

Currently facing an issue with the Nivo Slider as it no longer links to any of the photos in the slider. Initially, I had 14 pictures linked to a post with a full embedded gallery inside. The problem arose when using a "fixed" size for the gallery, which d ...

A creative way to display a div upon hovering over a link dynamically

My website has dynamically generated a tags, each with its own corresponding div containing unique content and height. I want to make it so that when a user hovers over an a tag, the matching div is displayed. The div should appear at the position of the m ...

Tips for designing a masonry grid with Bootstrap 4

I'm attempting to achieve a specific layout using Bootstrap 4, JavaScript, CSS, and HTML. I have not been able to find something similar on Stack Overflow, but I did come across the Bootstrap 4 Cards documentation. However, I am unsure if this is the ...

Internet Explorer 11 displays a white X instead of the image

While developing a Single Page Application on Angular 1.5, I encountered a frustrating bug in IE11. Instead of rendering my pictures correctly, I see a white X in a black box. Strangely, the browser is receiving a 200 code response for the image request. C ...

The art of smashing elements using flexbox and react flawlessly

I am facing a situation where elements are organized into rows with equal heights. When one element in the row resizes, I want to flex elements in a column layout when the screen size decreases, but rearrange their order slightly. The challenge is that I ...

The embedded Google iframe is causing unnecessary padding at the bottom of the page

Below is a snippet of HTML code: <div class="google-maps"> <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d109782.8671228349!2d76.62734023564995!3d30.69830520749905!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x390fee90 ...

Fill the list items with values from the given array

I have an array called var array = ["one", "two"]; and I want to display each element in a separate list item. The desired output is: <li>Number one</li> <li>Number two</li> However, the current output is: <li>Number onetw ...

difficulty obtaining today's date

Currently, I am facing an issue with displaying the current date in an input field for the user: <input id="uploadformData" ng-model="upload.date" type="date" value="<?php echo date('Y-m-d'); ?>" /> The problem arises when the input ...

JavaScript date formatting without using jQuery

Looking for help to apply a date mask (dd/mm/yyyy) on an ASP.net textbox. I have tried several JavaScript solutions found through Google search, but none seem to work seamlessly, especially with backspacing. Any suggestions for a reliable script would be ...

javascript creating php doesn't function

Greetings! I have an option list in HTML that is being dynamically generated by PHP. My goal is to retrieve the value selected from the option list without having to submit the entire form, and then trigger another PHP function to populate another set of o ...

Tips on attaining the desired CSS impact

I'm curious about the method used by the creators of carv.ai to achieve the text masking effect seen on the paragraph "Carv connects wirelessly ...". I'm aware of how background images can be masked on text. ...