开发者

avoid * from css styles

i must set styles within other styles from a 开发者_如何学Cpage that have a style like this:

* {
width : 200px;
color : #0000;
}

and it applies styles to all tags include my own. Is there a way to avoid those styles with asterisks and that my owns are not changed to taht styles?

I've set inline styles in my tags, but the asterisk change it anyways.

Thanks.


You can set css flag !important in your styles to override written styles.


Use more specific selectors to overwrite the existing one. Or you can put your stuff in a div with a class and reset CSS for everything inside that div.

.yourstuff * {
  /* reset your stuff here */
  Width: auto; /* etc. */
}

<div class="yourstuff">
  Your stuff
</div>


You stated that you couldn't override the * via inline tags but I think what you meant was you couldn't override it via tag attributes. This is correct, CSS will always override the old style tag attributes. If you set them within a style attribute however, it should work fine.

For example:

<html>

<head>
<style type="text/css">
* {
    width : 200px;
    color : #000000;
}
</style>
</head>

<body>

<div style="background-color: aqua" width="100%">
    This is some text </div>
<div style="background-color: yellow; width: 100%;">
    This is some text </div>

</body>

</html>

The first DIV will be limited to 200px wide because the Width attribute is overridden by your * style.

The second DIV will be 100% of the screen because the Width style will override your * style. CSS works by applying the most specific rule it can find. Because the inline style is as specific as you can get, it always wins.


No.

If a selector matches an element then its rules will apply.

Either:

  1. Change the selector so it doesn't match
  2. Write new ruleset(s) that override the rules with new ones
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