What is the right way to size html columns? - html

Simple question, I was wondering, what in 2011 is the right way to size html tables? (containing tabular data, of course!)
Should the following still be the way to go?
<tr>
<th width="45%">Name</th>
<th width="10%">Author</th>
<th width="20%">Description</th>
<th width="10%">Rating</th>
<th width="15%">Download</th>
</tr>
Or would it be better to give each column an ID (or class) and set its width with CSS?
Thank you for your input!

You can use col or colgroup for that purpose.
<table>
<col class="x"/>
<col class="y"/>
<col class="z"/>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td>3476896</td>
<td>My first HTML</td>
<td>$53</td>
</tr>
</table>
...and apply styles to the classes:
col.x {
...
}

In 2011? From about 2000 onwards it was the better approach to use class-names and CSS styles to give table-cells their width.
Unless they're all the same width, in which case just use:
th /* or td */ {
width: 20%;
}
You could, conceivably, use nth-child too:
tr th:nth-child(1) {
/* styles the first th of the tr */
}
JS Fiddle demo, using nth-child() css.

I've taken to using colgroup and col tags, like this:
<table>
<colgroup>
<col width="45%"></col>
<col width="10%"></col>
<col width="20%"></col>
<col width="10%"></col>
<col width="15%"></col>
</colgroup>
<thead>
<tr>
<th>Name</th>
<th>Author</th>
<th>Description</th>
<th>Rating</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

My personal preference is to use the width attribute on column tags.
<table>
<col width="15%"></col>
<col width="20%"></col>
...etc...
It keeps the presentation part out of the table content, so to speak.

Related

How do I apply border style attributes to a <colgroup> tag?

I am having trouble using the < colgroup > tag to style the borders of a table column in CSS.
Here is the code I am trying to run:
<table style='font-size:18px; margin:auto; text-align:center; font-family:sans-serif; border-spacing:1.875em;'>
<caption style='text-align:left;'>Wait...</caption>
<colgroup>
<col span='1' style='border:solid firebrick;'>
</colgroup>
<tr>
<td style='color:blue;'>They chose:</td>
<td style='color:blue;'>They chose:</td>
</tr>
<tr>
<th>Option 1</th>
<th>Option 2</th>
</tr>
<tr>
<td>You: $4</td>
<td>You: $5</td>
</tr>
<tr>
<td>Them: $5</td>
<td>Them: $4</td>
</tr>
</table>
As you will see if you run it yourself, the code does not produce the firebrick border that I would like to have around the first column of my table. Even when I apply "border-spacing: 0em" and "border-collapse: collapse" to the < table > tag, the firebrick border does not appear.
Does anyone know what I am doing wrong?
You need to set border-collapse to collapse on the table.
<table style='border-collapse: collapse; font-size:18px; margin:auto; text-align:center; font-family:sans-serif; border-spacing:1.875em;'>
<caption style='text-align:left;'>Wait...</caption>
<colgroup>
<col span='1' style='border:solid firebrick;'>
</colgroup>
<tr>
<td style='color:blue;'>They chose:</td>
<td style='color:blue;'>They chose:</td>
</tr>
<tr>
<th>Option 1</th>
<th>Option 2</th>
</tr>
<tr>
<td>You: $4</td>
<td>You: $5</td>
</tr>
<tr>
<td>Them: $5</td>
<td>Them: $4</td>
</tr>
</table>
Obviously you will then need to make adjustments to get whatever spacing between cells you require.

Table colgroup col style inconsistency with colspan

I am trying to highlight a column with cells that use the span attribute, like a overarching header cell.
I tried it in the most obvious way by using the colgroup and col tag. Unfortunately, this delivers inconsistent results. An overarching cell is highlighted with the first column that spans it but not with consecutive ones (see the example below).
I can see that when using background color on different cols then the overarching cell, if highlighted, would have to have both colors which is not possible. Hence, I think the most consistent result would be that it gets no color. Maybe there some attribute or so I can set to get consistent highlighting?
Test: https://jsfiddle.net/m13d2arf/1/
.highlight {
background-color: red;
}
th, td {
border: 1px solid;
}
<table>
<colgroup>
<col class="highlight">
<col>
</colgroup>
<thead>
<tr>
<th colspan="2">1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
</tr>
</tbody>
</table>
<br>
<table>
<colgroup>
<col>
<col class="highlight">
</colgroup>
<thead>
<tr>
<th colspan="2">1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
</tr>
</tbody>
</table>
As a workaround you can override the background color on the th element.
th {
background-color: white;
}
.highlight {
background-color: red;
}
th {
background-color: white;
}
th, td {
border: 1px solid;
}
<table>
<colgroup>
<col class="highlight">
<col>
</colgroup>
<thead>
<tr>
<th colspan="2">1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
</tr>
</tbody>
</table>
<br>
<table>
<colgroup>
<col>
<col class="highlight">
</colgroup>
<thead>
<tr>
<th colspan="2">1</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
</tr>
</tbody>
</table>

