Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set one style to override another conflicting style in CSS?

Tags:

I'm displaying links that get marked as read in a database when a user clicks them. I want to style the clicked and unclicked links based on the database information not the user's browser history. So far, when I use:

 10 a:visited {  11   color: #444;  12 }  13  14 a:link {  15   font-weight: bold;  16   color:black;  17 }  18  19 .read {  20   color: #444!important;  21 }  22  23 .unread {  24   font-weight: bold!important;  25   color:black!important;  26 } 

and

<tr class="even">   <td><a class="read" href="example.com">blah</a></td> </tr> <tr class="odd">   <td><a class="unread" href="example.org">foo</a></td> </tr> 

and a link has been visited, but not from this page (it's still marked as unread in the database), I get weird results. For example only the color will work, but the weight won't, etc.

Is it possible to have one style override another when they conflict?

Thanks!

EDIT: updated code to clarify

Solution

 10 a:link,  11 a:visited {  12   font-weight: bold;  13   color: black;  14 }  15  16 a.read {  17   color: #444;  18   font-weight: lighter !important; /* omission or even "normal" didn't work here. */  19 }  20  21 a.unread {  22   font-weight: bold !important;  23   color: black !important;  24 } 
like image 885
Thomas G Henry Avatar asked Feb 14 '09 21:02

Thomas G Henry


People also ask

How do I make one CSS override another?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.

How do you override inherited CSS styles?

Identify the element with inherited style by right-clicking the element and select Inspect Element within your browser. A console appears, and the element is highlighted on the page. Right-click the element and select Copy > Copy selector. You will paste this selector into your variation code.

What is CSS style overriding?

CSS allows you to apply styles to web pages. More importantly, CSS enables you to do this independent of the HTML that makes up each web page. Overriding: Overriding in CSS means that you are providing any style property to an element for which you have already provided a style.


1 Answers

You can use the !important directive. eg.

.myClass {    color:red !important;    background-color:white !important; } 

Place !important after each style as shown above when you need to override any other styles also being applied.

like image 139
Ash Avatar answered Oct 16 '22 21:10

Ash