473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Adjacent Sibling Selectors in CSS

I'm not sure if this is the right place to ask this question, but I wasn't
sure where else to go. I have a table made of the following tags:

<table class="myclass">
<tbody>
<tr><td>.</td></tr>
<tr><td>.</td></tr>
<tr><td>.</td></tr>
</tbody>
</table>

The <tbody>.</tbodysection is repeated several times, so I do not want to
individually specify a style attribute or class in all instance, so I
decided to use a stylesheet. Here are the selectors I have:

..myclass{}
..myclass tbody{}
..myclass tbody tr{}
..myclass tbody tr td{}
..myclass tbody tr td *{}

These selectors work fine when you want the same style for all rows in the
tbody, but I want to specify different styles for the first, second, and
third tr tags (and their descendants, the td tag and anything inside the td
tag). I would think the adjacent sibling selector would be the solution for
this, giving me the following selectors:

For the second tr tag:
..myclass tbody tr+tr{}
..myclass tbody tr+tr td{}
..myclass tbody tr+tr td *{}
For the third tr tag:
..myclass tbody tr+tr+tr{}
..myclass tbody tr+tr+tr td{}
..myclass tbody tr+tr+tr td *{}

However, this does not seem to be working. It looks like the selectors I am
using for the second and third tr tags is being ignored, and the ones for
the first tr tag are being used (which is what I would expect if I did not
have the others). Am I doing something wrong? Is there a different selector
I should be using to style the second and third tr tags? Any help would be
appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Jun 27 '08 #1
5 1731
On Jun 23, 10:02*am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
I'm not sure if this is the right place to ask this question, but I wasn't
sure where else to go. I have a table made of the following tags:

<table class="myclass">
* * <tbody>
* * * * <tr><td>.</td></tr>
* * * * <tr><td>.</td></tr>
* * * * <tr><td>.</td></tr>
* * </tbody>
</table>

The <tbody>.</tbodysection is repeated several times, so I do not want to
individually specify a style attribute or class in all instance, so I
decided to use a stylesheet. Here are the selectors I have:

.myclass{}
.myclass tbody{}
.myclass tbody tr{}
.myclass tbody tr td{}
.myclass tbody tr td *{}

These selectors work fine when you want the same style for all rows in the
tbody, but I want to specify different styles for the first, second, and
third tr tags (and their descendants, the td tag and anything inside the td
tag). I would think the adjacent sibling selector would be the solution for
this, giving me the following selectors:

For the second tr tag:
.myclass tbody tr+tr{}
.myclass tbody tr+tr td{}
.myclass tbody tr+tr td *{}
For the third tr tag:
.myclass tbody tr+tr+tr{}
.myclass tbody tr+tr+tr td{}
.myclass tbody tr+tr+tr td *{}

However, this does not seem to be working. It looks like the selectors I am
using for the second and third tr tags is being ignored, and the ones for
the first tr tag are being used (which is what I would expect if I did not
have the others). Am I doing something wrong? Is there a different selector
I should be using to style the second and third tr tags? Any help would be
appreciated. Thanks.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/
Whats wrong if you set two different class for your tr...
.str
{
}
.ttr
{
}

<table class="myclass">
<tbody>
<tr><td>.</td></tr>
<tr class='str'><td>.</td></tr>
<tr class='ttr'><td>.</td></tr>
</tbody>
</table>

some thing like that...

Best of luck

Munna
www.munna.shatkotha.com
www.munna.shatkotha.com/blog
www.shatkotha.com
Jun 27 '08 #2
as i understand the selector-syntax the selector for row one also
applies to rows two and three, and the selector for row two also
applies to row three.

e.g. a selector like "body span {}" applies to all span-elements in
the following hirarchy, regardless of where they stand. "body div
span{}" applies to span-element four and five and "body div div
span{}" just applies to span-element five.
<body>
<span></span>
<span></span>
<span></span>
<div>
<span></span>
<div>
<span></span>
</div>
</div>
</body>

to be honest i don't think there is a way to select a specific element
among siblings by just using the ancestor- and sibling-selectors.

i don't know what you are aiming at but, depending on the case, you
can try the following:
..use headerrow and footerrow-elements thead and tfoot for a table
(google).
..use id-or attribute-selectors to select a sepcific row.
..use class-selectors to select every first, second or third row.
Jun 27 '08 #3
I'm not going to say you're wrong about there not being a way to select a
specific element among siblings, since I have never used them, but if you
are right then what exactly is the purpose of the adjacent sibling selector?
Could you give me an example in which the adjacent sibling selector is used?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Arthur Milfait" <am*@gmx.infowrote in message
news:96**********************************@t54g2000 hsg.googlegroups.com...
as i understand the selector-syntax the selector for row one also
applies to rows two and three, and the selector for row two also
applies to row three.

e.g. a selector like "body span {}" applies to all span-elements in
the following hirarchy, regardless of where they stand. "body div
span{}" applies to span-element four and five and "body div div
span{}" just applies to span-element five.
<body>
<span></span>
<span></span>
<span></span>
<div>
<span></span>
<div>
<span></span>
</div>
</div>
</body>

