What's the best way to represent an empty TH in HTML5? - html

Say I have the given table:
+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+------+
| Row1 | D1.1 | D1.2 | D1.3 |
+------+------+------+------+
| Row2 | D2.1 | D2.2 | D2.3 |
+------+------+------+------+
| Row3 | D3.1 | D3.2 | D3.3 |
+------+------+------+------+
And I want to represent it in HTML5. The tricky thing is that tables like this must be semantically important, but the top-left cell is not semantically important, but instead a spacer to line up the more important column headers. What's the best way to do this? My first idea is to do it like this:
<table>
<thead>
<tr>
<th></th>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</tr>
</thead>
<tbody>
<tr>
<th>Row1</th>
<td>D1.1</td>
<td>D1.2</td>
<td>D1.3</td>
</tr>
<tr>
<th>Row2</th>
<td>D2.1</td>
<td>D2.2</td>
<td>D2.3</td>
</tr>
<tr>
<th>Row3</th>
<td>D3.1</td>
<td>D3.2</td>
<td>D3.3</td>
</tr>
</tbody>
</table>
Though, putting <th></th> in there feels just wrong, like using <p> </p> for spacing. Is there a better way to do this?

It's completely acceptable to have an empty <th> element, speaking in terms of either validity or semantics. Nothing in the spec forbids it; in fact, it contains at least one example that makes use of an empty <th> for this very purpose:
The following shows how one might mark up the gross margin table on page 46 of Apple, Inc's 10-K filing for fiscal year 2008:
<table>
<thead>
<tr>
<th>
<th>2008
<th>2007
<th>2006
<tbody>
<tr>
<th>Net sales
<td>$ 32,479
<td>$ 24,006
<td>$ 19,315
<!-- snip -->
</table>

For a discussion about semantics and empty table elements I would like to refer to this question on StackOverflow
Styling of "empty" cells (like background or borders) can sometimes depend on the absence/presence of "content" that is why people often put a inside. There is a special CSS tag for styling empty cells you can read about it here on MDN.
table {
empty-cells: hide;
}
Here you can find another article with some nice background information on this topic.

Any better way of using empty <th></th>:
Exact code:
<tr>
<th></th>
<th colspan="6"></th>
</tr>

Related

HTML: Split list in multiple Columns

While there are many questions like that, none of them describes my problem:
I have this list:
<ul>
<li>Burger</li>
<li>Fries</li>
<li>Coke</li>
</ul>
The list gets it's data from a database, that also includes the prices.
Now I need a list that also can show me the price in another column, like:
1. Burger | 6.99$
2. Fries | 2.99$
3. Coke | 1.99$
But all questions I find are about multiple columns if the list is too long.
Is there a way to reach my goal?
Lists aren't designed like that, I guess you could implement some kind of hacky way to make a multi-column list, or you can use a table:
<table>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
<tr>
<td>Burger</td>
<td>$6.99</td>
</tr>
<tr>
<td>Fries</td>
<td>$2.99</td>
</tr>
<tr>
<td>Coke</td>
<td>$1.99</td>
</tr>
</table>

How to change HTML table tab order from horizontal to vertical without tabindex?

