Kyrbi
Kyrbi

Reputation: 2250

First-child is not working in my code

I don't know why my a:first-child is not working. Here is my 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>

and this is my 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;
}

I just want to have first a to be 36px size. Can anyone help why does it affect other as? Here is my jsfiddle http://jsfiddle.net/dsT4Z/

Here is what I see. I added red arrows to show which as I don't want to be that sized and etc.

enter image description here

Upvotes: 0

Views: 101

Answers (3)

henryaaron
henryaaron

Reputation: 6192

I think you should consider using the :nth-of-type() selector. It seems more appropriate for this application.

first-child will just select whatever the first child is. With nth-of-type you're guaranteeing selection of the first immediate anchor child of the header element.

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

jsFiddle.
Documentation.

Upvotes: -1

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

Looks to me like you only want the first <a> which is immediately nested the <header> to be targeted. Change the a:first-child rule to this:

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

That way only the first immediate child of the <header> should be affected. http://jsfiddle.net/dsT4Z/1/

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

All of the <a> tags in the navigation are are the first child of the containing <li> tags, and therefore match the rule.

The one in the text area is the first (element) child of the containing <p>, and therefore matches the rule.

It would be far easier to use an ID or Class name on the element you actually want to affect.

Upvotes: 2

Related Questions