【问题标题】:Can I add multiple classes to a div element? (CSS) [duplicate]我可以向一个 div 元素添加多个类吗? (CSS)[重复]
【发布时间】:2016-02-15 03:14:32
【问题描述】:

是否可以将多个类添加到一个 div 元素?我尝试添加 fullb 类,但它不起作用。

#card { 
/* default for card */
    width: 300px;
    height: 50px;
    padding: 15px;
    background-color: yellow;
    box-shadow: 10px 10px 40px grey;
}

#card.g {
    background-color: lightgreen;
    box-shadow: 10px 10px 40px grey;
}
#card.b {
    background-color: lightblue;
    box-shadow: 10px 10px 40px grey;
}

#card.full{
    width:100%;
    height:100%;
    padding:10px;
}
<h3 id="card" class="full" class="b">Hi!</h3>

如果这是一个不好的问题,我很抱歉,我是编程新手。

【问题讨论】:

  • 目前它不起作用,因为浏览器只呈现第一类属性。
  • 是的,只需添加由空格分隔的不同类。 class="全b"

标签: html css


【解决方案1】:

是的,您只需将多个类用空格分隔即可。如果你在一个元素中输入两次class="",就像你所做的那样,它将忽略第二个

#card { 
/* default for card */
    width: 300px;
    height: 50px;
    padding: 15px;
    background-color: yellow;
    box-shadow: 10px 10px 40px grey;
}

#card.g {
    background-color: lightgreen;
    box-shadow: 10px 10px 40px grey;
}
#card.b {
    background-color: lightblue;
    box-shadow: 10px 10px 40px grey;
}

#card.full{
    width:100%;
    height:100%;
    padding:10px;
}
<h3 id="card" class="full b">Hi!</h3>

【讨论】:

  • 你也可以使用 jquery 或 javascript
  • 谢谢,这很好地解决了我的问题。 :-)
【解决方案2】:

是的,你可以

<h3 id="card" class="full b g">Hi!</h3>

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 2017-01-03
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    相关资源
    最近更新 更多