Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I exclude a specific element from inheriting CSS rules?

I have this CSS:

a {   color:#19558D;   padding:3px 5px;   text-decoration:none; }  a:hover {   background-color:#D1E1EA;   color:#19558D;   text-decoration:none; } 

It applies to all links, but what if I don't want it to apply to a specific link on the page? What can I do?

like image 495
Justin Meltzer Avatar asked Mar 31 '11 22:03

Justin Meltzer


People also ask

How do I exclude an element from a rule in CSS?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.

How do you exclude an element?

Press TAB to highlight the element, and then click to select it. In the drawing area, click the icon ( ) to exclude the element, or right-click, and click Exclude.

How do you select certain elements in CSS?

The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.


2 Answers

There are two ways you can do this.

First way is to use the :not() selector and give your link that you don't want the styles applied to class:

a:not(.unstyled):hover {   background-color:#D1E1EA;   color:#19558D;   text-decoration:none; } 

However, the :not() selector is not supported in IE8 or less, so the second option is to give your unstyled links a class, and override those properties for that link with that class:

a.unstyled:hover {   background-color:none;   color:#000   text-decoration:none; }  
like image 71
Alex Avatar answered Oct 04 '22 18:10

Alex


You can apply your own class or inline style to the link in question.

for example:

<a href="#" class="MyNewClass" /> 

or

<a href="#" style="color:red;" /> 
like image 43
NotMe Avatar answered Oct 04 '22 18:10

NotMe