Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass value to scss class from HTML [duplicate]

Tags:

html

css

sass

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 ?

like image 512
Vikramaditya Avatar asked Mar 20 '16 07:03

Vikramaditya


People also ask

How to pass value from HTML to CSS class?

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?

How to get the value of a CSS Variable in JavaScript?

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.

How do I import variables from Sass to JavaScript?

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.

Can I share variables between CSS and typescript?

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:


1 Answers

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

like image 174
Oron Bendavid Avatar answered Sep 30 '22 04:09

Oron Bendavid