Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在类之间共享属性_Html_Css_Web - Fatal编程技术网

Html 在类之间共享属性

Html 在类之间共享属性,html,css,web,Html,Css,Web,我有两个类共享属性,例如: .divone{ width:10px; height:20px; float:right; cursor:pointer; } .divtwo{ width:11px; height:10px; float:right; cursor:pointer; } 如您所见,这两个类共享属性:“float”和“cursor”。如何在同一个类中声明这两个属性,然后将其应用于这两个类?有点像这样: .sharedproperties{ float:right; cursor:

我有两个类共享属性,例如:

.divone{ width:10px; height:20px; float:right; cursor:pointer; }
.divtwo{ width:11px; height:10px; float:right; cursor:pointer; }
如您所见,这两个类共享属性:“float”和“cursor”。如何在同一个类中声明这两个属性,然后将其应用于这两个类?有点像这样:

.sharedproperties{ float:right; cursor:pointer; }

.divone{ width:10px; height:20px; (+ .sharedproperties)}
.divtwo{ width:11px; height:10px; (+ .sharedproperties)}

谢谢

您只需在html文件中这样调用它们:

<div class="sharedproperties divone"><!-- Some content here --></div>

如果不想在标记中添加额外的类名,可以在CSS中使用逗号将规则应用于多个选择器:

.divone, .divtwo { float:right; cursor:pointer; }
.divone{ width:10px; height:20px; }
.divtwo{ width:11px; height:10px; }

它可以工作,但是,有没有什么方法我只能在CSS中这样做?我的意思是,将一个类中声明的属性添加到另一个类中。谢谢,伙计,据我所知不是这样。我总是看到每个人都这样做。看这里:你为什么要这么做?在html中使用多个类是正确的方法。如果出于某种无法解释的原因,这是一个问题,您可以使用CSS预处理器(如Sass)将选择器扩展到其他选择器中,但这很快就会变得一团糟。多节课确实是你应该做的。你不会有更好的答案,因为这是最好的!
.sharedproperties{
  float: right;
  cursor: pointer;
}
.divone {
  width:10px;
  height:20px;
}
.divtwo {
  width:11px;
  height:10px;
}
.divone, .divtwo { float:right; cursor:pointer; }
.divone{ width:10px; height:20px; }
.divtwo{ width:11px; height:10px; }