html - 优先考虑 CSS 类?

标签 html css

我正在使用 Twitter 的 Bootstrap 库 2.0.3 版来制作网站原型(prototype)。我目前正在使用提供的模态控件(尽管是内联的)来显示表单:

<div class="modal in-layout span6 offset3">
    <div class="modal-header">
        <h3>Header</h3>
    </div>
    <div class="modal-body">
        <p>Hello</p>
    </div>
    <div class="modal-footer">

    </div>
</div>

我编写了自己的选择器来使用我的自定义类 .in-layout :

div.modal.in-layout {
    position: static;
    top: auto;
    left: auto;
    margin-top: 10px;
    margin-bottom: 10px;
}

问题在于 CSS 规则的匹配顺序非常不方便:

enter image description here

本质上,我需要 .span6.offset3优先级高于 .modal 的类为了给我<div>边距为 260px,宽度为 460px。

有什么办法可以做到这一点吗?

最佳答案

有 3 种方法可以使规则覆盖另一个规则(从最强到最弱):

  1. 重要性,例如作者样式表中的规则将覆盖用户代理样式表中指定的规则,使用 !important 规则将覆盖任何其他规则。
  2. 特定性,规则将覆盖任何其他具有不太特定的选择器的规则。 <强> Here's an article on the subject. 基本上,element.class.class 更具体,但不如 #id 具体。
  3. 顺序,规则将覆盖之前的任何其他规则。

除此之外,我还发现了您的代码的其他一些问题。设计网站时,最好使用语义定义(“这意味着什么”)而不是形式相关术语(“这应该是什么样子”)。

要具体引用您的代码,正在命名您的类名 span6in-layoutoffest3,您可能应该在其中包含一些内容像 warning-modalinformation-headererror-footer,它们传达元素的含义(毕竟 HTML 的唯一目的是提供上下文到您的文本)。

一旦您用这些术语思考,为您的元素设计样式就会容易得多。

关于html - 优先考虑 CSS 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10557566/

相关文章:

html - 悬停时删除CSS中的圆圈

javascript - 我试图从特定 div 的元素中获取所有内容,然后验证它们

css - 为什么 <a> 标签在应用悬停背景时给出与 <li> 标签不同的输出?

html - 如何使 <span> 文本在第一行开头下方很好地换行

jquery - 如何在 jQuery 中获取变换矩阵和 matrix3d 值?

使按钮标题加粗的CSS

html - Div 高度在 IE8 中没有改变

html - 位置为 :fixed not respnding to margin-right 的页脚

css - Codekit 和 Zurb Foundation 错误

html - 在 Angular 中自定义 NG Bootstrap 轮播