Forum Moderators: not2easy

Message Too Old, No Replies

Contextual selectors?

Contextual selectors

         

nbsp

4:32 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



I've established the follwing CSS code:

.LoginTable { }
.BillingTable { }

.LoginTable .Col1 { width: 200px; }
.LoginTable .Col2 { width: 400px;}

.BillingTable .Col1 { }
.BillingTable .Col2 { }

The thing I am noticing is that my .BillingTable .Col1 is rendering the width of .LoginTable .Col1.

My understanding was that since .LoginTable .Col1 and .BillingTable .Col1 are specifically assigned via a contextual selector, the values shouldn't be inherited. Can someone clarify this concept and what's actually happening here?

DrDoc

4:53 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld! [WebmasterWorld.com]

The billing table doesn't happen to reside inside the login table, does it?

nbsp

5:11 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



No it does not. In fact they are on seperate pages. (both pages call the same stylesheet)

The code is essentially as follows:
<style type="text/css">

.LoginTable { }
.BillingTable { }

.LoginTable .Col1 { width: 200px; }
.LoginTable .Col2 { width: 400px;}

.BillingTable .Col1 { }
.BillingTable .Col2 { }

</style>

<table class="LoginTable">
<tr>
<td class="Col1">Login Name:</td>
<td class="Col2"><input type="text" name="LoginName"></td>
</tr>
</table>

<table class="BillingTable">
<tr>
<td class="Col1">First Name:</td>
<td class="Col2"><input type="text" name="FirstName"></td>
</tr>
</table>

My thought was that despite that fact that the td tags are both assigned the same class name, since they are assigned specifically with their respective parent tables, they should be completly independent.

But, that doesn't seem to be the case. Thoughts?

nbsp

5:21 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



Actually I just tired the exact code I posted above and it actually works correctly.

Maybe nesting does have something to do with it?

The following code actually exhibits the effect that I am noticing:

<style type="text/css">

.LoginTable { }
.BillingTable { }
.SpanCol { }

.LoginTable .Col1 { width: 200px; }
.LoginTable .Col2 { width: 400px;}

.BillingTable .Col1 { }
.BillingTable .Col2 { }

</style>

</head>

<body>

<table class="LoginTable">
<tr>
<td class="Col1">&nbsp;</td>
<td class="Col2">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="SpanCol"><table border="1" class="BillingTable">
<tr>
<td class="Col1">First Name:</td>
<td class="Col2"><input type="text" name="FirstName" /></td>
</tr>
</table></td>
</tr>
<tr>
<td class="Col1">&nbsp;</td>
<td class="Col2">&nbsp;</td>
</tr>
<tr>
<td class="Col1">Login Name:</td>
<td class="Col2"><input type="text" name="LoginName"></td>
</tr>
</table>

I'm not understanding why .BillingTable .Col1 and .BillingTable .Col2 are exihibiting the same formatting as the one set for .LoginTable.

garann

8:57 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



Sometimes, to clear up this kind of confusion, I've had success specifying the type of element the class belongs to, so:

table.LoginTable td.Col1 { width: 200px; }
table.LoginTable td.Col2 { width: 400px;}

table.BillingTable td.Col1 { }
table.BillingTable td.Col2 { }

Maybe it will work for you, too. If not, best of luck!

g.

DrDoc

10:00 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not understanding why .BillingTable .Col1 and .BillingTable .Col2 are exihibiting the same formatting as the one set for .LoginTable

That's because:
.LoginTable .Col1 means:
any element with a class attribute that contains the word Col1 that is a descendant of any element with a class attribute that contains the word LoginTable

And, if .BillingTable resides somewhere inside .LoginTable ... and there is a .Col1 element anywhere inside .LoginTable (regardless of how many levels deep) the style will be applied. Like in this case, where .BillingTable is a child of .LoginTable, and .Col1 is a child of .BillingTable (thus a grandchild of .LoginTable).

What you actually want is:
.LoginTable > .Col1
...which requires .Col1 to be a child of .LoginTable for the styles to apply.

SelectORacle [penguin.theopalgroup.com]

DrDoc

10:06 pm on Dec 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To explain it further, consider the following:

.bar p { 
color: blue;
}
.foo p {
color: red;
}
.foo .bar p {
color: green;
}

<div class="foo">
<p>red, since .foo p applies</p>
<div class="bar">
<p>green, since .foo .bar p applies</p>
</div>
</div>
<div class="bar">
<p>blue, since .bar p applies</p>
<div class="foo">
<p>red, since .foo p applies</p>
</div>
</div>

kk5st

10:23 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



In

<table class="LoginTable">
<tr>
<td class="Col1">&nbsp;</td>
<td class="Col2">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="SpanCol">
<table border="1" class="BillingTable">
<tr>
<td class="Col1">First Name:</td>
<td class="Col2"><input type="text" name="FirstName" /></td>
</tr>
</table>
</td>
</tr>

The td.Col1 of table.BillingTable is also a descendent of table.LoginTable. Since table.BillingTable td.Col1 {} does not overrule the earlier rules, the table.LoginTable rules apply. You will need to distinguish the second group a little more specifically.

cheers,

gary