Navigation menu items hover with padded spacing, disregarding any margins

Coming up with an interesting title is always a challenge, but here goes. I want my navigation bar items to be evenly spaced regardless of the font type, which might vary in size slightly. I've added a :hover feature that darkens the background behind the items. It works well, but when I try to add margins or padding to the left and right of nav ul for indentation, the darkened area doesn't cover the margin/padding space.

https://i.sstatic.net/UmjSY.png

The first image shows how it looks with no 'nav ul' padding. The second one is with padding where the area gets cutoff, and in the third image, I attempted to center the 'home' text more (not very elegantly though, just a rough mspaint edit).

Additionally, is there a way to make the padding for 'header nav ul li a {' align perfectly so there's no extra space in between? I have added a few px of space between where the hover effect occurs to account for changes in font type without disrupting the even spacing.

I'm a total beginner at CSS and HTML, so please provide your most gentle and beginner-friendly advice if you're willing to help me out.

JSFiddle: https://jsfiddle.net/c1y9axqt/

Relevant code:

CSS

.container {
    width: 960px;
    padding: 0 10px;
    margin: 0 auto;
}

.nav_menu {
    background-color: #005073;
    background-image: -webkit-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3)); 
    background-image: -o-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3));
    background-image: -moz-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3));
    background-image: linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3));
    -webkit-box-shadow: inset 0px 0px 2px 1px rgba(0,0,0,0.4);
    -moz-box-shadow: inset 0px 0px 2px 1px rgba(0,0,0,0.4);  
    box-shadow: inset 0px 0px 2px 1x rgba(0,0,0,0.4)
    border-style: solid;
    border-width: 1px;
    border-color: black;
    height: 26px;
}



nav ul {
    margin: 0;
    padding: 0;
    display: flex;
    justify-content:space-between;
}

nav ul li{
    display: inline;
}

header nav ul li a {
    display: inline;
    padding: 6px 30px 6px 30px;
    letter-spacing: 1px;
    text-decoration: none;
    font-weight: bold;
    line-height: 26px;
    color: #EBEAEA;
    text-shadow:
    -1px -1px 1px rgba(0, 0, 0, .6),
    1px -1px 1px rgba(0, 0, 0, .6),
    -1px 1px 1px rgba(0, 0, 0, .6),
    1px 1px 1px rgba(0, 0, 0, .6); 

}


header nav ul li a:hover {
    color: #E1E0E0;
    background: rgba(0,0,0,0.2);
}

header nav ul li a:active {
    -webkit-box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    -moz-box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    color: #CECCCC;
   background: rgba(0,0,0,0.3);
}

HTML

<header>
    <div class="container clearfix">           
        <div class="nav_menu">
            <nav>
                <ul>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Website Design</a></li>           
                    <li><a href="#">Art &amp; Poetry</a></li>  
                    <li><a href="#">Blog &amp; Other</a></li>
                    <li><a href="#">Music &amp; More</a></li>
                    <li><a href="#">Shop</a></li>  
                </ul>
            </nav>
        </div> <!-- end of container-->
        </div> <!-- end of navigator menu bar--> 
 </header>

JSFiddle (again lol): https://jsfiddle.net/c1y9axqt/

Answer №1

Looks like you were after this, I made some changes to the CSS only.

.container {
    width: 960px;
    padding: 0 10px;
    margin: 0 auto;
}   

.nav_menu {
    background-color: #005073;
    background-image: -webkit-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3)); /* For Safari 5.1 to 6.0 */
    background-image: -o-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3)); /* For Opera 11.1 to 12.0 */
    background-image: -moz-linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3)); /* For Firefox 3.6 to 15 */
    background-image: linear-gradient(rgba(0,0,0,.3), rgba(125,125,125,.3)); /* Standard syntax */
    -webkit-box-shadow: inset 0px 0px 2px 1px rgba(0,0,0,0.4); /* For Firefox 3.6 to 15 */
    -moz-box-shadow: inset 0px 0px 2px 1px rgba(0,0,0,0.4);  /* For Firefox 3.6 to 15 */
    box-shadow: inset 0px 0px 2px 1x rgba(0,0,0,0.4); /* Standard syntax */
    border-style: solid;
    border-width: 1px;
    border-color: black;
    height: 26px;
}

