简体   繁体   中英

Can I prevent a CSS style to be overwritten?

I'd like to apply a CSS to some linkbuttons on page load but one of them <a id="lb1">logoff</a> must keep its style, no hover nor other event must change its style.

The linkbuttons have no class and the css applied to all of them is done to tags, this way:

a
{
 //style
}

a:hover
{
  // style
}

Is it possible?

No, you can't.

You can use more specific selectors (or even inline CSS with the style attribute) so that they are less likely to be overridden accidentally.

You can use the (eugh) sledgehammer of !important so they will only be overridden by another !important rule.

There is no way to prevent them being overridden though.

Please please please please please avoid using !important whenever possible. You will run into SO many annoying problems and issues from using this. I consider it a very lazy hack.

What you want to do is append a class to the link that you don't want overwritten. Classes are given a higher priority than general selectors (such a , p , b ). So if you append this class to the link, the CSS will override the default CSS you have set for a .

CSS:

a {
color: red;
}
a:hover {
color: blue;
}
.derp:hover { /*you can add everything you want to preserve here, essentially make it the same as the link css. you can also change it to #lbl:hover, although there's no good reason to be using an ID as a CSS selector*/
color: red;
}

HTML:

<a href="#">this will turn blue on hover</a>

<a class="derp" href="#">this will stay red on hover</a>

Here's a fiddle to show you. The second link has a class appended that preserves the original style: http://jsfiddle.net/p6QWq/

Why not add a class to all the link buttons you want to change, and not add it to the one you don't want to change.

Then you can call:

  $(".myClass").css("backgound-color", "blue");
  • This would change the background color for every element with a class of myClass to a blue background.

Or you could add a whole new class to the link buttons that have a class of myClass :

  $(".myClass").addClass("myExtraClass");
  • This would then make the class attribute of your link button class="myclass myExtraClass"

Seeing your code posted makes it a little more clear on what you want to do. Try this:

a {
    text-decoration: none;
    color: orange;
}
a:hover {
    text-decoration: underline;
    color: blue;
}

This would apply a default style to all <a> elements. Now you could overwrite this default style by providing a specific style for the anchor with the id you gave above:

#lb1 {
    color: black;
    text-decoration: none;
}
#lb1:hover {
    color: black;
    text-decoration: none;
}

I mocked this up in a quick and dirty jsFiddle . See if this gives you the desired result. IDs take precedence over classes and default element styling. So if you have one that you want to keep the same, apply and ID and style the particular element accordingly. This would also help you by preventing you from having to apply a class to several elements. It's less coding to apply one ID than to apply twelve classes. (Just an exaggerated example. I don't know how many links you have.)

Hope this helps.

css is cascading by definition, so any style you apply to a tags will apply to this specific one, except if you overwrite it.

You'll have to either assign a class to all the other buttons or overwrite all the default properties for this specific button.

Also, do not forget the pseudo-classes :visited and :active.

You should use !important in your css like :

a {
    /* style */
    background: #FFF !important;
}

a:hover {
    /* style */
    background: #FFF !important;
}

You could always overwrite your css by simply creating another stylesheet and place it at the END of your stylesheet links in the head of your html.

<head>
    <link rel="stylesheet" href="location/location/first_stylesheet.css">
    <link rel="stylesheet" href="location/location/revised_stylesheet.css">
</head>

This is not the most productive method of overwriting your css however; one would be well advised to eliminate the necessity for this separate stylesheet by simply appending elements with a class attribute. The class attr will allow you to modify basic html elements, tags and overlay a final layer to "rule them all". Enjoy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM