CSS body:first-child

css, css-selectors, html

Solution

To target the first div, you need to do `body div:first-child`. Right now (I assume) you're just selecting the first-child body. (Actually I'm not entirely sure what you're selecting right now, come to think of it. I don't think the `first-child` selector is valid to hang directly on the body tag.)

body div:first-child {
    color:#f00;
}​

This CSS will color it as you expect. Read it as "the div that is the first child of body."

Problem

Can someone explain to me why this doesn't work? ``` <html> <head> <style> body:first-child { color:#f00; } </style> </head> <body> <div>I should be red.</div> <div>This is not red.</div> </body> </html> ``` From what I've read, the first-child selector should select the first div object from the body tag. If it's not selecting the div element, what is it selecting?

Original source