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

丶灬走出姿态 提交于 2019-12-04 23:52:32

First of all, if you don't want the browsers own history to interfere with your styles then use the :visited pseudo-class to match the style of the non-visited link, then just apply classes manually based on your DB records.

Regarding conflicting styles, it's all about the specificity of the selector, and if two with the same properties conflict (have the same specificity) the last one "wins".

Do this:

a:link, 
a:visited {
    font-weight: bold;
    color: black;
}

a.read {
    color: #444;
}

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.

One, check your HTML to make sure class="read" and class="unread" are being added to your links.

Two, try adding a in your .read and .unread CSS rules:

a.read { /* ... */ }
a.unread { /* ... */ }

If that doesn't work, try adding a space before !important. I don't think this is required, but most examples I have seen include it.

Ria

Try:

a.unread, a:visited.unread {style 1}

a.read, a:visited.read {style 2}

You can define specificity of your CSS selectors.

a { /* style 1 */ }

would be overridden by

div a { /* style 2 */ }

where div is a parent element of a

More details can be found on Selectutorial.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!