如何覆盖CSS类

Car*_*ñiz 2 css

我有一个由Drupal生成的网页,它附带了一个特定的样式表.

我想覆盖其中一种风格.它设置为这样的类:

<div class="description"></div>
Run Code Online (Sandbox Code Playgroud)

因此,我不想使用Drupal CSS附带的".description"样式,而是希望页面使用我的".description"样式.换句话说 - 如果页面有2个".description"样式,我该如何告诉页面使用我的?

Mat*_*all 13

  1. 使用具有更高特异性的选择器:

    div.description {
        /* will take precedence over just .description */
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将样式表放在要覆盖的样式表之后:

    /* Drupal's style */
    .description {
        foo: bar;
    }
    
    /* Your style */
    .description {
        foo: baz; /* takes precedence over foo: bar */
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 不要用!important.只是不要.

另请参见CSS 3:计算选择器特异性.

  • 当我查看生成页面的源代码时,我的样式表是最后一个声明的,而我的 .description 样式不是正在使用的样式。有任何想法吗? (2认同)