Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

applying css to tables

Status
Not open for further replies.

smsinger3

Programmer
Oct 5, 2000
192
US
Hello. I have a question about applying css to table cells. How can I specify a style for an individual cell in this table in a .css. Keep in mind I can't add a new class, or id to anywhere else in the table. Here's the table:

Code:
<table class=MyTable>
<tr>
<td>I want my width 10%</td>
<td>I want my width 50%</td>
<td>I want my width 40%</td>
</tr>
</table>

My guess the css would look something like this:

Code:
.MyTable td { width: 10%; }
.MyTable td td { width: 50%; }
.MyTable td td td { width: 40%; }

Am I close to being right? Thank you very much for your help!


 
I add classes to tds all the time.

I thought that was the beauty of css! Overriding...

[cheers]
Cheers!
Laura
 
Given your example:

Code:
<table class=MyTable>
    <tr>
        <td>I want my width 10%</td>
        <td>I want my width 50%</td>
        <td>I want my width 40%</td>
    </tr>
</table>

If you wanted to access any specifc cell without using IDs, you might be able to use the " x + y " pattern in your selector:

Code:
table.MyTable > tr {
    /* Should match only the first td */
}

table.MyTable > tr + td {
    /* Should match only the second td */
}

table.MyTable > tr + td + td {
    /* Should match only the third td */
}

You can find out more about selectors here:


Be aware of 3 things:

- Not all browsers support that syntax.

- The above CSS matches the HTML markup as it appears onscreen, but most browsers will insert a tbody element, so you might need to take that into account in your selector (adding something like " > tbody " before the tr.

- The above CSS only takes into account one row. You would have to modify it in a similar manner to access each specific row.

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]
 
Oops - I knew I had the CSS right first time:

Code:
table.MyTable > tr + td {
    /* Should match only the first td */
}

table.MyTable > tr + td + td  {
    /* Should match only the second td */
}

table.MyTable > tr + td + td + td  {
    /* Should match only the third td */
}

:eek:)

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top