【问题标题】:I can't seem to get css first_child to work我似乎无法让 css first_child 工作
【发布时间】:2014-12-13 22:13:58
【问题描述】:

通常,这是一项非常简单的任务。由于某种原因,今天,它似乎不想工作。有什么我做错了吗?问这个问题我可能听起来很傻。 :)

这是css代码:

#header > .navigation {
    display: inline-block;
}
    #header > .navigation > a {
        text-decoration: none;
        color: #202020;
    }
        #header > .navigation > a:first_child { /* This is what isn't working */
            color: #ff0000;
        }

这是html代码:

<div id="header">
    <div class="navigation">
        <a href="#">Start</a>
        <a href="#">Account</a>
        <a href="#">Contact</a>
        <a href="#">Whoah, what is this place?</a>
    </div>
</div>

.navigation 显示为内联块有问题吗?它必须保持类似的显示模式,因为在#header 中它旁边还有另一个div。我没有在上面的代码中显示这个 div,以使这个消息尽可能简单易懂。

【问题讨论】:

  • 第一个子元素带有破折号,而不是下划线。
  • 是的,我感谢大家帮助我解决我的愚蠢错误!

标签: html css css-selectors


【解决方案1】:

代替:

#header > .navigation > a:first_child

用途:

#header > .navigation > a:first-child

【讨论】:

  • 有区别吗?
  • 是的。第一个孩子而不是_
  • 傻我...谢谢!
【解决方案2】:

错误已解决

a:first-child{ ... }

匹配&lt;a&gt; 元素,它们是其父元素的第一个子元素。您的元素位于完全不同的&lt;li&gt;-elements 中,因此它们都是其父元素的第一个子元素。

参考: a:first-child modifying all links within my list

【讨论】:

  • 这个答案看起来很奇怪。 “已解决的错误”几乎是唯一与该问题相关的文本,即使它看起来如此格格不入。从链接中获取的其余文本根本不适用于此处。
【解决方案3】:

立即尝试

这是css代码:

#header > .navigation { display: inline-block; }
#header > .navigation > a { text-decoration: none; color: #202020; }     
#header > .navigation > a:first-child { /* dash not underscore */ color: #ff0000; }

【讨论】:

  • 看起来你过度分析了这个@foru
猜你喜欢
  • 2011-11-30
  • 2015-09-20
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多