Two distinctive link styles housed within a single container in CSS

Creating two different link styles for a div container is my current goal. I want individual links in the text to be underlined, but not the ones in the list - they should only underline when hovered over. However, it seems that the second styling declaration is being ignored.

<div class="news">
  <h2>Lorem ipsum</h2>
  <p>At vero eos et accusam et justo duo dolores et ea rebum.</p>
  <p><a href="xxx" target="blank"></a></p> <!-- **underline** -->
  <ol>
    <li><a class="news_links" href="xxx" target="blank"></a></li> <!-- **no-underline, but underline-hover** -->
  </ol>
</div>
.news a {
  text-decoration: underline;
}

.news_links {
  text-decoration: none;
}

.news_links:hover {
  text-decoration: underline;
}

Answer №1

When multiple style declarations conflict on an element, the CSS Cascade plays a vital role in determining which CSS rules will be applied to that element. One crucial aspect that the Cascade considers is known as Specificity.


Understanding Specificity:

A more specific CSS declaration always takes precedence over less specific ones, regardless of where it appears in the stylesheet.

In general,

  • An ID selector (#id) holds higher specificity than any number of class selectors (.class), and a class selector has higher specificity than any number of type selectors (e.g. h1).
  • If no declaration contains a selector with higher specificity, a larger amount of a single selector will override a smaller amount of the same selector (as seen in your situation).

It's important to note that .news a is more specific because it includes both a class selector and a type selector, making it preferred over .news-links which only has one class selector.

In the code snippet below, I added another type selector with .news-links to make it equally specific, ensuring that this style will now be applied since it appears later in the stylesheet (based on Rule Order).

To delve deeper into Specificity, you can refer to MDN.

.news a {
  text-decoration: underline;
}

li .news_links {
  text-decoration: none;
}

.news_links:hover {
  text-decoration: underline;
}
<div class="news">
  <h2>Lorem ipsum</h2>
  <p>At vero eos et accusam et justo duo dolores et ea rebum.</p>
  <p><a href="xxx" target="blank">Individual Link</a></p>
  <!-- **underline** -->
  <ol>
    <li><a class="news_links" href="xxx" target="blank">List Link</a></li>
    <!-- **no-underline, but underline-hover** -->
  </ol>
</div>

Answer №2

There seems to be a slight issue with specificity here. However, you can resolve this particular problem by utilizing the following syntax instead:

.news a {
text-decoration: underline;
}

.news li a {
text-decoration: none;
}

.news_links:hover {
text-decoration: underline;
}
    <div class="news">
      <h2>Lorem ipsum</h2>
      <p>At vero eos et accusam et justo duo dolores et ea rebum.</p>
      <p><a href="xxx" target="blank">Link 1</a></p> 
      <ol>
        <li><a class="news_links" href="xxx" target="blank">Link 2</a></li>
      </ol>
    </div>

In the provided code, the issue lies in targeting links inside lists within the news class. The selector .news a takes precedence over .news_links, regardless of their order in the CSS.

You may find this website helpful in understanding specificity:

I hope this guidance proves useful to you. Best regards.

Answer №3

To ensure that the styling of .news a is overridden, you can include two additional selectors in your code. By adding the class .news before the definition, you limit its effect to within the div.news only.

.news a {
  text-decoration: underline;
}

.news a.news_links {
  text-decoration: none;
}

.news a.news_links:hover {
  text-decoration: underline;
}
<div class="news">
  <h2>Lorem ipsum</h2>
  <p>At vero eos et accusam et justo duo dolores et ea rebum.</p>
  <p>
    <a href="#" target="blank">Link underlined</a>
  </p>
  <ol>
    <li>
      <a class="news_links" href="#" target="blank">Link not underlined</a>
    </li>
  </ol>
</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

What is the best way to assign a class to a read-only field in HTML and CSS?

My situation is as follows: I have jQuery code that uses an if-statement to add a specific class to an element based on the selected value on a form page. I also created another form where users can view the details of a previously filled out form. However ...

CSS: concealing certain portions of an element's content

I don't have control over the following markup: <p class="filename"> <img src="http://domain.com/uploads/image_gallery/_thumbs/Chrysanthemum.jpg" alt="Chrysanthemum.jpg"> <br> Chrysanthemum.jpg </p> Is it possible ...

What is the best way to utilize :focus while simultaneously applying properties through Javascript?

When styling an input element with JavaScript, it seems that any properties set this way are ignored in a :focus rule. An example is provided to illustrate this: Initially, the background and border of the input element matches the surrounding element ( ...

Implementing jQuery in ASP.NET code-behind files

I am new to asp.net and trying to learn by experimenting with rendering aspx in only specific parts. I was able to make it work, but encountered an issue when trying to create a button, textbox, and label where the label would display the text from the tex ...

Incorporate dynamic interval transitions to the carousel rotation

I'm currently working on implementing a carousel with 'Next' and 'Previous' buttons that slide through images. However, I've been struggling to get the auto-slide feature to work at regular intervals. If anyone has any sugges ...

I created some jQuery code that modifies a button when it is hovered over, however, I ended up writing the code individually for each button. What steps can I take to turn it

Is there a way to turn the code for each button on my website into a jQuery function that can be applied to any button? This is how the code currently appears: $(document).ready(function() { $("#linkXML").hover( function() { ...

The button component is unresponsive

An app was developed with a component containing signin and signup buttons on the right side. However, there is an issue where the Sign up button is not clickable. Below is the code snippet for the component => import React from "react"; expo ...

How to adjust the banner image placement in Squarespace

I need the banner image to shift position on smaller screens, specifically those with a max-width of 414px. I want the focus to be on showcasing a specific product (a building) located on the right side of the image. This building should only appear on wid ...

What is the best way to create spacing between images in a CSS image slider?

UPDATE: I've made significant progress by modifying the code provided in the link below. I am very close to achieving my desired result, but there are parts of it that still puzzle me. The transition now has spacing on both sides and the hidden space ...

Utilizing HTML5 to import content from text files

Currently, I am experiencing an issue with HTML 5. I am struggling to find a way to load data into a web page statically from locally saved files. So far, my only successful method has been using < input type="file" id="fileinput" / >, but I am inter ...

Add a jQuery click function within a loop indefinitely

Hello there, I have a simple loop set up in PHP and I am trying to figure out how to create an event that triggers whenever the user clicks on any line generated by this loop. Here's my basic PHP while loop: <?php $x = 1; while($x <= 5) { ...

What is preventing the background image from being visible to me?

I'm having trouble displaying an image in a section of my website. Below is the CSS code for that specific section: #start{ height: 100vh; display:block; text-align: center; } .start-img{ background-image: url("assets/img/bg.png"); displa ...

Different approach to tagging div elements

Is there a way to label my divs differently to achieve the layout I need? I have a main div named errors where error messages are displayed. The issue arises when I need to insert form input areas and instruction text in between the error messages and text ...

What could be causing the HTML page to scroll up when the datepicker is initialized in JavaScript?

Wondering about a strange issue with my HTML page built using Laravel Blade. Whenever the date picker is initialized in JavaScript, the HTML page unexpectedly scrolls up. Here's the code snippet located at the bottom of the page: <script type=" ...

Mozilla puts an end to the default behavior of parent anchor tags

I am working with HTML code. In my HTML, the parent element is an anchor tag and the child element is a dropdown. I am trying to prevent the parent's default behavior. For example, when a user clicks on the dropdown, I want to trigger the dropdown ...

Navigating up and down in a horizontal row of 'tabs'

For a school project, I am in the process of creating a website that I wanted to make unique and different. However, I have encountered a major challenge with a tight deadline approaching. Any assistance would be greatly appreciated. Let's address ...

Loading stylesheets from node_modules before rendering in Angular

I have successfully installed Ionicons in Angular 9 by running npm install --save [email protected]. In order to use them, I imported them into my styles.scss file as shown below: $ionicons-font-path: "~ionicons/dist/fonts"; @import "~ionicons/dist/s ...

Adjust the size of a component dynamically to match another

In this scenario, we are dealing with two columns labeled A and B. Typically, column A contains more content, resulting in a greater height of 126px, while column B has less content, leading to a shorter height of 94px. The challenge arises when the h ...

JavaScript: Invoking a function within another function via an HTML document

I have a unique scenario where I have one function inside another and I am looking to call the inner function from an HTML document. <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="style.css"> ...

What could be causing my button to pass the event to its parent element?

I've implemented a clever trick within a div, where I have included my own custom "data" indicating what action to take when the user clicks on it. My JavaScript code snippet is as follows (with some code omitted for clarity): $('[data-hqf-swit ...