For example if I have the following HTML:
<div class="someDiv"></div>
and this CSS:
.opacity {
filter:alpha(opacity=60);
-moz-opacity:0.6;
-khtml-opacity: 0.6;
opacity: 0.6;
}
.radius {
border-top-left-radius: 15px;
border-top-right-radius: 5px;
-moz-border-radius-topleft: 10px;
-moz-border-radius-topright: 10px;
}
.someDiv {
background: #000; height: 50px; width: 200px;
/*** How can I reference the opacity and radius classes here
so this div has those generic rules applied to it as well ***/
}
Like how in scripting languages you have generic functions that are used often written at the top of the script and every time you need to use that function you simply call the function instead of repeating all the code every time.
This question is related to
css
I had this problem yesterday. @Quentin's answer is ok:
No, you cannot reference one rule-set from another.
but I made a javascript function to simulate inheritance in css (like .Net):
var inherit_array;_x000D_
var inherit;_x000D_
inherit_array = [];_x000D_
Array.from(document.styleSheets).forEach(function (styleSheet_i, index) {_x000D_
Array.from(styleSheet_i.cssRules).forEach(function (cssRule_i, index) {_x000D_
if (cssRule_i.style != null) {_x000D_
inherit = cssRule_i.style.getPropertyValue("--inherits").trim();_x000D_
} else {_x000D_
inherit = "";_x000D_
}_x000D_
if (inherit != "") {_x000D_
inherit_array.push({ selector: cssRule_i.selectorText, inherit: inherit });_x000D_
}_x000D_
});_x000D_
});_x000D_
Array.from(document.styleSheets).forEach(function (styleSheet_i, index) {_x000D_
Array.from(styleSheet_i.cssRules).forEach(function (cssRule_i, index) {_x000D_
if (cssRule_i.selectorText != null) {_x000D_
inherit_array.forEach(function (inherit_i, index) {_x000D_
if (cssRule_i.selectorText.split(", ").includesMember(inherit_i.inherit.split(", ")) == true) {_x000D_
cssRule_i.selectorText = cssRule_i.selectorText + ", " + inherit_i.selector;_x000D_
}_x000D_
});_x000D_
}_x000D_
});_x000D_
});
_x000D_
Array.prototype.includesMember = function (arr2) {_x000D_
var arr1;_x000D_
var includes;_x000D_
arr1 = this;_x000D_
includes = false;_x000D_
arr1.forEach(function (arr1_i, index) {_x000D_
if (arr2.includes(arr1_i) == true) {_x000D_
includes = true;_x000D_
}_x000D_
});_x000D_
return includes;_x000D_
}
_x000D_
and equivalent css:
.test {_x000D_
background-color: yellow;_x000D_
}_x000D_
_x000D_
.productBox, .imageBox {_x000D_
--inherits: .test;_x000D_
display: inline-block;_x000D_
}
_x000D_
and equivalent HTML :
<div class="imageBox"></div>
_x000D_
I tested it and worked for me, even if rules are in different css files.
Update: I found a bug in hierarchichal inheritance in this solution, and am solving the bug very soon .
You can use var() function.
The var() CSS function can be used to insert the value of a custom property (sometimes called a "CSS variable") instead of any part of a value of another property.
Example:
:root {
--main-bg-color: yellow;
}
@media (prefers-color-scheme: dark) {
:root {
--main-bg-color: black;
}
}
body {
background-color: var(--main-bg-color);
}
If you're willing and able to employ a little jquery, you can simply do this:
$('.someDiv').css([".radius", ".opacity"]);
If you have a javascript that already processes the page or you can enclose it somewhere in <script> tags. If so, wrap the above in the document ready function:
$(document).ready( function() {
$('.someDiv').css([".radius", ".opacity"]);
}
I recently came across this while updating a wordpress plugin. The them has been changed which used a lot of "!important" directives across the css. I had to use jquery to force my styles because of the genius decision to declare !important on several tags.
You can easily do so with SASS pre-processor by using @extend.
someDiv {
@extend .opacity;
@extend .radius;
}
Ohterwise, you could use JavaScript (jQuery) as well:
$('someDiv').addClass('opacity radius')
The easiest is of course to add multiple classes right in the HTML
<div class="opacity radius">
Just add the classes to your html
<div class="someDiv radius opacity"></div>
You can't unless you're using some kind of extended CSS such as SASS. However it is very reasonable to apply those two extra classes to .someDiv
.
If .someDiv
is unique I would also choose to give it an id and referencing it in css using the id.
Source: Stackoverflow.com