代码之家  ›  专栏  ›  技术社区  ›  Fer

忽略CSS文本装饰规则

css
  •  1
  • Fer  · 技术社区  · 15 年前

    我发现了与我相似的问题,但这些建议似乎都不适用于我的情况,所以下面是…

    我有一个网页,上面有很多图片。每个图像都有一个标题,在标记中位于h2标记之间。标题是一个链接,因此生成的标记如下所示:

    <ul class="imagelist">
    <li>
    <a href=""><h2>Title 1</h2></a>
    <a href=""><img src="" /></a>
    </li>
    <li>
    Image 2, etc...
    </li>
    </ul>
    

    我只想要标题链接到 下划线。我试着这样说:

    .imagelist li a h2 { color:#333; text-decoration:none; }
    

    它完全忽略了文本装饰规则,但尊重颜色规则。从其他问题中我了解到,这可能是因为子元素不能推翻其任何父元素的文本装饰。所以,我开始寻找父元素,看看是否应用了任何显式的文本装饰规则。我什么也没找到。

    这让我发疯了,有什么帮助吗?

    为了完整起见,这里是Firebug CSS输出,它显示了完整的继承等等。可能比你想要的要多,但我看不出这里有任何冲突。

    .imagelist li a h2 {
    color:#333333;
    text-decoration:none;
    }
    main.css (line 417)
    h2 {
    font-size:14px;
    }
    main.css (line 40)
    h1, h2, h3, h4, h5, h6 {
    display:block;
    font-weight:bold;
    margin-bottom:10px;
    }
    main.css (line 38)
    h1, h2, h3, h4, h5, h6 {
    font-size:100%;
    font-weight:normal;
    }
    reset-min.css (line 7)
    body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td {
    margin:0;
    padding:0;
    }
    reset-min.css (line 7)
    Inherited froma /apps/ju...mage/745
    a {
    color:#0063E2;
    }
    main.css (line 55)
    Inherited fromli
    .imagelist li {
    list-style-type:none;
    }
    main.css (line 411)
    li {
    list-style:none outside none;
    }
    reset-min.css (line 7)
    Inherited fromul.imagelist
    .imagelist {
    border-collapse:collapse;
    font-size:9px;
    }
    main.css (line 410)
    Inherited frombody
    body, form {
    color:#333333;
    font:12px arial,helvetica,clean,sans-serif;
    }
    main.css (line 36)
    Inherited fromhtml
    html {
    color:#000000;
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   Sarfraz    15 年前

    试试这个:

    .imagelist li a, .imagelist li a:hover{color:#333; text-decoration:none;}
    
        2
  •  1
  •   David Thomas    15 年前

    尝试切换一下HTML,而HTML5允许块级元素在 a 标记,我不确定HTML4.01或XHTML是否有,因此它可能值得:

    ...
    <li><h2><a href="#">title 1</a></h2></li>
    ...
    

    使用CSS:

    a:link,
    a:visited {text-decoration:  none; }
    

    或者,在特殊性成为问题的情况下:

    ul.imagelist li h2 a:link,
    ul.imagelist li h2 a:visited {text-decoration: none; }