I have an HTML table like this:
*----------*----------*
| Cell 1 | Cell 4 |
*----------*----------*
| Cell 2 | Cell 5 |
*----------*----------*
| Cell 3 | Cell 6 |
*----------*----------*
How do I change the tab order to properly run from Cell 1 to Cell 6 without using tabindex? I ask this knowing that the WAI-ARIA guidelines discourage the use of tabindex to change the tab order of cells.
I've considered breaking the table into two elements -- left and right -- so that they will have the correct order in the DOM. However, this solution does not seem to maintain the aspects of a natural HTML table (such as equal height across rows of cells).
The following works from a tab perspective but does not work for a screen reader. I'm using a <button> in the table just to demonstrate the tabbing order.
<table>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
<tr>
<td>
<table>
<tr>
<td><button>cell 1</button></td>
</tr>
<tr>
<td><button>cell 2</button></td>
</tr>
<tr>
<td><button>cell 3</button></td>
</tr>
</table>
</td>
<td>
<table>
<tr>
<td><button>cell 4</button></td>
</tr>
<tr>
<td><button>cell 5</button></td>
</tr>
<tr>
<td><button>cell 6</button></td>
</tr>
</table>
</td>
</tr>
</table>
To tab vertically, you have to group cell1-cell3 in a container and cell4-cell6 in another container, then display the two containers side by side. Since you can't have a container span across rows in a table, you have to use a (nested) table as the container. The first cell of the main table is a nested table, so tabbing goes through the nested table (cell1-cell3) first. Then tabbing goes to the second cell of the main table, which again is a nested table (cell4-cell6).
You could try to simplify it and put cell1-cell3 in a <div> and have that <div> be the first cell of the main table, but then cell1-cell3 would not, individually, be in separate data cells, but that's up to you if that's important.
<table>
<tr>
<th>header 1</th>
<th>header 2</th>
</tr>
<tr>
<td>
<div>
<button>cell 1</button><br>
<button>cell 2</button><br>
<button>cell 3</button>
</div>
</td>
<td>
<div>
<button>cell 4</button><br>
<button>cell 5</button><br>
<button>cell 6</button>
</div>
</td>
</tr>
</table>
Getting back to the screen reader, there are keyboard shortcuts to traverse through all the cells of a table. On a PC, using JAWS or NVDA, it's ctrl+alt+arrow.
So ctrl+alt+RightArrow will let me traverse across a row. Even if you used ill-advised positive values for tabindex to control the vertical tabbing order, it will not affect the way a screen reader can navigate through a table. So if there's significant meaning in reading the table vertically, the screen reader user will lose that meaning and may not understand your table.
A few questions to consider:
Are the two columns related?
Is the table a "real" table in that it's displaying data, or is the table being used for layout purposes?
Are there column headers?
Tabbing vertically might not be necessary if the purpose of tabbing vertically can be conveyed by having sufficient row headers. Most tables have column headers but ofttimes, row headers are left off. They are very useful.
<table>
<tr>
<th scope="col">name</th>
<th scope="col">age</th>
<th scope="col">height</th>
</tr>
<tr>
<th scope="row">dave</th>
<td>12</td>
<td>4'8"</td>
</tr>
<tr>
<th scope="row">fred</th>
<td>13</td>
<td>4'9"</td>
</tr>
<tr>
<th scope="row">henry</th>
<td>14</td>
<td>4'10"</td>
</tr>
</table>

Split html table, odd columns to even [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like a single <table> tag to render like this:
+_____+_____+_____+
| | | |
+_____+_____+_____+
| | | |
+_____+_____+_____+
| |
+________+________+
| | |
+________+________+
| | |
+________+________+
Possible? I am lost on this one. I've been tableless for a long time. Documentation on changing the column numbers half way through is not clear. I know it could be done with col-span if the shift in columns was even, but how to go from odd to even?
Explanation
Provide a colspan of two for those td's that only require one space, and then you'll be able to use 3 (which is actually 1.5) as the colspan for the other row. Maybe this isn't the best solution, but it's the first that I came up with.
The example only has two of the 3 types of rows you had in your example, but to add the whole width cell, just add a colspan of 6.
Code
<table>
<tr>
<td colspan=2></td>
<td colspan=2></td>
<td colspan=2></td>
</tr>
<tr>
<td colspan=3></td>
<td colspan=3></td>
</tr>
</table>
The Fiddle
Find the smallest common divisor, in this case 6, and set a colspan on each cell to divide that number by the number of cells in the row.
<table>
<tr>
<td colspan="2"></td>
<td colspan="2"></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td colspan="3"></td>
<td colspan="3"></td>
</tr>
</table>
Demo: http://jsfiddle.net/S5K8p/

Is it possible to alternate rowspans?

I guess this schema of what i am trying to do will be more descriptive than the question itself.
+------------------------+-----------------------+
| | |
+ +-----------------------+
| | |
+------------------------+ +
| | |
+ +-----------------------+
| | |
+------------------------+-----------------------+
| | |
+------------------------+-----------------------+
As you can see, i am trying to create a 2 column table where at some point it should be able to rowspan one of the columns, and eventually the other over the next 2 rows.
Am i missing some obvious way of doing it, or some advanced table tag/attribute? Or there is just no way of doing that using tables?
I am aware that it is possible to achieve that using other ways, but tables are what i am interested in, and what is the question about.
For reference:
There are some things i've tried and didn't succeed
http://jsfiddle.net/dbtYk/
Unless I've missed something, here is an example based on your jsfiddle
<table>
<tr style="height:20px;">
<td rowspan="2">left</td>
<td>right</td>
</tr>
<tr style="height:20px;">
<td rowspan="2">right</td>
</tr>
<tr style="height:20px;">
<td rowspan="2">left</td>
</tr>
<tr style="height:20px;">
<td>right</td>
</tr>
<tr>
<td>left</td>
<td>right</td>
</tr>
</table>
UPDATE
As pointed out by Cicada the above unfortunately doesn't work in Chrome.
UPDATE 2
As pointed out by Alohci, adding a height makes it work in Chrome. The above has been amended to reflect that, along with a new jsfiddle.
Of course that is possible :-)
<table border="yes">
<tr>
<td rowspan="2">1</td>
<td>2</td>
</tr>
<tr>
<td rowspan="2">3</td>
</tr>
<tr>
<td rowspan="2">4</td>
</tr>
<tr>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
</tr>
</table>
Just skip the cells that are already covered by spanned cells from a previous row.

