How to pass parameters to css classes

前端 未结 8 2095
我在风中等你
我在风中等你 2020-12-08 09:57

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

where .round i

相关标签:
8条回答
  • 2020-12-08 10:01

    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

    0 讨论(0)
  • 2020-12-08 10:07

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 10:09

    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.).

    0 讨论(0)
  • 2020-12-08 10:11

    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

    0 讨论(0)
  • 2020-12-08 10:17

    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!

    0 讨论(0)
  • 2020-12-08 10:17

    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>
    
    0 讨论(0)
提交回复
热议问题