Wrapper for multiple <tbody>? - html

For specific css requirements I'm using multiple <tbody> tags in my table design which looks something like this:
Use of multiple tbody tags
But I also require a wrapper for multiple tbody tags (something like a common tbody parent) such that this wrapper can be scrolled in order achieve the following effect:
A common tbody which can be scrolled
How do I achieve the latter srolling effect in the former one?
(P.S.: I know this can be done through nested table approach, but I'm looking for other alternatives if any)

As mentioned in the comments by FelipeAls and others that a <tbody> tag can be wrapped only by a <table> tag, I tried wrapping <thead> and <tbody>s in separate tables to create the desired effect in the following way:
<table>
<thead>
...
</thead>
</table>
<table>
<tbody>
...
</tbody>
<tbody>
...
</tbody>
<tbody>
...
</tbody>
</table>
This solved the issue.
Here's a Working Demo.

You cannot have a wrapper for tbody elements inside a table. The tbody element itself is a wrapper for tr elements. HTML syntax does not allow any other container for tbody but table. What matters more is that this syntax rules is actually enforced by the way browsers parse HTML.
If you try to use, say, a div element as a wrapper (the most reasonable approach), it will actually create a div element in the DOM, but an empty one, and before the table. All the tbody and tr elements are inserted into the table element; they are effectively extracted from the div element, which thus becomes empty, unless it contains something else than table-related elements.
An illustration, using intentionally invalid markup:
<style>
.x { outline: solid red }
</style>
<table>
<tbody>
<tr><td>foo
</tbody>
<div class=x>
FOO!
<tbody>
<tr><td>foo2
</tbody>
<tbody>
<tr><td>foo3
</tbody>
</div>
<tbody>
<tr><td>The end
</tbody>
</table>
The conclusion is that you need a different approach. The most obvious one is to use just a single tbody element. If this is not feasible, you should explain why, but this would be a topic for a new question.

Related

Element not inserting correctly in HTML code [duplicate]

I'm developing application with Backbone.js
View class returns one element after rendering. It's ok if I use divs or spans.
But problem starts when I start to render objects as tr rows.
One objects should be rendered to 2-3 rows.
So can I use this structure?
<table>
<div>
<tr>...</tr>
<tr>...</tr>
</div>
</table>
divs immediately inside a table tag is invalid. use tbody instead
Yes, it is a very bad idea.
The HTML specification does not allow div elements to be child elements of a table element, so it triggers error recovery routines in browsers.
Given this input:
<table>
<tr><td>1</td></tr>
<div>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
</div>
<tr><td>4</td></tr>
<div>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
</div>
The DOM that Firefox will generate (and you can see this by using the Inspect Element feature) will look like this:
<div>
</div><div>
</div><table>
<tbody><tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
</tbody></table>
Note that the div elements have been removed from the table and placed before it. This makes them useless for manipulating the rows.
HTML provides the thead, tfoot and tbody elements to group table rows. Use the appropriate ones of those instead of div elements (you can have only one thead and only one tfoot, but there are no limits on the number of tbody elements).
That is not valid HTML. You can not nest block or inline elements in a table, only table elements, such as <tbody>, <tr> or <thead>. You can of course nest a <div> in a table cell (<td> or <th>).

CSS: Is it possible for a td to infer style from its header or vice versa?

I am formatting my tables, and some of them have hyperlinks in the right hand column which I want right aligned. Is there a way from css to infer that the column has links in it, and right align the whole column, including the header?
Alternatively, is there a way to apply a class to just the header and have it affect the alignment of all of the columns underneath it?
I recognize that I can apply a style to the individual th and td elements, but I was hoping for something a little more elegant.
EDIT: There is only one table.
<table>
<thead>
<tr>
<th>Some Column</th>
<th>actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some data</td>
<td>Edit</td>
</tr>
</tbody>
<table>
I am asking if I can apply a style to the element for the actions and write CSS which will cause all of the elements in that column be style a particular way.
This functionality is not part of CSS. Shaun Inman suggested something like a parent selector that would allow parents to inherit from their children, but there are tons of issues with this methodology.
I would suggest, instead, that you try a javascript solution. You could search the table to see if it contains links, then add a class to the table in the case that they do. Something like this:
HTML
<table>
<thead>
<tr>
<th>Normal</th>
<th>Align Me</th>
<th>Normal</th>
</tr>
</thead>
<tbody>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
</tbody>
</table>
jQuery
$('td > a').each(function(){
var $td = $(this).parent();
$td.addClass('align-right');
var $th = $td.closest('table').find('th').eq($td.index()).addClass('align-right');
});
Here is a fiddle for you to check out.
CSS can only be applied to child or siblling elements. Children cannot tell their parents what to do.
Applying a class to the TD is the right thing to do.
What you want can not be done with CSS.
But in your special case, you can refer to the fact, that the column with the links is the last one in each row.
There is a special "pseudo-class" in CSS for this: last-child.
th:last-child { ... }
td:last-child { ... }
I came up with a solution, but it only works when the actions are the last column in the table.
<table class="hasActions">
...
</table>
And CSS:
table.hasActions td:last-of-type, table.hasActions th:last-of-type {
text-align: right;
}
Since this doens't work for arbitrary columns, I'll leave the question open for now.

Is it necessary to put the <td> inside a <tr>

<TR class="bcgrndClr">
<span class="Title">
My title
</span>
</TR>
I wrote this part, but the background color is not coming in chrome and other browsers, where as working fine in "IE".
so is it necessary to put a <td> before <span>
or should i go for <th> instead of <tr>
or how else should i give the title (with some conditions) to this particular table
TH and TD are interchangable, but not TH and TR. Thats how i been using it at least.
you can do something like:
<tr><td colspan="2"><span>Hello World</span></td></tr>
colspan is used to make a TD element stretch across multiple row elements.
Yes, you need <td>. Browsers will still try to render the table if you write invalid HTML, but the rendering will be inconsistent between browsers.
<th> can take the place of <td> if the cell is a header cell. It does not take the place of <tr> which is always required.
You can always check the HTML5 spec if you are in doubt about which elements are required and which are optional:
The tr element represents a row of cells in a table. Permitted
contents Zero or more of: one td element, or one th element
If you look at the HTML 4 spec or HTML5 Spec, you will see
HTML 4:
<!ELEMENT TR - O (TH|TD)+ -- table row -->
<!ATTLIST TR -- table row --
%attrs; -- %coreattrs, %i18n, %events --
%cellhalign; -- horizontal alignment in cells --
%cellvalign; -- vertical alignment in cells --
>
HTML5:
4.9.8 The tr element
Content model: Zero or more
td, th, and script-supporting elements
Notice the TH and TD? Those are the only two child elements allowed.
What happens when you add an invald element to the TR is up to the browser. Some will try to figure out what you are doing, others will remove it from the flow and add it after. Write valid code so the browser does not have to guess.
Since you have a class of title, it seems like you should not be using a row. If you want a title row on the table, you want to use the <caption> element.
From MDN:
The HTML <caption> Element (or HTML Table Caption Element) represents
the title of a table. Though it is always the first descendant of a
<table>, its styling, using CSS, may place it elsewhere, relative to
the table.
Basic usage:
<table summary="Description Text">
<caption>My Table Of Numbers</caption>
<thead>
<tr>
<th>C 1</th>
<th>C 2</th>
<th>C 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1-1</td>
<td>1-2</td>
<td>1-3</td>
</tr>
<tr>
<td>2-1</td>
<td>2-2</td>
<td>2-3</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>C 1</th>
<th>C 2</th>
<th>C 3</th>
</tr>
</tfoot>
</table>
JSFiddle: http://jsfiddle.net/vM688/
According to spec, a tr can contain only th and td elements. As other have said, don't be lazy and write the few extra characters to make your HTML valid
Use
<table>
<thead>
<tr>
<!-- text b/w <th></th> will bold and center aligned by default -->
<th> You title here </th>
</tr>
</thead>
<tbody>
<!-- Content of you table here -->
</tbody>
</table>
A td tag stands for table data whereas a tr tag is a table row.
So you make a row in HTML and then add a td to it the number of tds you add decides the number of columns in that row.
A th tag stands for table heading which can be used in a row.
The background color will show if you add the span tag inside the td. Your code will look somewhat like this:
<table><tr class="bg-color"><td><span class="title">My title</span></td></tr></table>
And the css should go like:
.bg-color{background:#ff0000;}

Is it really bad idea to group tr tags with div?

I'm developing application with Backbone.js
View class returns one element after rendering. It's ok if I use divs or spans.
But problem starts when I start to render objects as tr rows.
One objects should be rendered to 2-3 rows.
So can I use this structure?
<table>
<div>
<tr>...</tr>
<tr>...</tr>
</div>
</table>
divs immediately inside a table tag is invalid. use tbody instead
Yes, it is a very bad idea.
The HTML specification does not allow div elements to be child elements of a table element, so it triggers error recovery routines in browsers.
Given this input:
<table>
<tr><td>1</td></tr>
<div>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
</div>
<tr><td>4</td></tr>
<div>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
</div>
The DOM that Firefox will generate (and you can see this by using the Inspect Element feature) will look like this:
<div>
</div><div>
</div><table>
<tbody><tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
</tbody></table>
Note that the div elements have been removed from the table and placed before it. This makes them useless for manipulating the rows.
HTML provides the thead, tfoot and tbody elements to group table rows. Use the appropriate ones of those instead of div elements (you can have only one thead and only one tfoot, but there are no limits on the number of tbody elements).
That is not valid HTML. You can not nest block or inline elements in a table, only table elements, such as <tbody>, <tr> or <thead>. You can of course nest a <div> in a table cell (<td> or <th>).

Hierarchical html table, putting last td on next line

I'm creating a simple hierarchical table with html and CSS and I'm getting into trouble with formatting the last td element with class .child to be on next line.
I want to have the nested table inside table > tr > td.child becase each table can be sorted and javascript sorters don't implement any grouping of rows (my problem of having nested table could be easily solved by moving the .child > table element into next table > tr however this would break the nice nesting structure)
Is there a way to put td.child on next row with css?
html sample:
<table>
<tr>
<td>I have</td>
<td>1</td>
<td>pie</td>
<td class="child">
<table>
<tr>
<td>I have</td>
<td>1</td>
<td>pie</td>
</tr>
</table>
</td>
</tr>
</table>
You could do something like this . You'd need to be careful cross browser though (only checked on Chrome)