Table colgroup col style inconsistency with colspan - html

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>

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. How to save background color of col using colSpan?

Is it possible to save col background color using colpan? In example below I want to achive yellow color in space for last col even if I use colspan="3".
Please, take a look to picture above, I want to achive this result using colspan="3"
Solution example
Wrong solution example
Thanks!
table,
th,
td {
border: 1px solid black;
}
<table>
<colgroup>
<col span="2" style="background-color:#E6E6DC">
<col style="background-color:yellow">
</colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td colspan="3">Error descriotion, last col should be yellow</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>
You can just add an "id" to your td colspan,
<td colspan="3" id="your id here">Error descriotion, last col should be yellow</td>
and add this to css
#your id here{
background-color: yellow;
}
UPDATE
OP didn't make sense:
I want to achieve yellow color in space for last col even if I use colspan="3"
Ok, I chopped up that large 3 rowspan and now the last column is yellow.
Ok, kept middle row at colspan='3', and used <mark> in order to hold the styles within the <td>. See updated Snippet.
SNIPPET
<!DOCTYPE html>
<html>
<head>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>
<body>
<table style='border-collapse:collapse;table-layout:fixed;width:325px;'>
<colgroup>
<col span="2" style="background-color:red">
<col span='1' style="background-color:yellow">
</colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td colspan='3' style='padding:0'>Error description, last col should
<mark style='line-height:1.2;width:12.25ex;border-right:1.75px solid yellow;border-left:3.75px solid yellow;display:inline-block;padding:0 12px 0 2px;position:relative; left: 3px;'> be yellow</mark>
</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>
</body>
</html>
Try adding a class to col and apply the same style for the colspan.
table, th, td {
border: 1px solid black;
}
table col.col-color, table td[colspan="3"]{
background-color: yellow;
}
<table>
<colgroup>
<col span="2" style="background-color:red">
<col class="col-color">
</colgroup>
<tr>
<th>ISBN</th>
<th>Title</th>
<th>Price</th>
</tr>
<tr>
<td colspan="3">Error descriotion, last col should be yellow</td>
</tr>
<tr>
<td>5869207</td>
<td>My first CSS</td>
<td>$49</td>
</tr>
</table>

Splitting a table into bordered groups with the colgroup and col elements

