Html CSS-将参数传递给类

Html CSS-将参数传递给类,html,css,sass,css-preprocessor,Html,Css,Sass,Css Preprocessor,我不确定这是否可能,但仍在与你核实 假设我有一个类,它有很多属性,我想使用 该类位于另一节中,但有一个属性值不同 我们能做这样的事吗 请看我的例子: .class-test { position: relative; display: inline-block; @include border-radius(3px / 3px 3px 3px 4px); background-color: GREEN; width: 26

我不确定这是否可能,但仍在与你核实

假设我有一个类,它有很多属性,我想使用 该类位于另一节中,但有一个属性值不同

我们能做这样的事吗

请看我的例子:

.class-test {
       position: relative;
       display: inline-block;
        @include border-radius(3px / 3px 3px 3px 4px);
        background-color: GREEN;
       width: 266px; // This property should be dynamic..    
      .select-something {
            display: inline-block;
            font-size: 14px;
       }
       ....
        and much more properties..
       ....
}
所以我在两个不同的地方使用这个类 在其中一种情况下,宽度应为266px 在另一种情况下,宽度应为120px

  <div class="class-test">
       .....
       // here width should be 266px
  </div>

  <div class="class-test">
       .....
       // here width should be 120px
  </div>
当然,我可以创建两个不同宽度的类
但是它会以大量重复代码结束

我想你可以使用这个技巧:

<div class="class-test">
       .....
       // here width should be 266px
  </div>

  <div class="class-test" style="width:120px">
       .....
       // here width should be 120px
  </div>

我想你可以用这个技巧:

<div class="class-test">
       .....
       // here width should be 266px
  </div>

  <div class="class-test" style="width:120px">
       .....
       // here width should be 120px
  </div>
试试这个

<div class="class-test">
       .....
       // here width should be 266px
  </div>

  <div class="class-test testclass" >
       .....
       // here width should be 120px
  </div>
小提琴:试试这个

<div class="class-test">
       .....
       // here width should be 266px
  </div>

  <div class="class-test testclass" >
       .....
       // here width should be 120px
  </div>

Fiddle:

您可以从类中删除该属性

然后再使用两个类。例如

.width-1 {
width: 266px;
}

.width-2 {
width: 120px;
}
。 .

并在每个元素中包含两个类

<div class="class-test width-1">
       .....
       // here width should be 266px
</div>

<div class="class-test width-2">
     .....
     // here width should be 120px
</div>

您可以从类中删除该属性

然后再使用两个类。例如

.width-1 {
width: 266px;
}

.width-2 {
width: 120px;
}
。 .

并在每个元素中包含两个类

<div class="class-test width-1">
       .....
       // here width should be 266px
</div>

<div class="class-test width-2">
     .....
     // here width should be 120px
</div>

这似乎让我可以在页面上的任何位置放置视频

<!DOCTYPE html> 
<html> 

<head>
<style>
div.relative {
    position: relative;
    z-index: -1;
}
</style>
</head>

<body> 

<div class="relative" style='left:20px; top:30px'>
    <video width="400"  autoplay loop>
      <source src="dinoheads.mp4" type="video/mp4">
      Your browser does not support HTML5 video.
    </video>
</div>

<p>

</p>

</body> 
</html>

这似乎让我可以在页面上的任何位置放置视频

<!DOCTYPE html> 
<html> 

<head>
<style>
div.relative {
    position: relative;
    z-index: -1;
}
</style>
</head>

<body> 

<div class="relative" style='left:20px; top:30px'>
    <video width="400"  autoplay loop>
      <source src="dinoheads.mp4" type="video/mp4">
      Your browser does not support HTML5 video.
    </video>
</div>

<p>

</p>

</body> 
</html>
为什么不在第二个div中class=class-test-small,然后在CSS.class-test.small{width:120px;…?为什么不在第二个div中class=class-test-small,然后在CSS.class-test.small{width:120px。。。。。。?