在一个元素上使用两个CSS类\[重复\]

CSS

米亚

2020-03-24

我在这里做错了什么?

我有一个.social div,但在第一个上,我想在顶部填充零,在第二个上,我想没有底部边框。

我试图为此首先和最后创建类,但我认为在某处错了:

.social {
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
    border-bottom: dotted 1px #6d6d6d;
}

.social .first{padding-top:0;}

.social .last{border:0;}

和HTML

<div class="social" class="first">
    <div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
    <div class="socialText">Find me on Facebook</div>
</div>

我猜不可能有两个不同的课程吗?如果可以,我该怎么做?

第3413篇《在一个元素上使用两个CSS类\[重复\]》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
神乐Mandy小哥 2020.03.24

要解决您的根本问题,可以使用:focus伪选择器来代替使用多个CSS类

input[type="text"] {
   border: 1px solid grey;
   width: 40%;
   height: 30px;
   border-radius: 0;
}
input[type="text"]:focus {
   border: 1px solid #5acdff;
}
JinJinEva 2020.03.24

我知道这篇文章已经过时,但这是他们的要求。在样式表中:

.social {
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
    border-bottom: dotted 1px #6d6d6d;
}
[class~="first"] {
    padding-top:0;
}
[class~="last"] {
    border:0;
}

但这可能是使用选择器的不好方法。另外,如果您需要多个“第一个”扩展名,则必须确保设置不同的名称或优化选择器。

[class="social first"] {...}

我希望这会对某人有所帮助,在某些情况下可以非常方便。

例如,如果您有一小段必须链接到许多不同组件的css,并且您不想编写一百遍相同的代码。

div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}

div.myClass1.red {color:red;}
div.myClass2.red {color:red;}
...
div.myClassN.red {color:red;}

成为:

div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}

[class~=red] {color:red;}
宝儿 2020.03.24

另一种选择是使用后代选择器

HTML:

<div class="social">
<p class="first">burrito</p>
<p class="last">chimichanga</p>
</div>

参考CSS中的第一个: .social .first { color: blue; }

参考CSS中的最后一个: .social .last { color: green; }

Jsfiddle:https://jsfiddle.net/covbtpaq/153/

卡卡西 2020.03.24

请记住,可以通过将每个类的类属性内的空格分隔开来将多个类应用于一个元素。例如:

<img class="class1 class2">
宝儿 2020.03.24

如果您只想将样式应用于作为其父项的第一个孩子的元素,那么使用:first-child伪类更好吗?

.social:first-child{
    border-bottom: dotted 1px #6d6d6d;
    padding-top: 0;
}
.social{
    border: 0;
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
}

然后,规则.social既具有通用样式,又具有最后一个元素的样式。

.social:first-child使用第一个元素的样式覆盖它们。

您也可以使用:last-child选择器,但:first-child旧浏览器更支持它:请参阅 https://developer.mozilla.org/en-US/docs/CSS/:first-child#Browser_compatibilityhttps://developer.mozilla.org/ es / docs / CSS /:last-child#Browser_compatibility

Harry飞云 2020.03.24

如果您有2个类,即.indent.font,则class="indent font"可以。

您不必.indent.font{}在CSS中。

您可以将类在css中分开,并且仍然仅使用class="class1 class2"html中的进行调用您只需要在一个或多个类名之间留一个空格即可。

飞云古一 2020.03.24

如果要在一个元素上使用两个类,请按以下方式进行操作:

<div class="social first"></div>

像这样在css中引用它:

.social.first {}

例:

https://jsfiddle.net/tybro0103/covbtpaq/

小胖 2020.03.24

您可以尝试以下方法:

的HTML

<div class="social">
    <div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
    <div class="socialText">Find me on Facebook</div>
</div>

CSS代码

.social {
  width:330px;
  height:75px;
  float:right;
  text-align:left;
  padding:10px 0;
  border-bottom:dotted 1px #6d6d6d;
}
.social .socialIcon{
  padding-top:0;
}
.social .socialText{
  border:0;
}

要在同一元素中添加多个类,可以使用以下格式:

<div class="class1 class2 class3"></div>

演示

Mandy 2020.03.24

如果只有两个项目,则可以执行以下操作:

.social {
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
    border: none;
}

.social:first-child { 
    padding-top:0;
    border-bottom: dotted 1px #6d6d6d;
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android