header nav ul li:hover {
    color: #E1E0E0;
    background: rgba(0,0,0,0.2);
}

nav ul {
    margin: 0;
    padding: 0;
    display: flex;
    justify-content:space-between;
}

nav ul li{
    display: inline;
    width:160px;
    text-align:center;
}

header nav ul li a {
    display: inline;
    letter-spacing: 1px;
    text-decoration: none;
    font-weight: bold;
    line-height: 26px;
    color: #EBEAEA;
    text-shadow:
    -1px -1px 1px rgba(0, 0, 0, .6),
    1px -1px 1px rgba(0, 0, 0, .6),
    -1px 1px 1px rgba(0, 0, 0, .6),
    1px 1px 1px rgba(0, 0, 0, .6); 
}

header nav ul li a:hover {
    color: #E1E0E0;
    background: rgba(0,0,0,0.2);
}

header nav ul li a:active {
    -webkit-box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    -moz-box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    box-shadow: 0 0 5px rgba(0,0,0,0.7) inset;
    color: #CECCCC;
    background: rgba(0,0,0,0.3);
}

I hope this modification is what you had in mind!

Answer №2

Changes have been made to the styling to simplify it and keep it in blocks. Setting widths on list items in the navigation using % will evenly size them and applying text-align: center; adds to the aesthetic appeal. The background color of anchor tags has also been set for easier color changes when hovering.

Consider looking into Media Queries to make the navigation responsive for smaller screens.

I hope this information is helpful,

Happy Coding!

Check out the Fiddle here

HTML:

<div id="navContainer"> 
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Website Design</a></li>           
            <li><a href="#">Art &amp; Poetry</a></li>  
            <li><a href="#">Blog &amp; Other</a></li>
            <li><a href="#">Music &amp; More</a></li>
            <li><a href="#">Shop</a></li>  
        </ul>
    </nav>
</div>

CSS:

body {
margin: 0px 0px;
}

#navContainer {
    width: 100%;
}

#navContainer nav {
    width: 960px;
    margin-left: auto;
    margin-right: auto;
}

#navContainer ul {
    list-style: none;
    list-style-type: none;
    padding: 0px 0px;
    margin: 0px 0px;
}

#navContainer li {
    float: left;
    width: 16.666667%;
    text-align: center;
}

#navContainer a {
    text-decoration: none;
    display: block;
    background-color: #005073;
    line-height: 26px;
    /* more CSS properties */
}

#navContainer a:hover {
    color: #E1E0E0;
    /* more CSS properties */
}

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

What is the best way to display the information contained within a .nfo file on a website?

Does anyone know of a plugin or code that can display the content of a .nfo file on a webpage (html or php)? I want to create multiple web pages with individual .nfo files, but I'm not sure if this is achievable through coding or if there's a scr ...

Can anyone explain to me why I am unable to retrieve the pricing information from the Nike website using selenium and Python?

I've been working on a Python script that scrapes the prices of trainers from the Nike website and saves them into a CSV file. Initially, I tried extracting the price data from the HTML element, but that didn't work. I then switched to using CSS ...

Troubleshooting: Scrollspy Bootstrap 4 functionality not functioning properly on Codepen

I’ve been experimenting with the Scrollspy feature in Bootstrap 4 for my Portfolio Page, but I’m facing some issues getting it to work properly on Codepen. After doing some research on StackOverflow, I came across a working example on Codeply, here’ ...

Eliminate any attributes related to images (such as alt text, classes, or styles) from the image tag

I operate a website where individuals are able to directly copy and paste content into my platform, which is then stored in my database. Initially, I only accounted for users utilizing my image insertion tool, which saves images as basic HTML image tags: ...

