Reputation: 576
I want select the first td of my table using css selector. Here is my Html code.
<div class="mydiv">
<table class="mytable">
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
Here is my CSS code first attempt.
div.mydiv > table > tbody > tr > td
{
width:25px ;
}
second attempt
div.mydiv > table > tr > td
{
width:25px ;
}
It seems none of them are working.
Regards.
Upvotes: 1
Views: 2886
Reputation: 22643
Since you only have one element .mydiv
table just use :first-of-type
, nth-child(1)
or :first-child
:
div.mydiv td:first-of-type {
width:25px
}
<div class="mydiv">
<table class="mytable">
<tbody>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
</tbody>
</table>
</div>
There is no need of using !important
here that is bad practice.
Upvotes: 1
Reputation: 722
Try this:
div.mydiv table.mytable tr > td:first-child {
width:25px !important;
color: #ff0000;
}
See example below:
http://jsfiddle.net/kb3gN/8866/
Upvotes: 0
Reputation: 2030
Try this:
table.myTable > tbody > tr:first-child > td:first-child {
width:25px !important;
}
Happy Coding!
Upvotes: 0
Reputation: 1
You could use the :first-child
pseudo-selectors:
tr td:first-child
{
/* styles */
}
You could also use ´:first-of-type´.
Reference: http://www.w3.org/TR/css3-selectors/#selectors
Upvotes: 0
Reputation: 1776
div.mydiv > table > tbody > tr > td:first-child{
}
This will select the first child of tr
which is td
. This should give you what you need.
Hope this helps
Upvotes: 1
Reputation: 2530
Try this:
div.mydiv > table > tbody > td:first-of-type {
width:25px !important
}
Upvotes: 1