I want a generic class in my scss file which sets the color and border property if i just pass a color value. I dont want to hard code color value in my scss file.
@mixin myStyle($color) {
color: $color;
border-left: 5px solid $color;
}
.item{
@include myStyle(red); // i want to pass color value from my html class.
}
How to pass arguments from html class name ?
You can pass value from HTML to CSS class using css custom properties (variables) .fill-color { display: block; height: 40px; width: 40px; color: var (--color); } Really Impressive. But can we do for any other style ? What about parent element, in case if i want color on somewhere else element how can we do this?
We can also retrieve CSS variables using getComputedStyle in JavaScript. The logic behind this is fairly simple: custom properties are part of the style, therefore, they are part of computed style. Same sort of deal with getPropertyValue. That let us get the custom property value from an inlined style from HTML markup.
To make Sass (or, specifically, SCSS in this case) variables available to JavaScript, we need to “export” them. The :export block is the magic sauce webpack uses to import the variables. What is nice about this approach is that we can rename the variables using camelCase syntax and choose what we expose.
Sometimes you need to share variables between CSS (or SCSS) and Typescript, for example, if you have a list of colors in your SCSS file and need to check the variable names in typescript to be sure is an available color. Imagine a Vue component have a property to set the background color:
I've create a JSFiddle for you: enter link description here
HTML:
<p class="item" data-test="red">Vikramaditya</p>
SCSS:
p::before {
content: attr(data-test) " ";
}
please note: The attr() function can be used with any CSS property, but support for properties other than content is experimental.
source: enter link description here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With