Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a parameter to a CSS class from web page

Problem statement is, I have 2-3 web pages; and I want to have different body colors on this pages but keeping the class name unique to "myBody".

I looked for many blogs and authors but did not find any suitable answer to achieve this from a traditional CSS approaches.

Please suggest if it is possible to have a single CSS class accepting a parameter from a web page which will decide what body color should be applied using the same CSS with different parameters"

    .myBody(@color)
     {
     background-color: @color;
     font-size: 11px;
     color: Black;
     border: 1px solid;

     }

The answer may be tricky for some folks but I really want to see if I can achieve this using CSS only.

like image 558
Kaps Avatar asked Apr 15 '15 16:04

Kaps


People also ask

Can you pass a variable into CSS?

Like most programming languages, native CSS now has support for variables, and they're here to stay. If you know a bit of CSS, chances are you've heard of CSS preprocessors such as Sass and Less. You've probably used them in your projects regardless of your frontend framework of choice.

How do I pass data into a CSS file?

In general, you declare a custom variable by using a custom property name that begins with a double hyphen -- , and a property value that can be any valid CSS value. Using var() allows us to insert the value of custom property (variable).

How do I apply a CSS to a specific class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)

How do you pass text in CSS?

CSS can insert text content before or after an element. To specify this, make a rule and add ::before or ::after to the selector. In the declaration, specify the content property with the text content as its value.


1 Answers

You should split them up into different classes like this.

.myBody
 {
     font-size: 11px;
     color: Black;
     border: 1px solid;
 }
 .background_red
 {
     background: red;
 }
 .background_green
 {
     background: green;
 }

Then use them like this

<div class="mybody background_red"></div>

<div class="mybody background_green"></div>

You also have the ability to overwrite css like this:

.myBody
{
    background:red;
}
.overwrite_background
{
    background:green;
}

<div class="myBody"></div>
<div class="myBody overwrite_background"></div>

The first div would have a background of red where the second one would have a background of green.

Here is another post you should look at. This reference a couple of options you have to handle this situation. How to pass parameters to css classes

Another option is to use Sass. Sass allows you to use a programming language to create your css. This is wonderful for changing things over a mass on the fly. If you use the same color in multiple places, or if you want to have a different configuration for each site and still carry the same css just different colors.

like image 128
Sari Rahal Avatar answered Sep 29 '22 21:09

Sari Rahal