How do I make a button stretching to the entire length of the right side of a table?

I need a button spanning the entire length of a table that will perform the function of making a call to get the next data-set to save the user from having to manually selecting it.
Unfortunately my css is not that great. Any help? Thanks!
There is some confusion as to what I want, let me try to draw it.
| table |b|
| |u|
| |t|
|_______|n|
I can't give you the mark up because it's at work. Plus it's heavily templated so it wouldn't help much.
Ok, so that problem is a little trickier.
<div style="position: relative;">
<table style="">
<tr>
<td>asdasdasdssssssss sssssssssssss sssssssssss sssssssss sssssssssssssss sssssssss</td>
<td>betasssssssssssssssssssssssssssss ssssssssssssssssssssssssssss ssssssssssssssss sssssssssss sssssss sssssssssssssssss s s ss s s s</td>
</tr>
<tr>
<td>alpha</td>
<td>beta</td>
</tr>
</table>
<div style="position: absolute; top: 0pt; right: 0pt; bottom: 0pt;">
<button style="height: 100%;">c</button>
</div>
</div>
The problem with this solution is you need to give your content that sits on the right a width then add a margin to the table so that it does not hide behind the button.
There is a lot of information for you to take in over at this related question which points to some valuable resources.
<table>
<tr>
<td>alpha</td>
<td>beta</td>
</tr>
<tr>
<td colspan="2"><button style="width:100%">c</button></td>
</tr>
</table>
Some example of what you are trying to lay out and what you have tried would have been useful.
The important thing is the colspan which turns many columns into 1.
I wouldn't recommend using <table>s, as they scale differently than most elements (i.e. if you have an element inside a table that gets too big (like a block/inline-block/or long word) the table scales, possibly throwing off your layout. That, and having to mark each row adds more cruft to your markup.
Anyways, if you want to span multiple rows to be able to right align an <input type="submit"/> or whatever you're trying to do, you can simply say:
<table>
<tr>
<!-- lots of other rows and stuff -->
<td colspan="4" align="right"><!-- Adjust [colspan] to # of rows in yours -->
<input type="submit" value="I'm on the right now!"/>
</td>
</tr>
</table>
The align="right" does what you'd expect - aligns inline/inline-block elements to the right (generally text and images). The colspan="4" (again, replace with your own number of columns) simply tells the table that you're going to be merging 4 columns of the table starting with you (going to the right).
So, before:
-------------------------
| 1 | 2 | 3 | 4 |
-------------------------
| <------ after ------> |
-------------------------
Hope that helps.