Using a combination of CSS and plain HTML, I am trying to get a table that has 3 groups of columns. I only want the groups to have vertical rules:
Here is what I have so far:
colgroup col {
border-left: 1px solid #ccc;
border-right: 1px solid #ccc;
}
tr.secondary-headers th {
border: 0 !important;
text-align: center;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
<table class="table table-condensed-extra table-hover">
<colgroup>
<col span="5">
<col span="4">
<col span="5">
</colgroup>
<thead>
<tr class="top-headers">
<th colspan="5"> </th>
<th colspan="4">Merchant</th>
<th colspan="5">ClearPath</th>
</tr>
<tr class="secondary-headers">
<th>SKU</th>
<th>Commodity</th>
<th>Restricted</th>
<th>COO</th>
<th>Quantity</th>
<th>Sale Price</th>
<th>Shipping</th>
<th>Handling</th>
<th>Insurance</th>
<th>International Shipping</th>
<th>International Handling</th>
<th>Duties</th>
<th>Taxes</th>
<th>Brokerage</th>
</tr>
</thead>
This gets me vertical rules on ALL the columns:
I'm trying to avoid nth-child as the solution should suit IE 8.
The CSS properties are inherited into every column covered by the span attribute. To prevent this:
Create three <colgroup> elements. The first colgroup gets the span attribute and no children, accounting for five columns.
The second <colgroup> gets four individual <col> elements and the third gets five.
Apply the left or right borders for each column as needed with a class.
The multiple colgroups also make sense semantically; matching the grouping of the data.
Bonus: You could possibly use this instead of classes:
colgroup col:first-child {
border-left: 1px solid #ccc;
}
Complete Example
colgroup .leftBorder {
border-left: 1px solid #ccc;
}
colgroup .rightBorder {
border-right: 1px solid #ccc;
}
table tr.top-headers th {
text-align: center;
border: none !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
<table id="parcel-details" class="table table-condensed-extra table-hover">
<colgroup span="5">
</colgroup>
<colgroup>
<col class="leftBorder">
<col>
<col>
<col>
</colgroup>
<colgroup>
<col class="leftBorder">
<col>
<col>
<col>
<col class="rightBorder">
</colgroup>
<thead>
<tr class="top-headers">
<th colspan="5"> </th>
<th colspan="4">Merchant</th>
<th colspan="5">ClearPath</th>
</tr>
<tr class="secondary-headers">
<th>SKU</th>
<th>Commodity</th>
<th>Restricted</th>
<th>COO</th>
<th>Quantity</th>
<th>Sale Price</th>
<th>Shipping</th>
<th>Handling</th>
<th>Insurance</th>
<th>International Shipping</th>
<th>International Handling</th>
<th>Duties</th>
<th>Taxes</th>
<th>Brokerage</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

<col> element border thru CSS not working

I am using to apply borders to specific columns only, it is not working at all to me..
other properites are working like background-color..
Is browsers doesn't support column level border on tables?
here my code
<style type="text/css">
.check_border {
border-right: 1px solid black;
background-color: #0000ff;
}
</style>
<table cellspacing='0'>
<colgroup>
<col/>
<col class="check_border"/>
<col class="check_border"/>
<col class="check_border"/>
<col/>
</colgroup>
<tr>
<td>Country</td>
<td>Salmon</td>
<td>Shrimps</td>
<td>Oysters</td>
<td>Rice</td>
<td>Wheat</td>
</tr>
<tr>
<td>United Kingdom</td>
<td>2050</td>
<td>1545</td>
<td>1156</td>
<td>5007</td>
<td>12254</td>
</tr>
<tr>
<td>United States</td>
<td>1358</td>
<td>1884</td>
<td>784</td>
<td>10597</td>
<td>24554</td>
</tr>
<tr>
<td>Australia</td>
<td>985</td>
<td>65</td>
<td>518</td>
<td>2548</td>
<td>10548</td>
</tr>
</table>
PPK has a good article about it all. The summary of it is that styling a col is somewhat variable in browser support and very troublesome in application.
http://www.quirksmode.org/css/columns.html

Setting table column width

I've got a simple table that is used for an inbox as follows:
<table border="1">
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
How do I set the width so the From and Date are 15% of the page width and the Subject is 70%. I also want the table to take up the whole page width.
<table style="width: 100%">
<colgroup>
<col span="1" style="width: 15%;">
<col span="1" style="width: 70%;">
<col span="1" style="width: 15%;">
</colgroup>
<!-- Put <thead>, <tbody>, and <tr>'s here! -->
<tbody>
<tr>
<td style="background-color: #777">15%</td>
<td style="background-color: #aaa">70%</td>
<td style="background-color: #777">15%</td>
</tr>
</tbody>
</table>
table {
width: 100%;
border: 1px solid #000;
}
th.from, th.date {
width: 15%
}
th.subject {
width: 70%; /* Not necessary, since only 70% width remains */
}
<table>
<thead>
<tr>
<th class="from">From</th>
<th class="subject">Subject</th>
<th class="date">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>[from]</td>
<td>[subject]</td>
<td>[date]</td>
</tr>
</tbody>
</table>
The best practice is to keep your HTML and CSS separate for less code duplication, and for separation of concerns (HTML for structure and semantics, and CSS for presentation).
Note that, for this to work in older versions of Internet Explorer, you may have to give your table a specific width (e.g., 900px). That browser has some problems rendering an element with percentage dimensions if its wrapper doesn't have exact dimensions.
Use the CSS below, the first declaration will ensure your table sticks to the widths you provide (you'll need to add the classes in your HTML):
table{
table-layout:fixed;
}
th.from, th.date {
width: 15%;
}
th.subject{
width: 70%;
}
Alternative way with just one class while keeping your styles in a CSS file, which even works in IE7:
<table class="mytable">
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
<style>
.mytable td, .mytable th { width:15%; }
.mytable td + td, .mytable th + th { width:70%; }
.mytable td + td + td, .mytable th + th + th { width:15%; }
</style>
More recently, you can also use the nth-child() selector from CSS3 (IE9+), where you'd just put the nr. of the respective column into the parenthesis instead of stringing them together with the adjacent selector. Like this, for example:
<style>
.mytable tr > *:nth-child(1) { width:15%; }
.mytable tr > *:nth-child(2) { width:70%; }
.mytable tr > *:nth-child(3) { width:15%; }
</style>
These are my two suggestions.
Using classes. There is no need to specify width of the two other columns as they will be set to 15% each automatically by the browser.
table { table-layout: fixed; }
.subject { width: 70%; }
<table>
<tr>
<th>From</th>
<th class="subject">Subject</th>
<th>Date</th>
</tr>
</table>
Without using classes. Three different methods but the result is identical.
a)
table { table-layout: fixed; }
th+th { width: 70%; }
th+th+th { width: 15%; }
<table>
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
b)
table { table-layout: fixed; }
th:nth-of-type(2) { width: 70%; }
<table>
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
c) This one is my favourite. Same as b) but with better browser support.
table { table-layout: fixed; }
th:first-child+th { width: 70%; }
<table>
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
</table>
Add colgroup after your table tag. Define width and number of columns here, and add the tbody tag. Put your tr inside of tbody.
<table>
<colgroup>
<col span="1" style="width: 30%;">
<col span="1" style="width: 70%;">
</colgroup>
<tbody>
<tr>
<td>First column</td>
<td>Second column</td>
</tr>
</tbody>
</table>
Depending on your body (or the div which is wrapping your table) 'settings' you should be able to do this:
body {
width: 98%;
}
table {
width: 100%;
}
th {
border: 1px solid black;
}
th.From, th.Date {
width: 15%;
}
th.Date {
width: 70%;
}
<table>
<thead>
<tr>
<th class="From">From</th>
<th class="Subject">Subject</th>
<th class="Date">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>Me</td>
<td>Your question</td>
<td>5/30/2009 2:41:40 AM UTC</td>
</tr>
</tbody>
</table>
Demo
Try this instead.
<table style="width: 100%">
<tr>
<th style="width: 20%">
column 1
</th>
<th style="width: 40%">
column 2
</th>
<th style="width: 40%">
column 3
</th>
</tr>
<tr>
<td style="width: 20%">
value 1
</td>
<td style="width: 40%">
value 2
</td>
<td style="width: 40%">
value 3
</td>
</tr>
</table>
table { table-layout: fixed; }
.subject { width: 70%; }
<table>
<tr>
<th>From</th>
<th class="subject">Subject</th>
<th>Date</th>
</tr>
</table>
Here's another minimal way to do it in CSS that works even in older browsers that do not support :nth-child and the like selectors: http://jsfiddle.net/3wZWt/.
HTML:
<table>
<tr>
<th>From</th>
<th>Subject</th>
<th>Date</th>
</tr>
<tr>
<td>Dmitriy</td>
<td>Learning CSS</td>
<td>7/5/2014</td>
</tr>
</table>
CSS:
table {
border-collapse: collapse;
width: 100%;
}
tr > * {
border: 1px solid #000;
}
tr > th + th {
width: 70%;
}
tr > th + th + th {
width: 15%;
}
<table>
<col width="130">
<col width="80">
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</table>
Demo
Don't use the border attribute, use CSS for all your styling needs.
<table style="border:1px; width:100%;">
<tr>
<th style="width:15%;">From</th>
<th style="width:70%;">Subject</th>
<th style="width:15%;">Date</th>
</tr>
... rest of the table code...
</table>
But embedding CSS like that is poor practice - one should use CSS classes instead, and put the CSS rules in an external CSS file.
style="column-width:300px;white-space: normal;"