0

我不知道为什么我a:first-child的不工作。这是我的html:

<!DOCTYPE html>
<link href='http://fonts.googleapis.com/css?family=Geo' rel='stylesheet'>
<title>Mira's place</title>
<header>
    <a href="index.html">Mira's place</a><br>
    <h2>&#8220;<span id="quote">If nothing, then this.</span>&#8221;</h2>
        <ul>
            <li><a href="#">Home</a>
            <li><a href="#">Games</a>
            <li><a href="#">Pixel Art</a>
            <li><a href="#">Contact</a>
        </ul>
</header>
<section>
    <h2>This is test H2</h2>
    <p>This is lorem ipsum shitsum, blah <a href="#">Test</a> blah baldflksafdjsl adslkf ajfdlks ajfldsa jflksda lfhsdalf jdsalfh sdlfdshfdsk fjsdl ajfl
</section>
<footer>
    <p>(C) MiraCZ 2013 - You can copy anything here! 
</footer>

这是我的 CSS

html {
    background-color: #002240;
    font-family: "Geo", sans-serif;
    margin: 0;
}
header {
    background-color: #001629;
    margin: 0 -10px 0 -10px;
}
a:first-child {
    color: #FF628C;
    font-size: 36px;
}
header h2 {
    color: #80FFBB;
}
li {
    display: inline-block;
    font-size: 18px;
}
p {
    color: white;
}

我只想先a设置为 36px 大小。任何人都可以帮助为什么它会影响其他人a?这是我的 jsfiddle http://jsfiddle.net/dsT4Z/

这是我所看到的。我添加了红色箭头来显示a我不想变得那么大等等。

在此处输入图像描述

4

3 回答 3

2

导航中的所有<a>标签都是包含<li>标签的第一个子标签,因此与规则匹配。

文本区域中的元素是包含的第一个(元素)子元素<p>,因此与规则匹配。

在您实际想要影响的元素上使用 ID 或类名称会容易得多。

于 2013-04-13T17:57:02.530 回答
1

在我看来,您只希望<a>立即嵌套的第<header>一个成为目标。a:first-child将规则更改为:

/* only target an <a> element which is the first immediate child of a <header> element */
header > a:first-child {
    /* ... */
}

这样,只有第一个直接孩子<header>应该受到影响。http://jsfiddle.net/dsT4Z/1/

于 2013-04-13T17:58:36.123 回答
-1

我认为您应该考虑使用:nth-of-type()选择器。它似乎更适合此应用程序。

first-child只会选择第一个孩子是什么。nth-of-type你保证选择标题元素的第一个直接锚子元素。

header > a:nth-of-type(1) {
color: #FF628C;
font-size: 36px;
}

js小提琴
文档

于 2013-04-13T18:22:05.273 回答