Reputation: 12922
I have a style assigned for a specific HTML element in my stylesheet file, like this
label {
width: 200px;
color: red;
}
In one special case, I would like to use a label element in the HTML document, but ignore the style specified for the label element (just for one instance in the document). Is there any way to achieve this in a generic way without overriding the element style attributes with inline styles?
<label for="status">Open</label>
To reiterate, to the HTML code displaying the label the element style (width 200 px, color red) is applied. I would like the default style attributes to be used (without knowing what they are) and without having to know what is specifically specified in the element style setting.
Upvotes: 15
Views: 53745
Reputation: 12922
The situation has somewhat changed in 6 years: now it is possible to remove a set CSS property without using JavaScript by using the initial, unset or revert keywords. These are supported in all modern browsers.
Upvotes: 4
Reputation: 8237
You can use the :not selector:
label:not([for="status"]) {
width: 200px;
color: red;
}
Support seems to date back to around 2009 (FF3.5)
Upvotes: 0
Reputation: 60413
<label ... class="default">...</label>
label {
width: 200px;
color: red;
}
label.default {
width: auto;
color: inherit;
}
however that may not provide the desired results - the other way would be to give all the other labels a particluar class(es) and then not assign that class to the "default" label.
Upvotes: 2
Reputation: 25257
From the Mozilla developer center:
Because CSS does not provide a "default" keyword, the only way to restore the default value of a property is to explicitly re-declare that property.
Thus, particular care should be taken when writing style rules using selectors (e.g., selectors by tag name, such as p) that you may want to override with more specific rules (such as those using id or class selectors), because the original default value cannot be automatically restored.
Because of the cascading nature of CSS, it is good practice to define style rules as specifically as possible to prevent styling elements that were not intended to be styled.
Upvotes: 20
Reputation: 22841
I don't think you can reset it to what it was before the label definition in the stylesheet, but you could give the label an id and override it in your stylesheet.
<label for="status" id="status-label">Open</label>
label {
width: 200px;
color: red;
}
label#status-label {
width: auto;
color: blue;
}
Upvotes: 2