to be honest i don't think there is a way to select a specific element
among siblings by just using the ancestor- and sibling-selectors.

i don't know what you are aiming at but, depending on the case, you
can try the following:
.use headerrow and footerrow-elements thead and tfoot for a table
(google).
.use id-or attribute-selectors to select a sepcific row.
.use class-selectors to select every first, second or third row.

Jun 27 '08 #4
your selectors can be simpified:

table.myclass tr td {first row & default.. }
table.myclass tr+tr td {second row only .. }
table.myclass tr+tr+tr td {thirs row only .. }

you can even remove the table if myclass is only used for tables.

unfortunately you will need to wait for next release of IE for full support
of sibling (adjacent) selectors. although this will work fine for all other
popular browsers.

-- bruce (sqlwork.com)
"Nathan Sokalski" wrote:
I'm not sure if this is the right place to ask this question, but I wasn't
sure where else to go. I have a table made of the following tags:

<table class="myclass">
<tbody>
<tr><td>.</td></tr>
<tr><td>.</td></tr>
<tr><td>.</td></tr>
</tbody>
</table>

The <tbody>.</tbodysection is repeated several times, so I do not want to
individually specify a style attribute or class in all instance, so I
decided to use a stylesheet. Here are the selectors I have:

..myclass{}
..myclass tbody{}
..myclass tbody tr{}
..myclass tbody tr td{}
..myclass tbody tr td *{}

These selectors work fine when you want the same style for all rows in the
tbody, but I want to specify different styles for the first, second, and
third tr tags (and their descendants, the td tag and anything inside the td
tag). I would think the adjacent sibling selector would be the solution for
this, giving me the following selectors:

For the second tr tag:
..myclass tbody tr+tr{}
..myclass tbody tr+tr td{}
..myclass tbody tr+tr td *{}
For the third tr tag:
..myclass tbody tr+tr+tr{}
..myclass tbody tr+tr+tr td{}
..myclass tbody tr+tr+tr td *{}

However, this does not seem to be working. It looks like the selectors I am
using for the second and third tr tags is being ignored, and the ones for
the first tr tag are being used (which is what I would expect if I did not
have the others). Am I doing something wrong? Is there a different selector
I should be using to style the second and third tr tags? Any help would be
appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Jun 27 '08 #5
i don't say that i am perfectly sure that i am right either.

a case where sibling-selection might be usefull would be an article in
which the first paragraph after a headline shall be formatted
differently than the other paragraphs - assuming that the headline-tag
and the paragraph-tag, lets say a div-tag, are siblings.

<div id="article">
<h1>Headline</h1>
<div>first paragraph</div>
<div>second paragraph</div>
</div>

selector:
#article h1+div{}
arthur
----------------------------------------------------
I'm not going to say you're wrong about there not being a way to
select a
specific element among siblings, since I have never used them, but if
you
are right then what exactly is the purpose of the adjacent sibling
selector?
Could you give me an example in which the adjacent sibling selector is
used?
Thanks.
--
Nathan Sokalski
njsokal...@hotmail.com
http://www.nathansokalski.com/

"Arthur Milfait" <a...@gmx.infowrote in message

news:96**********************************@t54g2000 hsg.googlegroups.com
----------------------------------------------------
Jun 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Rohleder | last post by:
Hi, I'm using a style-sheet where I make use of the XPATH-"following-sibling"-expression. The part which makes problems looks similar to the following code: --------------------------- ...
2
by: Michael K?nig | last post by:
Hello, I've an XML-file structured like this <table> <tr> <td>Nombre:</td> <td>Joseph</td> <td>Apellido:</td> <td>Ratzinger</td>
1
by: Tony Benham | last post by:
I have been getting to grips with css recently (very slowly), and one area I have a problem is when to use class selectors or id selectors. Are there any guidelines when to use each type ? The key...
4
by: Stan Brown | last post by:
Brian Wilson's Index Dot CSS at <http://www.blooberry.com/indexdot/css/supportkey/syntax.htm> says that no version of MSIE supports adjacent sibling selectors, such as p+p. Is that actually...
4
by: Andy Fish | last post by:
hi, I am using descendent selectors to format cells within a table according to the css class of the table. However, when using nested tables, it seems to pick up the outer matching rule rather...
0
by: Elaine | last post by:
I have a truly curious problem with HtmlHelp and Sibling Mode in Visual C++ ..Net 2003 in an MFC app. Sibling mode allows the help viewer to display on top of the app, but if the app is clicked,...
2
by: sgarfunkle | last post by:
I have a complex document that goes several heading levels deep, and it's unreadable with all the headings and paragraphs against the left margin. I'd like to indent H2 elements by 50 pixels, H3...
11
by: localpricemaps | last post by:
i have some html which looks like this where i want to scrape out the href stuff (the www.cnn.com part) <div class="noFood">Cheese</div> <div class="food">Blue</div> <a class="btn" href =...
3
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.