views:

54

answers:

5

I have something like:

<div id="content>

   <h1>Welcome to Motor City Deli!</h1>
   <div style=" font-size: 1.2em; font-weight: bolder;">Sep 19, 2010</div>
   <div > ... </div>

What is the css selector for the second div (1st div within the "content" div) such that I can set the font color of the date within that div?

+1  A: 

div > div:eq(0) will select the div. first-child is not what you want, by the way.

SimpleCoder
OP wants the *first* `div`, not all of the descendant `<div>s`.
Josh Leitzel
You didn't really answer the question at all. OP specifically asked how to select the `<div>` with the date inside.
Josh Leitzel
@Josh Leitzel. Ah, I see my mistake; Thank you.
SimpleCoder
A: 
#content div { color: # ... ; }

This will only select a div inside an element that has an ID of content.

FreekOne
That's what I thought too, but it didn't work. I'll try it again though.
GregH
If this doesn't work, I'd honestly go with Josh's solution. Just add a class to your date `div` and use that to style it, it's easy and cross-browser, as opposed to the `:first-child` pseudo class, which as it was already pointed out, won't work because `h1` is the first child.
FreekOne
A: 

You want

#content div:first-child {
//css
}
Capt Otis
Won't work because `<div>` is not the first child (it's `<h1>`).
Josh Leitzel
Not to mention it won't work at all in IE even if the date `div` would be the first child.
FreekOne
Really? W3 says it will as long as a doctype is specified. (I honestly don't know though.)
Josh Leitzel
Just ran a test, indeed it works if doctype is specified. In my defence, I wouldn't be surprised if stuff started working in IE if you'd type <the_cake_is_a_lie> at the top of the file ...
FreekOne
I always test before posting an answer. And the cake is a lie.
Capt Otis
@Capt Otis: .. and the poor cube had to die for it :) But it doesn't change the fact that it still wouldn't work, with `h1` being the first child. Didn't test *that*, did you now ?
FreekOne
Haha obviously not. I would honestly use jQuery for this.
Capt Otis
+2  A: 

The closest thing to what you're looking for is the :first-child pseudoclass; unfortunately this will not work in your case because you have an <h1> before the <div>s. What I would suggest is that you either add a class to the <div>, like <div class="first"> and then style it that way, or use jQuery if you really can't add a class:

$('#content > div:first')

Josh Leitzel
+1. I was just about to change my answer to that, after noticing that the `#content` `div` can have multiple `div` children. Speed reading = bad :)
FreekOne
+3  A: 

If we can assume that the H1 is always going to be there, then

div h1+div {...}

but don't be afraid to specify the id of the content div:

#content h1+div {...}

That's about as good as you can get cross-browser right now without resorting to a JavaScript library like jQuery. Using h1+div ensures that only the first div after the H1 gets the style. There are alternatives, but they rely on CSS3 selectors, and thus won't work on most IE installs.

Stan Rogers
+1. Didn't even think about that. Keep in mind this solution won't work in IE6.
Josh Leitzel