How to Override the Properties of a CSS Class Using Another CSS Class

Is possible overwrite a css class with another css class

Update: According to ZK documentation you could use sclass attribute to add classes to any element.

So, using the code I see in your screenshot...

 <vbox>
<combobox>
<combobox sclass="whatever">
<combobox>
</vbox>

...you'll be able to use the following selector to specify rules for the <combobox>es you place whatever class on:

.v-combobox.whatever .z-combobox-input {
border:1px solid #000;
border-radius: 3px 0 0 3px;
margin:0; padding:4px 5px;
line-height: 14px;
background: #fff;
}

For more information use this guide.

According to this guide, you should use sclass when you want to add to the default styles applied to an element and zclass attribute when you want to reset the default styles (that means only what you define in your custom class will apply, and not the default styling for that element).


Initial answer:

This is the most important principle in CSS and it's called specificity.

Learning CSS means learning how you can use selectors and their specificity in order to apply rules selectively to some elements and not to others. It's what CSS is typically used for and it's totally possible.

Referencing an element by its id will be stronger (and hence override) any rules specified for any of its classes.

To understand specificity, I recommend this article as a starting point. You should also search for specificity calculator in your search engine of choice.

And in order to be able to use everything you learn about specificity you'll need to understand CSS Selectors and CSS Combinators.


For your specific [ :) ] case, you probably want to use the element's id as selector to apply rules to that element only. Given the id from your first example, this will work:

#vXgV3-real {
/* the rules here will override the rules for .z-combobox-input
* for the element with id="vXgV3-real" and only for that element
*/
border:1px solid #000;
border-radius: 3px 0 0 3px;
margin:0; padding:4px 5px;
line-height: 14px;
background: #fff;
}

CSS class override property behaviour

It's the latter. Cascade resolution is on a per-property basis. If the color property exists somewhere with higher precedence (in this case, the internal stylesheet), then that property is cascaded to the more precedent one. The rest of the properties carry over because no more precedent declarations exist.

Interestingly, the CSS2.1 spec seems to conflate "style rules" and style declarations, in section 6.4. This may be a source of confusion. The subsection 6.4.1 clarifies this by referring only to property declarations.

How to Override CSS class?

all you have to do to overwrite a css property is to write it again but after the declaration. The browser reads you style file from the top to the bottom and applies only the last declaration of the same element.

.catsandstars {
width: 200px;
height: 200px;
background-image: url("https://developer.mozilla.org/samples/cssref/images/startransparent.gif");
}
<div class="catsandstars"></div>

how to override a css class without affecting other classes with the same name

There are multiple ways you can approach this. One, you can create an id for that HTML tag that you want add'l/different style and create a #<id_name> in your css to handle that style or add another classname in front of container for the one you want to style. Or you can use CSS specificity for the nth child render this add'l style.

Approach 1)

HTML

<tagname id="other_css" class="container" ...>

CSS

#other_css {
// css stuff
}

Approach 2

HTML

<tagname class="container other_css" ...>

CSS

.other_css {
...
}

.container {
....
}

Approach 3

HTML

<wrapper>
<tagname class="container ...>
<tagname class="container ...>
<tagname class="container ...> <- the one you care about
</wrapper>

CSS

wrapper:nth-child(3) {
...
}

Resources:
- https://www.w3.org/TR/selectors-3/#specificity

override a CSS class if two other classes are present

Just go

.class1.class2.class3 {
/* these definitions are more specific and don't need !important; */
}

Remember this requires you to put class3 on the element as well:

<element class="class1 class2 class3"></element>

If the only reason you wanted to add class3 is to specify styles that should be applied when both classes class1 class2 are present, you don't even need class3, just go

.class1.class2 {
/* these definitions are more specific than those for .class1 or .class2
and don't need !important;
These rules apply to any element that has both class1 and class2 */
}

CSS multiple classes property override

As long as the selectors have the same specificity (in this case they do) and .myclass-right style block is defined after .myclass, yes.

Edit to expand: the order the classes appear in the html element has no effect, the only thing that matters is the specificity of the selector and the order in which the selectors appear in the style sheet.

CSS: Is it possible to override one class with another?

CSS has a very well-defined order of priority.

If two selectors have the same priority (as per your two single-class selectors), then with all else being equal, the one which is defined last in the CSS code is the one that takes priority.

So the simple solution here is to move your template CSS to lower down your CSS code.

If you can't do that for whatever reason, your best option is to make the template selector more specific. For example, if all your template elements are contained inside a div element and you know that's always going to be true, you could change the selector to div .template. It is now more specific than the .Category selector, and should therefore take precedence.

Finally, you always have the option of !important. I try to avoid using it if possible, as it tends cause issues if overused, but it is there for cases where it's needed, and in fact, cases like this are about the best justified use-case for !important I can think of.



Related Topics



Leave a reply



Submit