The link background color is malfunctioning

I'm having trouble getting the background color to show for the active link class. The border changes to dashed, but the color remains the same. Can anyone help me figure out what's going on? I'm new to CSS and feeling a bit frustrated. Also, I know the text color isn't working either; I just set it to white for testing purposes. I definitely don't want white text on a white background.

nav{
    margin:0 auto;
    width: 75%;

}

nav a {
    text-align: center;
    display: block;
    width: 20%;
    float: left;
    margin: 0px 0px 0px 30px;
    padding: 20px 0px;
    border: 2px solid black;
}

a:link {
    font-family: sans-serif;
    background-color: rgba(221, 221, 221, 1);
    color: rgba(86, 19, 139, 1);
}

a:visited{
    background-color: rgba(221, 221, 221, 1);
    color: rgba(171, 171, 171, 1);
}

.active{
    background-color: white;
    color: white;
    border: 5px dashed black;
}
<header>
<h1>Ulimate Frisbee History</h1>
<nav>
<a href="index.html">Home</a>
<a href="teams.html">Teams</a>
<a href="history.html" class = "active">History</a>
<a href="http://www.usaultimate.org/index.html" target="_blank">USA Ultimate</a>
</nav>
</header>

Answer №1

Pointed out by @OvidiuUnguru, the background and color properties remain unaffected due to the principle of CSS selector priority, also known as Specificity. According to this principle, the most specific rule will be applied last, and in this case, a:link holds more specificity than .active.

Although you could utilize !important, the priority hierarchy still takes precedence unless there is only one !important rule... However, I recommend avoiding this approach as it may lead to unintended consequences in the future. The best practices of using !important are covered in detail on MDN:

Some guidelines to follow:

Always explore alternatives that involve specificity before resorting to !important

