Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector for table column

Tags:

html

css

I dont have much experience with CSS, but I am trying to help a friend format a table using CSS. Right now I got stuck trying to format table width, here is an example of the table: https://form.jotform.com/53306318713148

If I want to change the input of all the fields I can just

table input {
width: 100px;
}

But now we want to have different input sizes for each one of the columns, so after reading about CSS selectors I was trying something of the following:

#cid_1 [id$=_1] {
width: 100px;
}

The thought was that I would select the element with id cid_1 and the the children of that element ending with id _1, but that does not seem to work. Seems like a most element solution would be to use some kind of :nth-child(). Probably a stupid question, butI was hoping someone could show me how to do this.

like image 709
Mac Avatar asked Nov 27 '15 20:11

Mac


People also ask

How do I select a specific column in CSS?

If you want to apply a style to a specific column or row (but not on others), use :nth-child() property from CSS3. This selector is used to apply a style to the element number 'n' inside a parent element.

How do you select the first column of a table in CSS?

The syntax is :nth-child(an+b), where you replace a and b by numbers of your choice. For instance, :nth-child(3n+1) selects the 1st, 4th, 7th etc. child.

How do you use COL in a table?

If you want to style the two first columns of a table, use the <colgroup> and <col> elements. The <colgroup> element should be used as a container for the column specifications. Each group is specified with a <col> element. The span attribute specifies how many columns that get the style.

How do I add a column to a table in CSS?

HTML. The first <col /> tag now creates a column that spans the first cells of all rows. The second <col span="2" /> tag creates a column that spans the second and third cells of all rows. The fourth cell in every row is not a part of any column.


1 Answers

You can use css3 nth-child selector using this format:

table tr td:nth-child(2) input {
    background-color: red;
}

In the example above, the background color of the input inside the second column of each row will become red.

And in your case, you can say:

    table tr td:nth-child(2) input {
            width: 100px;
        }

    table tr td:nth-child(3) input {
            width: 200px;
        }
      ....

the selector's argument starts with 2, because the first one is labels for each row.

here's a working example

like image 199
Arash DaneshAraste Avatar answered Oct 23 '22 22:10

Arash DaneshAraste