tags:

views:

171

answers:

5

I am trying to apply css on the first A element inside .Outer,

 .Outer > a:first-child {font-weight:bold}

doesn't work. Why?

<div class="Outer">
 <img src='image123.jpg' />
 <a href="Default.aspx?ID=4083" id="ctl00_CPH_Main_Rep_List2_ctl03_HyperLink1">John Johnsson</a>
 <a href="../Users/ViewList.aspx?module=Occupation&amp;ID=70">Doctor</a>
 <a href="../Workplaces/Default.aspx?ID=31">Mayo Clinica>
</div>
A: 

try it without the angle bracket. also, the :first-child pseudo class isn't recognized by all browsers.

it's also possible that your CSS is being overridden by an existing rule. use firebug in firefox to see exactly what styles are being applied.

Jason
A: 

What you are doing will only work if the first child of your div.Outer was an a element. :first-child does exactly what it says, it only matches an element if that element is the first child of something.

Unfortunately, I don't think there is any way to select only the first matching child element of an element using CSS only.

Sean Nyman
thx all, thought a:first-child would apply to the first A element only...will check out alternatives!
David Thorisson
I gave you an alternative, check my answer :/
henasraf
@henasraf: definitely check out David's answer, it might be useful if you don't have to support some browsers.
Sean Nyman
Not sure if first-child in jQuery will fail, but you can do something else: `$('.Outer a').first().addClass('first');`
henasraf
+2  A: 

It's because <a> isn't the first child, <img/> is. What you're looking for is .Outer > a:first-of-type or .Outer > a:nth-child(2). Take not these "more advanced" selector don't work in all browsers *coughIEcough*

henasraf
Oh hey, I either forgot about or didn't know those pseudo classes.
Sean Nyman
this is the best solution but it has poor browser support (non IE8, FF3) so I'll have to use other alternatives
David Thorisson
can you maybe use a server-side script to add a class to the first link there?
henasraf
A: 

Because the first child is the image, this would work:

<div class="Outer">
 <a href="Default.aspx?ID=4083" id="ctl00_CPH_Main_Rep_List2_ctl03_HyperLink1">John Johnsson</a>
 <a href="../Users/ViewList.aspx?module=Occupation&amp;ID=70">Doctor</a>
 <a href="../Workplaces/Default.aspx?ID=31">Mayo Clinic</a>
</div>
kekekela
A: 
Cawas