Use !important solely for page-specific CSS that needs to override external CSS (from libraries like Bootstrap or

Avoid using !important for plugins or mashups.

Avoid using !important for global site-wide CSS.

Moreover, it is unlikely that you desire all elements with the class .active to have the same styles as the links; you may want different styling for the active section of the page, active buttons, etc. Therefore, it is advisable to be more specific. In the given code snippet, I simply prefixed a to your selector to make it more specific: a.active.

nav {
  margin: 0 auto;
  width: 75%;
}

nav a {
  text-align: center;
  display: block;
  width: 20%;
  float: left;
  margin: 0px 0px 0px 30px;
  padding: 20px 0px;
  border: 2px solid black;
}

a:link {
  font-family: sans-serif;
  background-color: rgba(221, 221, 221, 1);
  color: rgba(86, 19, 139, 1);
}

a:visited {
  background-color: rgba(221, 221, 221, 1);
  color: rgba(171, 171, 171, 1);
}

a.active {
  background-color: white !important;
  color: white !important;
  border: 5px dashed black;
}
<header>
  <h1>Ultimate Frisbee History</h1>
  <nav>
    <a href="index.html">Home</a>
    <a href="teams.html">Teams</a>
    <a href="history.html" class="active">History</a>
    <a href="http://www.usaultimate.org/index.html" target="_blank">USA Ultimate</a>
  </nav>
</header>

Answer №2

One explanation for the absence of a white background on the "active" class is due to priority. The CSS pseudo-classes a:visited and a:link take precedence over the .active class.

To address this, you can implement the following CSS:

nav{
    margin:0 auto;
    width: 75%;

}

nav a {
    text-align: center;
    display: block;
    width: 20%;
    float: left;
    margin: 0px 0px 0px 30px;
    padding: 20px 0px;
    border: 2px solid black;
}

a:link {
    font-family: sans-serif;
    background-color: rgba(221, 221, 221, 1);
    color: rgba(86, 19, 139, 1);
}

a:visited{
    background-color: rgba(221, 221, 221, 1);
    color: rgba(171, 171, 171, 1);
}

.active{
    background-color: white !important;
    color: white !important;
    border: 5px dashed black;
}
<header>
<h1>Ulimate Frisbee History</h1>
<nav>
<a href="index.html">Home</a>
<a href="teams.html">Teams</a>
<a href="history.html" class = "active">History</a>
<a href="http://www.usaultimate.org/index.html" target="_blank">USA Ultimate</a>
</nav>
</header>

However, the necessity of using :link and :visited may not be apparent.

Answer №3

nav{
    margin:0 auto;
    width: 75%;

}

nav a {
    text-align: center;
    display: block;
    width: 20%;
    float: left;
    margin: 0px 0px 0px 30px;
    padding: 20px 0px;
    border: 2px solid black;
}

a:link {
    font-family: sans-serif;
    background-color: rgba(221, 221, 221, 1);
    color: rgba(86, 19, 139, 1);
}

a:visited{
    background-color: rgba(221, 221, 221, 1);
    color: rgba(171, 171, 171, 1);
}

.active{
    background-color: white !important;
    color: white;
    border: 5px dashed black;
}
<header>
<h1>Ultimate Frisbee History</h1>
<nav>
<a href="index.html">Home</a>
<a href="teams.html">Teams</a>
<a href="history.html" class = "active">History</a>
<a href="http://www.usaultimate.org/index.html" target="_blank">USA Ultimate</a>
</nav>
</header>

Answer №4

Give this a try for some helpful tips.

To adjust the "font color," include the following:

.selected {
    color: blue !important;
    border: 3px solid black;
}

If you'd like to modify the "background color," use this code:

.selected {
    background-color: yellow !important;
    border: 3px solid black;
}

For changing both the "font and background color," utilize the following:

.selected {
    color: green !important;
    background-color: yellow !important;
    border: 3px solid black;
}

Answer №5

Similar outcomes to those shared by others without the need for using !important

nav{
    margin:0 auto;
    width: 75%;

}

nav a {
    text-align: center;
    display: block;
    width: 20%;
    float: left;
    margin: 0px 0px 0px 30px;
    padding: 20px 0px;
    border: 2px solid black;
}

a:link {
    font-family: sans-serif;
    background-color: rgba(221, 221, 221, 1);
    color: rgba(86, 19, 139, 1);
}

a:visited{
    background-color: rgba(221, 221, 221, 1);
    color: rgba(171, 171, 171, 1);
}

/* new colors working!! */
a.active, a.active:link, a.active:visited {
    background-color: green;
    color: white;
    border: 5px dashed black;
}
<header>
    <h1>Ulimate Frisbee History</h1>
    <nav>
        <a href="index.html">Home</a>
        <a href="teams.html">Teams</a>
        <a href="history.html" class = "active">History</a>
        <a href="http://www.usaultimate.org/index.html" target="_blank">USA Ultimate</a>
    </nav>
</header>

Alternatively, you can opt for a:link:not(.active) and a:visited:not(.active) for the first two blocks

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 split <hr> tags into different sections?

I'm striving to create a design similar to this - https://i.sstatic.net/hcaST.png Initially, I attempted to use a hr tag and modify it for my purposes. However, my efforts have not yielded the desired outcome. Any assistance in understanding this co ...

Incorporating various vertical divs with 100% height and width

Struggling to achieve the creation of multiple divs with 100% width and height? Here is an example: <html> <body> <div id="wrapper"> <div id="1">100% height & width, all you can see when opening this file!</div&g ...

Tips for creating a captivating full-screen parallax section on your website

Is there a way to make the first section of a parallax site full screen? I've been scouring the internet for a solution to this problem, but I'm not having any luck. Almost every parallax site I come across has their first section full screen, a ...

Using JavaScript values retrieved from a database to dynamically adjust the options in the second dropdown menu based on the selection made in the

I am currently working on a feature that involves populating two dropdown menus with values from a database. The idea is that when an option is selected in the first dropdown, the second dropdown should dynamically display relevant values based on that s ...

Can JavaScript be Utilized to Create Dynamic Layouts Successfully?

Are you curious about the viability of using Javascript to create fluid website layouts? While it is possible, how does it compare in terms of performance and complexity when compared to other methods like CSS3/HTML5? Take a look at the function below that ...

Issues encountered with the delete function using distinct row identification format

I am encountering an issue with variable value in input field. To provide some context, I have client information stored in a MySQL database. Using PHP, I retrieve this data and display it in a table. Currently, I am working on implementing a delete optio ...

Looking to create a div in HTML with two columns and two rows for display

Query: I am facing issues adjusting these grids using HTML and CSS. I have attempted to place them in a single frame within a separate div, but it's not working as expected. I need help aligning the grids according to the provided image below. Can som ...

What happens when there is a 1-second delay in loading CSS on an HTML page?

Lately, I've been working on creating HTML and CSS pages. However, whenever I load a page, I notice that there's always a brief half-second flash of the HTML content without any styling applied. Does anyone have an idea why this is happening and ...

Ensure that the data prop in Vue is always updated whenever there is a change in

In my Vue.js project, I am creating a data property called w to store the clientWidth of a specific element in my HTML. In the mounted hook, I am initializing it like this: data() { return { w: 0, }; }, mounted() { this.w = this.$refs.timelineProgres ...

What can be done to resolve the slowdown caused by an Ajax request within the function?

I've implemented drag and drop functionality for saving images, but I'm facing an issue with getting an instant preview of the saved images. Whenever I try to send the image to the server using an ajax request, the image doesn't appear on th ...

Showcase your skills in CSS, HTML, and Javascript

I attempted to search for something similar but came up empty-handed. I have two buttons. When I click one of them, I want to hide a certain division, but it's not working as expected. The divs d2 and d3 are initially hidden when the page opens, whic ...

Using JavaScript to create temporary drawings on a webpage that automatically erase themselves

I am curious about how to achieve a scribble effect that self-erases, similar to what is seen on this website: Example: Thank you! I have come across some similar scripts, but I am struggling to understand how to make the scribble disappear after a few ...

Modify how various classes are shown when hovering over a pseudo class

This is my customized scss code .image-container { position: relative; .delete-image { display: none; position: absolute; top: 0px; right: 0px; } .make-primary { display: none; position: absolute; ...

Guide on including the sequential images within the previous and next button of a carousel

Currently working on my portfolio website, I have managed to code a carousel for displaying my images. The only challenge I am facing now is how to make the 'Next' and 'Prev' buttons show the next and previous images respectively. Does ...

Using postMessage to communicate with the localstorage of an iframe from a separate domain

I have a question regarding the challenges of using localStorage for performance reasons and any potential workarounds. From what I gather, once a reference to localStorage is detected on a page (at compile time?), it blocks the thread to read data from di ...

jQuery Files in Conflict

The issue I am facing involves the code below. The first function requires linked js and css, while the second one needs a jQuery and javascript file inherited from base.html. However, there seems to be a conflict in loading these files (possibly because o ...

What is the best way to validate adjusted tags within the html editor of Visual Studio 2012?

The html editor in Visual Studio 2012 keeps giving me warnings for tag names that are not recognized in the detected or configured html schema. Since I am using AngularJs by Google, this is quite inconvenient as it relies on the ability to enter custom ta ...

Achieve full width with flexbox column wrap while minimizing the height

How can I arrange elements in columns within a container with a fixed width and variable height, when the number of elements is unknown? My challenge is that I do not know the maximum width of the child elements, preventing me from setting specific column ...

Using jQuery to implement multiple FadeIn effects

Here is the code that I have written: $('.frame').each(function(){ $(this).click(function(e){ var id = $(this).attr('id'); e.preventDefault(); $('.active').removeClass('active').fadeOut(8 ...

Struggling to make rCarousel function properly due to navigation width and autoscroll issues

I'm having some trouble figuring out what's going wrong with my rCarousel setup. Specifically, I can't seem to get the top part labeled "vrienden van het koksgilde" to work properly. Despite following the instructions on , I'm still no ...