【问题标题】:a tag with id and css带有 id 和 css 的标签
【发布时间】:2016-05-23 17:42:32
【问题描述】:

我正在尝试输出一些具有特定悬停/活动/链接属性的链接:

a#links:link {
  color: red;
  text-decoration: none
}
a#links:visited {
  color: goldenrod;
  text-decoration: none
}
a#links:hover {
  color: maroon;
  text-decoration: none;
  font-variant: small-caps
}
<div id="links" class="right" style="margin-right:65px">
  <ul class="nobull" style="margin:0">
    <li><a href="http://com">This link</a>
    </li>
  </ul>
</div>

链接属性没有被占用。某处的语法错误?我想不通。

【问题讨论】:

    标签: css hyperlink anchor


    【解决方案1】:

    使用a#links,您可以为所有带有 id 链接的 a 赋予样式,但您希望 div 中的所有 a 带有 id “链接”,因此您必须使用 #links a

    #links a:link{
     color:red;
     text-decoration:none}
    #links a:visited{
     color:goldenrod;
     text-decoration:none}
    #links a:hover{
     color:maroon;
     text-decoration:none;
     font-variant:small-caps}
    <div id="links" class="right" style="margin-right:65px">
     <ul class="nobull" style="margin:0">
      <li><a href="http://com">This link</a></li>
     </ul></div>

    【讨论】:

    【解决方案2】:

    a#links 选择一个不存在的具有链接 ID 的锚点。您需要#links a,它选择作为具有链接 ID 的 div 的后代的锚点。

    #links a:link {
      color: red;
      text-decoration: none
    }
    #links a:visited {
      color: goldenrod;
      text-decoration: none
    }
    #links a:hover {
      color: maroon;
      text-decoration: none;
      font-variant: small-caps
    }
    <div id="links" class="right" style="margin-right:65px">
      <ul class="nobull" style="margin:0">
        <li><a href="http://com">This link</a>
        </li>
      </ul>
    </div>

    【讨论】:

      【解决方案3】:

      没有错误,但它在你的代码中。

      通过在 CSS 文件中声明 a#links,它将寻找一个带有 ID linksa 标签。因此,您需要将id="links" 添加到a 标签,而不是像您那样添加到父div 标签。

      或者,我会做的是定位您的具有 ID 的 div,并将 a 标记作为 CSS 规则中的选择器。

      #links a {
        color: red;
        text-decoration: none
      }
      #links a:visited {
        color: goldenrod;
        text-decoration: none
      }
      #links a:hover {
        color: maroon;
        text-decoration: none;
        font-variant: small-caps
      }
      

      【讨论】:

      • 啊,谢谢!我曾用 #links a:link { ...} 尝试过,但由于其他页面样式没有看到变化。现在可以使用了。
      • :link 并不是真正需要的,因为您可以使用a 来定位它。这是您想要的结果吗?
      猜你喜欢
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多