Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do not apply CSS on a specific TAG

Tags:

html

css

I do not know if is possible in CSS, but I would like a general Role to don't apply to a specific element in my case id=special. In my example all the DIV should be red but I want only id=special to have not applied this color. I understand I can always overwrite the element with id=special but I'm interested to know if is possible have some sort of exclusion from the general Role.

Any idea in CSS 2 and CSS 3?

Example:

<div>this is red</div>
<div>this is red</div>
<div id="special">this one must be blus</div>
div>this is red</div>

// My General Role
div
{
  background-color:Red;
}
like image 778
GibboK Avatar asked Sep 28 '11 15:09

GibboK


People also ask

How do you exclude tags 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 I apply a specific tag in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

Will you use to style a single specific tag?

An inline CSS is used to apply a unique style to a single HTML element. An inline CSS uses the style attribute of an HTML element.


5 Answers

CSS2 Solution

div {
    color: red;
}

div#special {
    color: inherit;
}

demo: http://jsfiddle.net/zRxWW/1/

like image 101
Germán Rodríguez Avatar answered Sep 28 '22 09:09

Germán Rodríguez


CSS3 allows for the :not() selector:

div:not([id])
-or-
div:not(#special)
depending on what you want to exclude

You can read all about the selectors in the w3c docs.

As for CSS2, there's a limited set of selectors, and they don't account for negation.

like image 34
zzzzBov Avatar answered Sep 28 '22 07:09

zzzzBov


CSS3 solution:

div:not(#special)
like image 41
JNDPNT Avatar answered Sep 28 '22 07:09

JNDPNT


You would want to use div:not(#special), but if it's going to be blue anyways then you might as well just override the rule. The :not() selector is a CSS3 property and thus isn't supported by all browsers.

like image 25
ayyp Avatar answered Sep 28 '22 08:09

ayyp


This is a great example of how CSS specificity works. A general rule applying to all elements of a certain type (your div rule turning the background red) is superseded by the more specific rule applying to an ID. This concept has existed since CSS1, and is available in all browsers. Thus:

div {
  background-color:red;
}
#special {
 background-color: blue;  
}

... will leave all divs red. By applying the special id to a div, that one div will be blue instead.

See it in action: http://jsfiddle.net/9Hyas/

Check out these articles for more about CSS specificity: http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/ and http://www.htmldog.com/guides/cssadvanced/specificity/

like image 21
Chris Baker Avatar answered Sep 28 '22 08:09

Chris Baker