Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selector to select first and second table cells

Tags:

html

css

Can someone help me figure what selectors I need to change the width of my columns globally on my two column table?

Using this, will of course select all cells:

table td {
    width: 50px;
}

What I want is this:

<table>
    <tr>
        <td style="width: 50px">data in first column</td>
        <td style="width: 180px">data in second column</td>
    </tr>
    <tr>
        <td style="width: 50px">more data in first column</td>
        <td style="width: 180px">more data in second column</td>
    </tr>
</table>

I'd like to put the width specification in a style sheet rather then typing the width implicitly for each tag. What CSS selector will let me set the 50px width and 180px appropriately?

like image 943
Icemanind Avatar asked Mar 09 '13 01:03

Icemanind


People also ask

Which selector is at first row of the table?

The ::first-line selector is used to add a style to the first line of the specified selector. Note: The following properties can be used with ::first-line: font properties.

How do you select the first two elements in CSS?

You start with -n, plus the positive number of elements you want to select. For example, li:nth-child(-n+2) will select the first 2 li elements.

Can I select multiple elements at once with CSS?

It is possible to give several items on your page the same style even when they don't have the same class name. To do this you simply list all of the elements you want to style and put a comma between each one.

What are the 3 selectors in CSS?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)


1 Answers

Yep.

td:first-child{
   width:50px;
}

td:nth-child(2){
   width: 180px;
}

or, as suggested by Brainslav's edit:

td:nth-child(1) {
      width: 50px;}
td:nth-child(2) {
      width: 180px;}

link

like image 196
Aurelio Avatar answered Sep 19 '22 21:09

Aurelio