Struggling to incorporate an .active state style into a navigation bar, even though the :hover effect is working perfectly fine

Looking at the image, it's clear that the active state (link 1) in jQuery has some styling issues. I want it to match the a:hover state seen at link three.

Check out the links here:

navigation bar image

I've tried everything to make the .active state text line up with the other links but nothing seems to work. Any suggestions on how I can achieve this?

Links HTML:

<div class="col-12">
              <div class="resources-links make-active" id="resources-nav">
                        <a id="tab1" class="tab active">link 1</a>
                        <a id="tab2" class="tab">link 2</a>
                        <a id="tab3" class="tab">link 3</a>
                        <a id="tab4" class="tab">link 4</a>
                        <a id="tab5" class="tab">link 5</a>
                        <a id="tab6" class="tab">link 6</a>
                        <a href="javascript:void(0);" class="icon" onclick="myFunction()">
                        <i id="resources-nav-chevron" class="fa fa-chevron-down"></i>
                      </a>
                </div>
             </div>

CSS:

.use-case-nav-bk {    
    background-color: #F9FAFA;
}
/*
#use-cases-nav .resources-nav {
    height: 71px;
}
*/    

#use-cases-nav .resources-links {
    overflow: hidden;
    background-color: #F9FAFA;
    width: 96%;
    padding-left: 40px;
    margin: 0 auto;
}

#use-cases-nav .resources-links a {
    float: left;
    display: block;
    color: #9b9999;
    text-align: center;
    padding: 28px 18px;
    text-decoration: none;
    font-family: Gilroy-Extrabold, sans-serif;
    font-size: 16px;
}

#use-cases-nav .resources-links a:hover {
    background-color: #B5B5B5;
    color: #fff;
    border-radius: 10px;
    height: 10px;
    padding-top: 8px;
    padding-bottom: 42px;
    margin-top:20px;
}

.resources-links .active  {   
    color: #2359B9 !important;
    background-color: #B5B5B5;    
    border-radius: 10px;
    height: 10px;
    padding-top: 8px;
    padding-bottom: 42px;
    margin-top:20px;
}

#use-cases-nav .resources-links .icon {
    display: none;
}

Jquery:

$(document).ready(function(){
    $('.make-active a').click(function(event){
        $('.active').removeClass('active');
        $(this).addClass('active');
        event.preventDefault();
    });
});

JSFIddle: https://jsfiddle.net/webbtrad/ek93L5rn/1/

Answer №1

The main issue lies in the specificity of the selector used for the .active class.

Currently, you have:

.resources-links .active 

However, this is targeting all the a tags within:

#use-cases-nav .resources-links a

This selector has higher specificity due to the leading #use-cases-nav, causing the padding properties to not be overridden properly.

On the other hand, the padding property does override correctly for the a:hover selector because it has higher specificity:

#use-cases-nav .resources-links a:hover

Therefore, even though the CSS rules are identical, they do not match up as expected.

To resolve this, simply add #use-cases-nav into your .active selector like so:

#use-cases-nav .resources-links .active 

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

Unexpected Placement of CSS Grid Items

I am currently facing an issue with aligning items in a nested grid using grid-column/grid-row assignment. After setting the template with grid-template-rows:/grid-template-columns, I expected the $50 and Give Now items to appear on row 3, with one in colu ...

What are the common jQuery functions used to toggle both display and visibility simultaneously?

After incorporating the default Bootstrap theme into my mobile site, I discovered that jQuery is struggling with a seemingly simple task: displaying elements marked with the hidden class. It's hard to believe, but it's true. The tricky and troub ...

Enhancing progress bar appearance with CSS styling

