【问题标题】:CSS styling precedence [duplicate]CSS样式优先级[重复]
【发布时间】:2016-08-09 08:04:00
【问题描述】:

我有两个 CSS 类:

.thick-green-border {
border-color: green;
border-width: 10px;
border-style: solid;
border-radius:10px;
}

.smaller-image {
width: 100px;
border-radius:30px;
}

在 HTML 文档中的用法:

<img class="thick-green-border smaller-image" src="https://s3.amazonaws.com/freecodecamp/relaxing-cat.jpg" alt="A cute orange cat lying on its back.">

当图像元素同时分配到两个类时,.smaller-image 类中描述的样式border-radius 的规则生效,而不是thick-green-border 类中设置的半径。那就是图片的边框半径变成了 30px,而不是 10px。

谁能告诉我为什么?我尝试更改将类分配给图像的顺序,但结果是相同的。

【问题讨论】:

    标签: html css


    【解决方案1】:

    更改 CSS 中类的顺序。

    .thick-green-border {
      border-color: green;
      border-width: 10px;
      border-style: solid;
      border-radius: 10px;
    }
    .smaller-image {
      width: 100px;
      border-radius: 30px;
    }
    .smaller-image2 {
      width: 100px;
      border-radius: 30px;
    }
    .thick-green-border2 {
      border-color: green;
      border-width: 10px;
      border-style: solid;
      border-radius: 10px;
    }
    <img class="thick-green-border smaller-image" src="https://s3.amazonaws.com/freecodecamp/relaxing-cat.jpg" alt="A cute orange cat lying on its back.">
    
    <img class="thick-green-border2 smaller-image2" src="https://s3.amazonaws.com/freecodecamp/relaxing-cat.jpg" alt="A cute orange cat lying on its back.">

    它们都具有相同的层次结构级别,因此将应用后者。

    【讨论】:

    • 好的!两个班级处于同一水平是当时的问题!谢谢@Jamie-barker
    【解决方案2】:

    这是你想要的吗?

    .thick-green-border {
    border-color: green;
    border-width: 10px;
    border-style: solid;
    border-radius:10px;
    display: inline-block;
    }
    
    .smaller-image {
    width: 100px;
    border-radius:30px;
    }
    <div class="thick-green-border">
    <img class="smaller-image" src="https://s3.amazonaws.com/freecodecamp/relaxing-cat.jpg" alt="A cute orange cat lying on its back.">
      </div>

    【讨论】:

    • OP想要覆盖.thick-green-border设置的边框半径,而不是两个边框。
    • 好吧,我误解了他的意思
    • 我想要由“thick-green-border”类设置的边框。 @cHao 做对了。
    • 那你为什么不直接删除 .smaller-image 中的边框样式呢?
    • 我明白你的意思。我可以很好地做到这一点。但我只是想知道为什么第二类的边框样式有效,而不是第一类。我完全忘记了他们在同一级别@GabrielChiHongLee
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    相关资源
    最近更新 更多