简体   繁体   English

如何将参数传递给css类

[英]How to pass parameters to css classes

I want to know is it possible to add some flexibility to css via this: 我想知道是否可以通过这种方式为css增加一些灵活性:

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

where .round is a class with round corners and '5' determines the amount of radius. 其中.round是一个带圆角的类,'5'决定了半径。 Is it possible? 可能吗? I have seen some where, but I don't know how the implementation takes place. 我已经看到了一些地方,但我不知道实施是如何进行的。

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. 对于任何在2018年遇到这种情况的人来说, 虽然不完全支持的 CSS变量现在使您能够将变量直接传递到您的类中。

<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. 如果在一个元素中设置--radius则不会影响另一个元素。 Pretty jazzy right! 非常爵士吧!

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: HTML:

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

CSS: 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. .rounded类添加边框半径, .blue.green类添加背景颜色。

(I like to name and order the classes such that they read logically, like <div class="large box"></div> , etc.). (我喜欢命名和排序类,使它们在逻辑上阅读,如<div class="large box"></div>等)。

Here is an answer that I came up with that requires a small amount of jQuery, and a small knowledge of Regex. 这是我提出的答案,需要少量的jQuery,以及对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. .match()函数使用Regex。 Regex is used to detect parts of strings. 正则表达式用于检测字符串的一部分。 The \\d detects any digits. \\d检测到任何数字。 The + matches the previous selector 1 or more times. +与前一个选择器匹配1次或更多次。 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. 那么jQuery稍后会在border-radius属性中使用它。 All you have to do is append px , and you are good to go. 你所要做的就是附加px ,你很高兴。

Fiddle 小提琴

You can use multiclassing on the element. 您可以在元素上使用多重分类。 Eg.: 例如。:

HTML: 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: CSS:

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

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

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

You could do something similar but not exactly the way you've put it. 你可以做类似的事情但不完全像你说的那样。

CSS CSS

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

.r5{
    border-radius:5px;
}

HTML 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. 在上面的示例中,将保留红色边框,但border-radius将更改。

Note that you don't start class names with numbers, hence r5 rather than 5 请注意,您不会使用数字启动类名,因此请使用r5而不是5

you can do this. 你可以这样做。 but you have to create the css in the html document(not linked, but between the <style> tag). 但你必须在html文档中创建css(未链接,但在<style>标记之间)。 you can use php or javascript to make a loop. 你可以用php或javascript来制作一个循环。 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 :d

Maybe what you want is like this 也许你想要的就是这样

CSS CSS

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

HTML HTML

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

You can do what you are saying but you would have to reserve the keyword "round" for only this purpose. 你可以按照你的意思去做,但你必须保留关键字“round”才能达到这个目的。 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. 第一个代码块选择包含单词round的所有类名,这既可以是好事也可以是坏事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM