【问题标题】:Dilemma in deciding how to create CSS for H1, H2, H3 etc决定如何为 H1、H2、H3 等创建 CSS 的困境
【发布时间】:2009-03-15 04:21:32
【问题描述】:

我的网站目前有一些通用的 H1、H2、H3 样式,它们非常适合大多数需要简单“传统”标题的“通用”标题。

 h1 { /* lots of style attributes */ }
 h2 { /* lots of style attributes */ }
 h3 { /* lots of style attributes */ }

我也在创建一些组件,我有这样的东西,也就是说我需要一个特定于该特定类型控件的标题。

 <div class="titledimage"><h1>Section header</h1><img .../></div>

 .titledimage h1 { color:red; bottom-border: 1px solid blue; }

我遇到的问题是titledimage下的h1也是前面定义的h1,所以它继承了h1定义的所有样式。这通常是不希望的 - 我只想要 red1px solid blue 作为 .titledImage div 中的标题。

我正在阅读并尝试回答 this question about H1 styles。我的结论是,如果你正在做特定的标题样式(.titledimage h1),你不能真正做通用的标题样式(h1),除非:

 a) you reset every style attribute in the '.titledimage h1' style
 b) you just use a class name instead of h1
 c) your `h1` style is defined with very few attributes that you'd 
    be overriding anyway

我注意到他们实际上使用H6 来设置YUI menu control 的样式,我想知道他们是否这样做是为了避免此类冲突。

我应该

a) be using <h6> like yahoo does?
b) reset every attribute defined by `h1` when I define `.titledimage h1` ?
c) just use a class name for `.titledimage header`, and leave 
   `h1`, `h2`, `h3` for 'traditional more logical headers' 
d) something else

理想情况下我想说这个,但没有这样的事情(据我所知)

 .titledimage h1 { inherit: none; color:red; bottom-border: 1px solid blue; }

【问题讨论】:

  • @daniel - 完成。它在我想要它之前发布,但我不确定欺骗是如何发生的。
  • 你能发布你的 HTML 的一般结构吗?在大多数情况下,我认为拥有很多 H1 并不是很有用。
  • @strager - 你看到问题 503925 了吗?所以你建议什么 - h2 ;-) ?我倾向于 yahoo 对他们的
    所做的事情。如果它是合法的标题,它至少应该是 H 的东西。这些天我需要安装一个屏幕阅读器,看看可访问性是如何工作的
  • @Simon,通常,标题应该能够创建目录。 (Opera:View->Style->ToC)SO 不是一个很好的例子,但是 Wikipedia 是。
  • @strager - 好点。看看雅虎的主页(我假设你得到了新版本)。他们有带有标题的面板,例如“今日热门搜索”。这些就是我正在谈论的事情。这些可能是目录的好候选

标签: css


【解决方案1】:

对我来说,重置似乎很浪费。必须有一种干净的方法将/* lots of style attributes */ 应用到您想要应用它的h1 标签,而不是将它应用到.titledimage 中的h1

说你有:

<div class="top"><h1>PageName</h1></div>
<div class="leftNavigation"><h1>Cat1</h1><h1>Cat2</h1><h1>Cat3</h1></div>
<div class="rightMarginNote"><h1>Username</h1></div>
<div class="content">
 <h1>CONTENT</h1>
 <div class="titledimage">
  <h1>title</h1>
 </div>
</div>

那么你会希望你的 CSS 有点像:

.top h1, .leftNavigation h1, .rightMarginNote h1, .content > h1 {
  /* lots of style attributes */
}
.similarly h2 { /* lots of style attributes */ }
.similarly h3 { /* lots of style attributes */ }
.titledimage h1 { color:red; bottom-border: 1px solid blue; }

替代方案

h1 { /* lots of style attributes */ }
h2 { /* lots of style attributes */ }
h3 { /* lots of style attributes */ }
.titledimage h1, .otherCase h1, .anotherCase h1, yetAnotherCase h1 {
  /* lots of style backtracking */
}
.titledimage h1 { color:red; bottom-border: 1px solid blue; }
.otherCase h1 { color:blue; bottom-border: 1px solid blue; }
.anotherCase h1 { color:green; bottom-border: 1px solid blue; }
.yetAnotherCase h1 { color:mauve; bottom-border: 1px solid blue; }

还尽可能多地将 H1-H5 内容组合在一起,如果您必须使用替代方案,请定义一个专门用于重置的类,该类不应用于 h1,而是应用于任何类的包含 div。

<div class="titledimage hReset"><h1>title</h1></div>

【讨论】:

    【解决方案2】:

    简单的解决方案是不要嵌套 h1 标签,即:

    <div class="different-header-style">Some Header</div>
    

    如果 h1 没有您想要的样式,那么为什么要使用它?

    另外,比将它嵌套在一个 div 中只是为了样式我会这样做:

    <h1 class="special-header">Some Header</h1>
    

    h1 是一个块元素,您可以像 div 一样设置样式(边框、宽度等)。

    【讨论】:

    • @cletus - 我喜欢

      的简洁性。从逻辑上讲,它仍然是一个标题,我仍然想称它为一个标题。我链接到的问题中的答案也同意这是一个很好的做法。我确实想知道

      但这也有点笨拙 - 但可能是这里最好的解决方案

    【解决方案3】:

    b) 重置每个定义的属性 h1 当我定义 .titledimage h1 时?

    这种方法的问题是您不能将重置样式重用于具有特定样式的其他标题,例如

    <div class="titledimage"><h1>Section header</h1><img .../></div>
    <div class="titledimage"><h2>Section header</h2><img .../></div>
    <div class="otherimage"><h1>Section header</h1><img .../></div>
    

    我认为更好的方法是在单独的类中定义您的重置

    .hreset {
      /* Reset the header styles here */
    }
    

    然后,您可以在必要时重复使用这些重置,例如

    <div class="titledimage"><h1 class="hreset">Section header</h1><img .../></div>
    <div class="titledimage"><h2 class="hreset">Section header</h2><img .../></div>
    <div class="otherimage"><h1 class="hreset">Section header</h1><img .../></div>
    

    【讨论】:

    • @don - 看起来有点毛骨悚然,但我喜欢你拥有的 BBC 猫头鹰图标 :-) 带我回去......
    • 你可以把 hreset 类放在 div 上: .hreset h1 { /* reset */ }
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2013-06-15
    • 2023-03-06
    • 2012-09-17
    • 1970-01-01
    相关资源
    最近更新 更多