Css target :first-child... before any text node? (2.1)

R

ReGenesis0

So I've got this thing...

I want to apply css styles to a link if it's the first piece of
content within a LI, but /not/ if it's got anything before it.

<li> <a>Styled!</a> <li>
<li> This is <a>not styled!</a> <li>
I'm targeting the element I want to style with:
li > a:first-child { background-color:pink; }

And that works... insofar that if there are 2 <a>'s, only the first ti
styled, and if there are any child elements before the <a> (like a
<span>) then <a> is not styled.

But-- it doesn't do what i want if there is TEXT before the :first-
child node...
<li> This is <a>not styled!</a> <li>

The <a> is still styled, even though I want it not to be.

Approaching this from a DOM perspective, I would think that text-nodes
would count as children, and disqualify <a> from being a first-
child. ...but this is not the case.

Is there anything I can combine this with to get the effect I
want? :first-letter doesn't seem to fit the bill... but maybe I'm
just not thinking sideways enough?

Am I just out of luck?

-Derik
 
J

Jukka K. Korpela

ReGenesis0 said:
I want to apply css styles to a link if it's the first piece of
content within a LI, but /not/ if it's got anything before it.

You can't, except by using class (or id) attributes for those links.
li > a:first-child { background-color:pink; }

And that works... insofar that if there are 2 <a>'s, only the first ti
styled, and if there are any child elements before the <a> (like a
<span>) then <a> is not styled.

And if IE 6 is not used.
But-- it doesn't do what i want if there is TEXT before the :first-
child node...
<li> This is <a>not styled!</a> <li>

The <a> is still styled, even though I want it not to be.

It works the way you wrote in CSS, not the way you wanted.
Approaching this from a DOM perspective, I would think that text-nodes
would count as children, and disqualify <a> from being a first-
child. ...but this is not the case.

It's not the case. Textual content inside an element does not constitute an
element.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top