2

I have two tables with same class

<table class='pp' >
  <tr class='pp'>
    <td class='pppagetitle'>A Table</td>
  </tr>
</table>
<table class='pp' >
  <tr class='pp'>
    <td class='ppinstance'>Another Table</td>
  </tr>
</table>

but want to select only the one with td class pppagetitle (!) - any pointers?

Kay
  • 2,702
  • 6
  • 32
  • 48
  • There is a similar post here that might help you http://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class – JasonGroulx Oct 13 '12 at 17:32
  • Justed edited/specified the question: In my real HTML there are many tables all with *class pp*, and I want to select only certain ones, namely the ones containing data with td *class pppagetitle*. And, I can't edit the html, I'll need an external CSS.. – Kay Oct 13 '12 at 18:44

7 Answers7

2
.pp:nth-child(1){background:green;}

But this isn't well supported (doesn't work in IE8 and below)

Alternatively put the tables in a container and use

:first-child

Example:

<style>
    .container > .pp:first-child{background:green;}
</style>
<div class="container">
    <table class="pp"></table>
    <table class="pp"></table>
</div>

for more documentation: Quirksmode.org

Rene Koch
  • 1,252
  • 10
  • 24
1

My first thoughts, assuming they share a parent, would be:

table.pp:first-child

JS Fiddle demo.

But this will, and can, only select the particular table (with that class) if it's the first-child of its parent element.

There is no :first-of-class() selector (unfortunately).

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

You can add another class and select only the table desired:

<table class='pp' >
  <tr class='pp'>
    <td class='pppagetitle anotherClass'>A Table</td>
  </tr>
</table>
<table class='pp' >
  <tr class='pp'>
    <td class='ppinstance'>Another Table</td>
  </tr>
</table>
BurebistaRuler
  • 6,209
  • 11
  • 48
  • 66
0

table.pp:first-child

supported in ie7+

Jashwant
  • 28,410
  • 16
  • 70
  • 105
0

You can use

table.pp:first-child

or

table.pp:nth-of-type(1)
sachleen
  • 30,730
  • 8
  • 78
  • 73
0

You could use :first-child pseudo selector

Kokers
  • 1,818
  • 14
  • 19
0

@Jason Groulx's pointer on a previous post with a very elaborate answer regarding this issue solved things for me:

<style type="text/css">
.pp > .pppagetitle {
    border: 1px solid red;
}
</style>

<table class='pp' >
  <tr class='pp'>
    <td class='pppagetitle'>A Table</td>
  </tr>
</table>
<table class='pp' >
  <tr class='pp'>
    <td class='ppinstance'>Another Table</td>
  </tr>
</table>
Community
  • 1
  • 1
Kay
  • 2,702
  • 6
  • 32
  • 48