matching nth child

J

Jon Slaughter

Is there any way to match the nth child in css < 3? Obviously I can do
first-child but I can't seem to get any other siblings ;/
I tried something .myclass:first-child + div

for something like

<div class="myclass">
<div>First child</div>
<div>Second child</div>
</div>

but it doesn't work ;/

Thanks,
Jon
 
J

Jon Slaughter

Jon Slaughter said:
Is there any way to match the nth child in css < 3? Obviously I can do
first-child but I can't seem to get any other siblings ;/
I tried something .myclass:first-child + div

for something like

<div class="myclass">
<div>First child</div>
<div>Second child</div>
</div>

but it doesn't work ;/

Thanks,
Jon

BTW, I know above int he example one could do

..myclass>div + div

but that is not what I'm looking for as its not very robust. (if I end up
adding a third child div then it screws up everything.
 
J

Jon Slaughter

Jon Slaughter said:
Is there any way to match the nth child in css < 3? Obviously I can do
first-child but I can't seem to get any other siblings ;/
I tried something .myclass:first-child + div

for something like

<div class="myclass">
<div>First child</div>
<div>Second child</div>
</div>

but it doesn't work ;/

Thanks,
Jon

The main thing I need is a second-child like selector and not really an
nth-child and I want to avoid having to specify the class of the the second
div.
 
J

Jon Slaughter

Jon Slaughter said:
Is there any way to match the nth child in css < 3? Obviously I can do
first-child but I can't seem to get any other siblings ;/
I tried something .myclass:first-child + div

for something like

<div class="myclass">
<div>First child</div>
<div>Second child</div>
</div>

but it doesn't work ;/

Thanks,
Jon

Another this that I could do is first-child of first child but when I do
something like myclass:first-child:first-child it does not select the right
div.

basically doing it this way instead

<div class="myclass">
<div>First child
<div>Second child</div>
</div></div>


But the selector doesn't get the second child ;/
 
D

dorayme

"Jon Slaughter said:
Is there any way to match the nth child in css < 3? Obviously I can do
first-child but I can't seem to get any other siblings ;/
I tried something .myclass:first-child + div

for something like

<div class="myclass">
<div>First child</div>
<div>Second child</div>
</div>

but it doesn't work ;/

Thanks,
Jon

Last child works in some browsers. But why trust any of it, just
class things and be safe.

Try this in different browsers:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<style type="text/css">
ul li:first-child {color: blue}
ul li:last-child {color: red}
</style>
</head>
<body>
<ul>
<li>text</li>
<li>text</li>
<li>text</li>
<li>text</li>
</ul>
</body>
</html>

Nice in FF.
 
D

dorayme

"Jon Slaughter said:
The main thing I need is a second-child like selector and not really an
nth-child and I want to avoid having to specify the class of the the second
div.

Try ol li:first-child + li {...}

I modify my last to include the idea:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<style type="text/css">
ol li:first-child {color: blue}
ol li:last-child {color: red}
ol li:first-child + li {color: yellow}
</style>
</head>
<body>
<ol>
<li>text</li>
<li>text</li>
<li>text</li>
<li>text</li>
</ol>
</body>
</html>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top