<div> tag can not be used above <tr> tag. Instead you can use <tbody> tag to do your work. If you are planning to give id attribute to <div> tag and doing some processing, same purpose you can achieve through <tbody> tag.
For further information visit this page
For example:
<table>
<tbody class="green">
<tr>
<td>Data</td>
</tr>
</tbody>
<tbody class="blue">
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
secondly, you can put "div" tag inside "td" tag.
<table>
<tr>
<td>
<div></div>
</td>
</tr>
</table>
Further questions are always welcome.
Answer from Aditya Ekbote on Stack Overflow<div> tag can not be used above <tr> tag. Instead you can use <tbody> tag to do your work. If you are planning to give id attribute to <div> tag and doing some processing, same purpose you can achieve through <tbody> tag.
For further information visit this page
For example:
<table>
<tbody class="green">
<tr>
<td>Data</td>
</tr>
</tbody>
<tbody class="blue">
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
secondly, you can put "div" tag inside "td" tag.
<table>
<tr>
<td>
<div></div>
</td>
</tr>
</table>
Further questions are always welcome.
You can't put a div directly inside a table but you can put div inside td or th element.
For that you need to do is make sure the div is inside an actual table cell, a td or th element, so do that:
HTML:-
<tr>
<td>
<div>
<p>I'm text in a div.</p>
</div>
</td>
</tr>
For more information :-
http://css-tricks.com/using-divs-inside-tables/
html - Why are people making tables with divs? - Software Engineering Stack Exchange
How to put DIV between table rows? - HTML & CSS - SitePoint Forums | Web Development & Design Community
html - How create table only using <div> tag and CSS - Stack Overflow
How create table only using <div> tag and Css - HTML & CSS - SitePoint Forums | Web Development & Design Community
Videos
This is a common pattern for making responsive tables. Tabular data is tricky to display on mobiles since the page will either be zoomed in to read text, meaning tables go off the side of the page and the user has to scroll backwards and forwards to read the table, or the page will be zoomed out, usually meaning that the table is too small to be able to read.
Responsive tables change layout on smaller screens - sometimes some columns are hidden or columns are amalgamated, e.g. name and email address might be separate on large screens, but collapse down into one cell on small screens so the information is readable without having to scroll.
<div>s are used to create the tables instead of <table> tags for a couple of reasons. If <table> tags are used then you need to override the browser default styles and layout before adding your own code, so in this case <div> tags save on a lot of boilerplate CSS. Additionally, older versions of IE don't allow you to override default table styles, so using <div>s also smooths cross-browser development.
There's a pretty good overview of responsive tables on CSS-Tricks.
Edit: I should point out that I'm not advocating this pattern - it falls into the divitis trap and isn't semantic - but this is why you'll find tables made from divs.
The question is if the data is semantically a table (i.e. a set of data or units of information logically organized in two dimensions) or you just use the grid for visual layout, e.g. because you want a sidebar to expand or something like that.
If your information is semantically a table, you use a <table>-tag. If you just want a grid for layout purposes, you use some other appropriate html elements and use display:table in the style sheet.
When is data semantically a table? When the data is logically organized along two axes. If it makes sense with headers for the rows or columns, then you might have a semantic table. An example of something which is not semantically a table is presenting a text in columns like in a newspaper. This is not semantically a table, since you would still read it linearly, and no meaning would be lost if the presentation was removed.
OK, why not use <table> for everything rather than only for something that is semantically a table? Visually it is obviously the same (since the table element just have display:element in the default style sheet).
The difference is that the semantic information can help alternative user agents. For example a screen reader might allow you to navigate in two dimensions in the table, and read the headers for a cell for both axes if you forget where you are. This would just be confusing if the table was not semantically a table but just used for visual layout.
The <table> versus display:table discussion is just a case of the more general principle of using semantic markup.
See for example: Why would one bother marking up properly and semantically? or Why is semantic markup given more weight for search engines?
In some places you might actually be legally required to use semantic markup for accessibility reasons, and in any case there is no reason to purposefully make your page less accessible.
Even if you don't care for disabled users, having presentation separate from content gives you benefits. E.g. your three column layout could be presented in a single columns on a mobile using an alternative stylesheet.
.div-table {
display: table;
width: auto;
background-color: #eee;
border: 1px solid #666666;
border-spacing: 5px; /* cellspacing:poor IE support for this */
}
.div-table-row {
display: table-row;
width: auto;
clear: both;
}
.div-table-col {
float: left; /* fix for buggy browsers */
display: table-column;
width: 200px;
background-color: #ccc;
}
Runnable snippet:
.div-table {
display: table;
width: auto;
background-color: #eee;
border: 1px solid #666666;
border-spacing: 5px; /* cellspacing:poor IE support for this */
}
.div-table-row {
display: table-row;
width: auto;
clear: both;
}
.div-table-col {
float: left; /* fix for buggy browsers */
display: table-column;
width: 200px;
background-color: #ccc;
}
<body>
<form id="form1">
<div class="div-table">
<div class="div-table-row">
<div class="div-table-col" align="center">Customer ID</div>
<div class="div-table-col">Customer Name</div>
<div class="div-table-col">Customer Address</div>
</div>
<div class="div-table-row">
<div class="div-table-col">001</div>
<div class="div-table-col">002</div>
<div class="div-table-col">003</div>
</div>
<div class="div-table-row">
<div class="div-table-col">xxx</div>
<div class="div-table-col">yyy</div>
<div class="div-table-col">www</div>
</div>
<div class="div-table-row">
<div class="div-table-col">ttt</div>
<div class="div-table-col">uuu</div>
<div class="div-table-col">Mkkk</div>
</div>
</div>
</form>
</body>
divs shouldn't be used for tabular data. That is just as wrong as using tables for layout.
Use a <table>. Its easy, semantically correct, and you'll be done in 5 minutes.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
</head>
<body>
<table>
<tr>
<td>
<div>content</div>
</td>
</tr>
</table>
</body>
</html>
This document was successfully checked as XHTML 1.0 Transitional!
You can't put a div directly inside a table, like this:
<!-- INVALID -->
<table>
<div>
Hello World
</div>
</table>
Putting a div inside a td or th element is fine, however:
<!-- VALID -->
<table>
<tr>
<td>
<div>
Hello World
</div>
</td>
</tr>
</table>
I know this may sound like a stupid question, but I am a self-taught developer that's kinda just learning as I go.
I've been building alot of my websites using tables instead of div's... It was mentioned to me that using tables instead of div's is just "stupid".... Wasn't really given an explanation as to why, so I am just curious... Why?
EDIT: I've received some just amazing information you guys, thanks a ton!
| sidebar |
| content |
I want to make sure my table fits cleanly inside a div that takes up 1/6 of my horizontal screen space.
My current table spills out of the div and I have tried using the table.style.width = "100%" in javascript to set my table back to my div dimensions, but I can't seem to get it to work.
I have a codepen here, https://codepen.io/retug/pen/wvmOKdg, note you must click on the tab "rebar" to make the table popup. You should see the table overflow into the adjacent div.
Any ideas on how to fix?
Code of interest:
function rebarSelection() {
...
let table = document.getElementById("rebarData")
table.innerHTML= ''
let row = document.createElement('tr');
let Xpnt = 2
let Ypnt = 1
let Xdata = document.createElement('td')
let Ydata = document.createElement('td')
let rebarData = document.createElement('td')
var Xinput = document.createElement("input");
var Yinput = document.createElement("input");
var rebarInput = document.createElement("input");
Xinput.type = "Number";
Yinput.type = "Number";
rebarInput.type = "Number";
Xinput.value = Xpnt
Yinput.value = Ypnt
rebarInput.value = 5
Xdata.appendChild(Xinput)
Ydata.appendChild(Yinput)
rebarData.appendChild(rebarInput)
row.appendChild(Xdata)
row.appendChild(Ydata)
row.appendChild(rebarData)
table.appendChild(row)
table.style.width = "100%"
}Thanks!