Is there a way to style a progress bar using CSS to make it look like the image below? View expected design I have already attempted to achieve this but ended up with a different result. See what I have done so far progress { border: 1.5px solid si ...

Using JQuery and Ajax, what is the best way to transfer information from a popup form to a PHP page?

I've noticed that my question has been asked a few times already, but I'm still struggling to get my code working. It seems like I'm using the same code as those solutions, so I can't figure out what's going wrong. Currently, I am ...

Having trouble displaying font within a Stylus-defined class using @font-face in Chrome

I am currently running a MEAN application with Angular2 on the frontend and using Stylus-based CSS for styling. After compiling, the resources are organized in the following folder structure: dist backend frontend resources font.woff font.wo ...

Maintaining text in a straight line

My design includes an image floated to the left with text aligned to the right of the image. Unfortunately, the text is too long, causing a line from a paragraph to drop below the image. How can I ensure that the text stays in line with the paragraph and ...

The active navigation element is not displaying the CSS property when using PHP

Apologies for the vague title of this question, I found it difficult to express my issue in just one statement. So, here goes. Currently, I am enhancing my Personal Website and aiming to highlight the navigation element for the current page with a specifi ...

Is there a way to retrieve the value of a dropped file in a file input using jQuery?

Can the data from a dropped file be set in a file upload input field? Or does a dropped file need to be instantly uploaded to the server? Here is an example code snippet: <input type="file" id="drop-box" /> $("#drop-box").on('drop', fun ...

Is it possible to alter the appearance of my menu when clicked on?

Is there a way to update the appearance of an active menu or submenu that is selected by the user? I would like the chosen submenu and its parent menu to have a distinct style once clicked (similar to a hover effect, but permanent). /*jQuery time*/ $(do ...

Is there a way to position my image and text side by side?

My current objective revolves around implementing a specific design, as illustrated in this image. The issue I'm encountering in my code pertains to the utilization of the bootstrap grid system for ease of layout. However, when I incorporate both tex ...

How can you determine the distance of an SVG path?

I've been experimenting with Scrollmagic and I'm interested in implementing a similar effect to the one showcased here: To test it out, I created an svg with a squiggle path and now I need to insert the length of the path into stroke-dasharray: ...

Determine which submit button was clicked during the validation process

Currently, I am in the process of developing a new custom validator for MVC4. I have experience with this task and have successfully completed it several times before. However, this time around, I am faced with a new challenge - I need to determine which s ...

Creating a dynamic website with user-friendly editing capabilities

Having a good understanding of html5 and css3, I am currently exploring ways to create a website that enables visitors to edit content in real time for all other users to see. While aware of the contenteditable attribute, I have found that it does not reta ...

Having trouble loading a CSS file in CodeIgniter?

I've added a link to my header.php and despite trying various solutions from stackoverflow, it still isn't working for me. Can someone please help? Thank you in advance :) Here's my folder structure: Main Folder: ci_attl - application ...

Calculating the height of the browser menubar, toolbar, navigation bar, and bookmarks can be easily achieved using jQuery or JavaScript

How can I use jQuery to calculate the height of the browser and adjust the position of another div element accordingly? What steps do I need to take to achieve this functionality? ...

Express.js route not being properly handled in post request

This is the structure of my folders: https://i.sstatic.net/9BvbH.png I am attempting to create a python script that receives input from an HTML file and displays its output in the HTML. Below is the jQuery script in my HTML: <script src="https:/ ...

What is the correct way to invoke a function stored within a key?

My journey with Javascript began earlier this year, so my knowledge is quite limited at the moment. a.click(function(){a.setup()}),{key:"setup", value:function(){ /code/ }} I am trying to figure out how to call the function "setup," but I'm unsure o ...

What could be causing the position method to not provide the anticipated results?

Why is the position method not returning [left: 100, top: 10] in this particular scenario? <!DOCTYPE HTML> <html> <head> <style> #parent1 { width: 300px; height: 200px; border: solid 9px pink; } #child1 { position: ...

"Enhancing user experience with jQuery UI tabs for selecting the best option

Does anyone know how to create jquery ui tabs within a selected option on a mobile version? I am having trouble with this and would appreciate any assistance. My design is responsive, with desktop versions using jquery tabs and the mobile version using sel ...

Animation showing on the progress bar is not correctly halted

I am encountering a problem with handling an animation of a progress bar in my code. The issue arises when I pause the animation, as it seems to slightly backtrack instead of pausing at the correct position upon clicking a button. Can someone help identify ...