3

Here I want to add background color to the column Sunday without adding css class to it. Can we achieve it without adding a css class and inline style? Here's a sample Fiddle

/------------------HTML----------------/

<table> <tr> <td>Sun</td> <td>Mon</td> <td>Tue</td> <td>Wed</td> <td>Thurs</td> <td>Fri</td> <td>Sat</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td>6</td> <td>7</td> </tr> <tr> <td>8</td> <td>9</td> <td>10</td> <td>11</td> <td>12</td> <td>13</td> <td>14</td> </tr> </table> 

/------------------CSS----------------/

table{ border:1px solid #ccc; border-collapse:collapse; padding:5px; } table td{ background:#f6f6f6; padding:3px; border:1px solid #ccc; } 

7 Answers 7

4

Yes you can:

td:first-of-type will select first td i.e. first cell of all rows.

table tr td:first-of-type{//your code} 

Updated fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

2

http://jsfiddle.net/uQRA9/2/

Yes you can.

For example :

table td:first-child { background: red; } table td:nth-child(4) { background: blue; } 

Comments

2

Yes using :first-child pseudo class.

table tr:first-child > td:first-child{ background-color :red; } 

The above one is for older browsers. You can also use first-of-type as shown by Hiral.

Working Fiddle

Comments

0

use like this

 tr>td:nth-child(1){ background-color:red; } 

Comments

0

you can style sun column and all under it with this

table td:first-child{ background-color :red; } 

demo

Comments

0

You can manage by css, :nth-child(0) property provides selector specified index (0,....., n)

table td:nth-child(0){ background:#f6f6f6; padding:3px; border:1px solid #ccc; } 

AND

table tr:nth-child(0){ background:#f6f6f6; } 

Comments

0
table tr:first-child td:first-child { background:...; ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.