How to pass parameters to css classes

HtmlCss

Html Problem Overview


I want to know is it possible to add some flexibility to css via this:

<div class='round5'></div>

where .round is a class with round corners and '5' determines the amount of radius. Is it possible? I have seen some where, but I don't know how the implementation takes place.

Html Solutions


Solution 1 - Html

For anyone stumbling across this in 2018, whilst not fully supported CSS variables now give you the ability to pass a variable directly into your class.

<div class="round" style="--radius: 100%;"></div>
<style>
  .round {
    display: block;
    height: 40px;
    width: 40px;
    border: 1px solid #BADA55;
    border-radius: var(--radius);
  }
</style>

You can also define root variables and pass them in as well

<div class="round" style="--radius: var(--rad-50);"></div>
<style>
  :root {
    --rad-0: 0%;
    --rad-50: 50%;
    --rad-100: 100%;
  }
  .round {
    display: block;
    height: 40px;
    width: 40px;
    border: 1px solid #BADA55;
    border-radius: var(--radius);
  }
</style>

This is also scoped to the element as well. If you set the --radius in one element is wont effect another element. Pretty jazzy right!

Solution 2 - Html

You can't define the border radius separate from its value because it's all one property. There's no way to tell an element to have rounded corners "in general" without also specifying how much to round them by.

However, you can do something kind of similar with multiple classes and different properties:

HTML:

<div class="rounded blue"></div>
<div class="rounded green"></div>

CSS:

.rounded {
    border-radius: 5px;
}
.blue {
    background: blue;
}
.green {
    background: green;
}

The .rounded class adds the border radius and the .blue and .green classes add the background color.

(I like to name and order the classes such that they read logically, like <div class="large box"></div>, etc.).

Solution 3 - Html

Here is an answer that I came up with that requires a small amount of jQuery, and a small knowledge of Regex.

    $(function() {
      var number = $("div").attr("class").match(/\d+$/);
      $("div").css({
        "width": "100px",
        "height": "100px",
        "background-color": "green",
        "border-radius": number + "px"
      });
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='round54'>hello</div>

The .match() function uses Regex. Regex is used to detect parts of strings. The \d detects any digits. The + matches the previous selector 1 or more times. In other words, the number can be a multi digit number. And the $ means it has to be at the end.

So then the jQuery uses that in the border-radius property later. All you have to do is append px, and you are good to go.

Fiddle

Solution 4 - Html

You could do something similar but not exactly the way you've put it.

CSS

.radius{
    border-radius: 10px;
    border: 1px solid red;
}

.r5{
    border-radius:5px;
}

HTML

<div class="radius">Hello World</div>
<br/>
<div class="radius r5">Hello World</div>

Working Example

In the example above the red border will be retained but the border-radius will change.

Note that you don't start class names with numbers, hence r5 rather than 5

Solution 5 - Html

You can use multiclassing on the element. Eg.:

HTML:

<div class="round">Box without border radius</div>
<div class="round rounded-5">Box with 5px border radius</div>
<div class="round rounded-10">Box with 10px border radius</div>

CSS:

.round {
    border: 1px solid #000;
    width: 100px;
    height: 100px;
}

.round.rounded-5 {
    border-radius: 5px;
}

.round.rounded-10 {
    border-radius: 10px;
}

Solution 6 - Html

you can do this. but you have to create the css in the html document(not linked, but between the <style> tag). you can use php or javascript to make a loop. for example try this:

<style>
    <?php
    $round = 5;
    for ($round = 50; $round <= 150; $round+=25){
   
   echo "#round$round{
       height: 300px;
       width: 300px;
       background: #f00;

border-radius : ".$round."px;
    margin: 2px;
}
";
   
    }
    ?>
</style>
<?php 
for ($round=50;$round<=150; $round+=25){

    echo "<div id='round$round'>
    
</div>
            ";
    
}

?>

hope this helps! :D

Solution 7 - Html

Maybe what you want is like this

CSS

.round {
  border-radius: 4px; /*it's default when you juse using .round*/
}
.round.five {
  border-radius: 5px;
}
.round.ten {
  border-radius: 10px;
}

HTML

<div class="round five">something</div>

Solution 8 - Html

You can do what you are saying but you would have to reserve the keyword "round" for only this purpose. If you look at the following.

div[class*="round"] {
	background-color: green;
	color: white;
	padding: 10px;
}

And then targeting specific variants of it using...

div[class="round5"] {
	border-radius: 5px;
}

The first block of code selects all class names which contain the word round, this can be both a good thing and a bad thing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMostafa TalebiView Question on Stackoverflow
Solution 1 - HtmlstwilzView Answer on Stackoverflow
Solution 2 - HtmldaGUYView Answer on Stackoverflow
Solution 3 - HtmlCody GuldnerView Answer on Stackoverflow
Solution 4 - HtmlapaulView Answer on Stackoverflow
Solution 5 - HtmlsanntuView Answer on Stackoverflow
Solution 6 - HtmlOki Erie RinaldiView Answer on Stackoverflow
Solution 7 - HtmlBariq DharmawanView Answer on Stackoverflow
Solution 8 - HtmllimitlessloopView Answer on Stackoverflow