How can I make a whole column bold?

I have a big HTML table and I want to easily make the 16th column bold. Using <colgroup> and <col> is obviously not working:
<HTML>
<BODY>
<TABLE BORDER='1'>
<colgroup>
<col span='15'>
<col style='font-weight: bold;'>
</colgroup>
<TR>
<TH> </TH><TH>0</TH><TH>0.5</TH><TH>1</TH><TH>1.5</TH><TH>2</TH><TH>2.5</TH><TH>3</TH><TH>3.5</TH><TH>4</TH><TH>4.5</TH><TH>5</TH><TH>5.5</TH><TH>6</TH><TH>6.5</TH><TH>7</TH><TH>7.5</TH><TH>8</TH><TH>8.5</TH><TH>9</TH><TH>9.5</TH><TH>10</TH><TH>10.5</TH><TH>11</TH><TH>11.5</TH><TH>12</TH><TH>12.5</TH><TH>13</TH><TH>13.5</TH><TH>14</TH><TH>14.5</TH><TH>15</TH><TH>15.5</TH><TH>16</TH>
</TR>
<TR BGCOLOR='#DDDDDD'>
<TH ALIGN='LEFT'>ARG-21_VSDS (0):</TH><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.00</TD><TD>0.01</TD><TD>0.01</TD><TD>0.05</TD><TD>0.13</TD><TD>0.33</TD><TD>0.59</TD><TD>0.82</TD><TD>0.93</TD><TD>0.97</TD><TD>0.99</TD><TD>1.00</TD><TD>1.00</TD>
</TR>
<TR BGCOLOR='#DDDDDD'>
<TH ALIGN='LEFT'>ARG-21_VSDS (1):</TH><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>1.00</TD><TD>0.99</TD><TD>0.99</TD><TD>0.95</TD><TD>0.87</TD><TD>0.67</TD><TD>0.41</TD><TD>0.18</TD><TD>0.07</TD><TD>0.03</TD><TD>0.01</TD><TD>0.00</TD><TD>0.00</TD>
</TR>
</TABLE>
</BODY>
</HTML>
You can use CSS's nth-of-type selector to do this easily with no additional HTML markup required:
td:nth-of-type(16) {
font-weight: bold;
}
you should use classes on your tags and then target the within that class. for example:
<tr class="bold">some <td> tags</tr>
tr.bold td{
font-weight:bold;
}

Setting fixed column width does not work with Bootstrap 3

I have a table created using HTML and Bootstrap 3 like in the following example.
Even if I set a fixed column width through the width attribute in the colgroup this is not applied and I am not able to set a column width at all here.
Can someone tell me if this needs to be done differently when using Bootstrap 3 or if I am missing something else here?
In this case I want to set the column width of a specific column (here the 3rd one) to 70px.
Example table:
<table class="table table-condensed table-responsive tableDT">
<colgroup>
<col />
<col />
<col width="70px" />
</colgroup>
<thead>
<tr>
<th>Tag</th>
<th>No. of Items</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some text</td>
<td>Some number</td>
<td><button type='button' class='btn btn-primary btn-xs'>View</button></td>
</tr>
</tbody>
</table>
You do not use 'px'. you just give it as width="70"
you can do it in css as well as:
colgroup col:last-child{
width: 70px;
}

Avoid <colgroup> / <col> HTML tags to alter <thead> and <tfoot> rows?

Is there a way to limit/restrain the field of "operation" of styles defined in <colgroup> / <col> HTML tags.
Given the following table:
<table>
<colgroup>
<col align="center" />
<col align="center" style="background-color: lightgrey;" />
<col align="center" />
</colgroup>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
<th>Column C</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.a</td>
<td>1.b</td>
<td>1.c</td>
</tr>
<tr>
<td>2.a</td>
<td>2.b</td>
<td>2.c</td>
</tr>
</tbody>
</table>
I would like the background-color: lightgrey; not to be applied to the "Column B" cell (Second th in thead).
You can always apply a style to that cell in particular, or style the whole <tr> for your header row.
http://jsfiddle.net/QQ7LJ/
<tr style="background-color:white;">
<th>Column A</th>
<th>Column B</th>
<th>Column C</th>
</tr>
in short: no, there is no way to "limit" css, it will match every available target, and styling your <col> will match the entire column. In order to get different styling, you need to overwrite it somehow, the easiest way being to explicitly style the ones that dont match the general style.
Edit, you can also do this in a stylesheet block, by using CSS3 selectors :nth-of-type and scoping your selector to the <tbody> element.
http://jsfiddle.net/QQ7LJ/1/
tbody td:nth-of-type(2) {
background-color: lightgrey;
}​
and the changes to your HTML (everything else is the same)
<colgroup>
<col align="center" />
<col align="center"/>
<col align="center" />
</colgroup>