PHP not displaying Bootstrap input validation message set by JS

Currently, I am attempting to implement error messages using .setCustomValidity() and the .invalid-feedback class on an HTML form utilizing Bootstrap 4. Essentially, when the user validates the form, a JavaScript script assesses the inputs and if any erro ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...

What could be causing the width of my aside not to display as I intended?

I've successfully used Flex multiple times in the past, but for some reason, I'm encountering an issue now. I isolated the problematic code in a separate HTML file, yet the problem persists. In my layout, I have a container div with an aside and ...

Show off markdown formatting without the need for v-html

I run a unique type of platform that allows users to publish their own blog posts using a WYSIWYG editor, and then share them on the site. My top priority is to ensure security against XSS attacks, which is why I am looking for alternatives to using v-htm ...

Exploring the differences between SASS and Bootstrap: the battle of mixins versus @extend

Currently utilizing the SASS version of Bootstrap found here. Curious as to whether there are distinctions between using the pre-set mixins versus utilizing SASS's @extend. For example, considering the following: <div class="wrapper"> Some ...

What is the best way to incorporate tags into this type of statement?

I'm having trouble getting this code to work {% for student in students %} {{<th>student.Last_name</th>}} {{<th>student.First_name</th>}} {{<th>student.Email</th>}} {{<th>student.Contact</th>}} {{<t ...

CSS Troubleshoot: Unable to Change Background of Current Menu Page Item

I've been attempting to add a small graphic using CSS to indicate the current page the viewer is on, but for some reason, it's not highlighting properly. Below is my code: HTML: <ul class="side-nav"> <a href="http://www.cieloazulsant ...

When you hover over the image, the text will disappear

When I hover over the image of LTC, a thumbnail appears with text and a button. However, I do not want the text "LTC" to be shown when hovering over the image. <div class="col-md-4"> <div class="view view-eighth"> <div class=" ...

Positioning Images at the Top of the Screen in React Native

I have two images that I would like to display side by side at the top of the screen. <View style={styles.container}> <View style={styles.topWrapper}> <Image source={TOP_START_GRAPHIC} style={styles.topStartImage} /> ...

What is the best way to retrieve all label values of DOM elements from an HTML file using C#?

My goal is to open an HTML file, scan all the DOM elements with labels, translate those label values into another language, and save the modified file in a different location. ...

What is causing my images to exceed the boundaries of their parent columns?

Currently, I am in the process of developing a web application using asp.net. The layout of the site is being designed with Bootstrap version 4. However, I am facing an issue where the image overlaps the "Welcome to the site" title on the left-hand side wh ...

Creating an animated diagonal line using TweenMax/EaselJS can add a dynamic element to your designs

I am looking to create a constellation by connecting stars with animated lines. I have attached an image to show the desired end result. The stars and lines are generated using HTML5 canvas and easelJS. Below is the code snippet I have been working on, wh ...

Creating a form that can identify both letters and numbers using JavaScript

Is it possible to create an <input> field that can recognize both letters and numbers while disregarding spaces and capitalization? Here is my current progress, aiming for the feedback to display as "right" when 9 H 6 U 8 f is entered in the field, ...

The incompatibility of proxy objects and Object.create explained

Whenever I attempt the code below, I consistently receive a "RangeError: Maximum call stack size exceeded" error. However, if I simply access the receiver without including console.log(receiver);, everything works fine. This situation is very confusing. c ...

What is the procedure for incorporating custom fonts from Google Fonts into Tailwind CSS?

https://i.sstatic.net/99gpe.pngI tried following the steps from a YouTube tutorial, but I am unable to apply the font family "nunito." I inserted the @import code into the CSS file located in the "src" folder and executed "npm run (script name)" in the t ...

What is causing my website to refresh before I can save the edits I have made after clicking on the edit button

My website is fairly simple, allowing users to add blog posts with a title, author, and content. Each post has two buttons - one for deleting and one for editing. These actions add, delete, or edit the chosen posts within a .json file through a local json ...