Display rows of a table side by side - html

I have one table with two columns and e.g 24 rows (loaded from a database, so its a dynamic table). Now I want the table rows displayed side by side automatically (as it fits the screen), e.g. the left part holds 12 rows and the right part holds 12 rows, or (if the screen is wide enough) e.g. three columns with 8 rows and so on.
Is that possible with html/css?
Example:
This would display the table normally:
<table>
<thead><tr><th>Col 1</th><th>Col 2</th></tr></thead>
<tbody>
<tr><td>1.1</td><td>1.2</td></tr>
<tr><td>2.1</td><td>2.2</td></tr>
</tbody>
</table>
This is what I want to have (the number of parts of the table placed side by side depends on screen size and table size):
<table style="float: left;">
<thead><tr><th>Col 1</th><th>Col 2</th></tr></thead>
<tbody>
<tr><td>1.1</td><td>1.2</td></tr>
</tbody>
</table>
<table style="float: right;">
<thead><tr><th>Col 1</th><th>Col 2</th></tr></thead>
<tbody>
<tr><td>2.1</td><td>2.2</td></tr>
</tbody>
</table>

Short answer: If you need something responsive it will be a little harder with just tables. I suggest using bootstrap + tables.
So each table will look like this:
<div class="col-xs-6 col-sm-4 col-md-3">
<table class="blue">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.1</td>
<td>1.2</td>
</tr>
<tr>
<td>2.1</td>
<td>2.2</td>
</tr>
</tbody>
</table>
</div>
Here is a live example: https://jsfiddle.net/xwazzo/7x0v9hL6/
Note that you will need a big screen to see the responsive on jsfiddle.
Long answer:
If you want responsive tables, there is a great article about that in CSS Tricks
https://css-tricks.com/accessible-simple-responsive-tables/

If you have the ability to use divs instead of table elements, then I would suggest writing the whole thing out using divs and use css to make the divs act like a table. I write a mobile first approach, so I coded it to look like a standard table on mobile, then as you increase in screen size you get the look you want. Obviously you'd play with break points and adjust how wide each "group" is for each screen size to get the appropriate number of columns you want. Unfortunately, you have to repeat your table headers at every point, it's just unavoidable doing what you are looking to do... however you can hide them on mobile.
HINT: shrink the screen on the fiddle to see a "mobile" version of the table... expand it to see a larger one. There's only two sizes for demo. Add as many as you'd like.
HTML MARKUP:
<div class="table">
<div class="group">
<div class="table-row table-head table-head-main">
<div class="table-cell">Col 1</div>
<div class="table-cell">Col 2</div>
</div>
<div class="table-row">
<div class="table-cell">1.1</div>
<div class="table-cell">1.2</div>
</div>
</div>
<div class="group">
<div class="table-row table-head">
<div class="table-cell">Col 1</div>
<div class="table-cell">Col 2</div>
</div>
<div class="table-row">
<div class="table-cell">2.1</div>
<div class="table-cell">2.2</div>
</div>
</div>
</div>
CSS CODE:
.table {
display: table;
width: 100%;
}
.table-row {
display: table-row;
width: 100%;
}
.table-head div {
background: #cccccc;
}
.table-cell {
display: table-cell;
border: 1px dotted #000000;
}
.table-head {
display: none;
}
.table-head-main {
display: table-row;
}
.group {
display: table-row-group;
}
#media (min-width: 600px) {
.table-head {
display: table-row;
}
.group {
display: table;
float: left;
width: 50%;
}
}
https://jsfiddle.net/5s3cz15t/1/

What you want to do should not be possible with standard html tables without fundamentally breaking how tables work (and I'm not even sure if you could modify the CSS in a way that would get you your desired outcome).
As suggested by #Daniel C you might want to consider using divs instead of tables.
In cooperation with a responsive grid layout like those Bootstrap or Foundation offer, what you'd like to do should be possible.

Related

Is there a way to have each row in an HTML table wrapped seperately and not share a seperator line

I have a table within a webpage I'm building, where either the left or right columns (but not both) will have some fairly-long text (a handful of words). The table has two columns - the first column is left-aligned, and the second column is right-aligned. Here's a simplified version of my table:
<table>
<tr>
<td>Some rather long text</td>
<td class=right-align>Short text</td>
</tr>
<tr>
<td>Text</td>
<td class=right-align>Some very long piece of text</td>
</tr>
</table>
It also has a small bit of CSS that goes with it:
.right-align {
text-align: right;
}
table {
width: 100%;
}
At a reasonable width, it looks fine:
Unfortunately, if I shrink the window, the text wraps:
Is there a way to have the text proceed through the "column separator", and allow that border to be at a different place for each row? There would be room for each row to take up only one line if the "column separator" could be different for each row. I am not using any libraries, just CSS + HTML.
I'm looking for it to do something like this (drawing made in MS Paint)
No, not with classic html tables. You will have to use grid or flexbox to achieve your desired effect.
use two tables
use flex box
use a div
If you need to use html <table> syntax, create one table for each row of data, your CSS will work.
Alternatively use e.g. <div> tags with CSS flexbox layout applying flex: auto to the col class, to distribute the available space.
Empty space distribution is handled differently by either approach. Check their behavior in the snippet below to see how the empty space distribution is handled when resizing the available document width (browser window).
Obviously text will start wrapping, once the width of a cell/div is fully occupied.
I've added colored borders/background and removed the table's cellpadding and cellspacing to better illustrate the behavior of the 2 examples.
.right-align {
text-align: right;
}
table {
width: 100%;
}
td {
border: 1px solid red;
}
.wrap {
width: 100%;
background-color: gold;
margin-top: 10px;
}
.row {
display: flex;
}
.col {
border: 1px solid blue;
flex: auto;
}
.col:last-of-type{
text-align: right;
}
<table cellpadding="0" cellspacing="0">
<tr>
<td>Some rather long text</td>
<td class=right-align>Short text</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0">
<tr>
<td>Text</td>
<td class=right-align>Some very long piece of text</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0">
<tr>
<td>Some even more rather long text</td>
<td class=right-align>txt</td>
</tr>
</table>
<div class="wrap">
<div class="row">
<div class="col">
Some rather long text
</div>
<div class="col">
Short text
</div>
</div>
<div class="row">
<div class="col">
Text
</div>
<div class="col">
Some very long piece of text
</div>
</div>
<div class="row">
<div class="col">
Some even more rather long text
</div>
<div class="col">
txt
</div>
</div>
</div>

How to set an absolute margin to an element using CSS or Bootstrap?

I have several div elements where inside every div there is a table between some paragraphs (p) using the following code:
<p style="display: inline-block;">
Something BEFORE the table
<table style="display: inline-block;">
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
</tr>
</tbody>
</table>
Something AFTER the table
</p>
which produces a nice content that looks something like this:
head
Something BEFORE the table Something AFTER the table
data
However on each div there are different content lengths BEFORE and AFTER the table making it look like this:
head
Something BEFORE the table Something AFTER the table
data
head
Short BEFORE the table Short AFTER the table
data
head
Something long BEFORE the table Something long AFTER the table
data
What I want is set some "margin" to every table so they are a set distance from the beginning of their parent element (p on this case) so it will hopefully look like this:
head
Something BEFORE the table Something AFTER the table
data
head
Short BEFORE the table Short AFTER the table
data
head
Something long BEFORE the table Something long AFTER the table
data
The BEFORE, table, and AFTER elements of the page must be handled like this as having each of these on their own div and displaying them side by side will mess with this page section styling and also will produce a similar problem but now oriented vertically (however if your solution requires to do this please do share... maybe I'll end up using it).
P.D: If your solution includes Bootstrap please use version 3.
P.D.2: I'm sorry about how messy the examples look I'm still getting used to this.
Wrap it in a table structure. This can be done with div's styled as tables. This way you can make it responsive.
! Do not ever again put other block level elements in a p element
See: Why is <table> not allowed inside <p>
Below is the HTML of what you need:
.table{
display: table;
widht: 100%;
}
.row {
display: table-row;
}
.cell{
display: table-cell;
padding: 0 7.5px;
}
<div class='table'>
<div class='row'>
<div class='cell'>
Something BEFORE the table
</div>
<div class='cell'>
<table>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
</tr>
</tbody>
</table>
</div>
<div class='cell'>
Something AFTER the table
</div>
</div>
<div class='row'>
<div class='cell'>
Something LONGGGGGG BEFORE the table
</div>
<div class='cell'>
<table>
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
</tr>
</tbody>
</table>
</div>
<div class='cell'>
Something AFTER the table
</div>
</div>
</div>
Here is a simple and easy solution:
.before-table {
min-width: 250px;
display: inline-block;
}
<p style="display: inline-block;">
<div class="before-table">Something BEFORE the table</div>
<table style="display: inline-block;">
<thead>
<tr>
<th>header</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
</tr>
</tbody>
</table>
<span>Something AFTER the table</span>
</p>
To set an absolute position you can use position: absolute on the element you're trying to position. It will get positioned at coordinates (0,0) (top-left) of it's first non-static parent. (By default every element has a position set to static, until you explicitly set it to something else). So that means you also need to assign a position to the parent (p I'm your case) so that it overwrites the default. Something like position: relative on the parent should do the job.
After that you can use the "top, right, bottom, left" CSS properties respectively to set a custom position from the parent's top/right/bottom/left border.
p {
position: relative;
}
table {
position: absolute;
left: 150px; // or whatever distance works best
}
Something like this, or it's equivalent inline version should do.

Responsive CSS table

I created a table width 4 images which are placed in a row and four columns (1x4).
<table class="insrtTable">
<tr>
<td><img src=Guitarra.png></td>
<td><img src=Bajo.png></td>
<td><img src=Teclado.png></td>
<td><img src=Ukelete.png></td>
</tr>
</table>
My problem is that I need to see the images in my cellphone in two rows and two columns(2x2).(like this)
<table class="insrtTable">
<tr>
<td><img src=Guitarra.png></td>
<td><img src=Bajo.png></td>
</tr>
<tr>
<td><img src=Teclado.png></td>
<td><img src=Ukelete.png></td>
</tr>
</table>
How can I do that? I see a lot of responsible tables that transform 1 column and 4 rows into 4 columns and 1 row but I can't find one that works for me.
Try using a grid system like Flexbox or Bootstrap grid system. You can make your own grid system as well, but you will be just reinventing the wheel.
Responsiveness is also tied with how you mark your CSS properties, a simple example:-
<div class="container">
Text content </div>
.container {
width:3px; <!-- vs width:3%; --> }
Using % values instead of pixel hardcoded values, can make a difference in your website responsiveness.
A useful source for developing your own grid system- https://zellwk.com/blog/responsive-grid-system/
To answer the actual question, yes you can make somewhat responsive tables, but that would involve unnecessary hacking since tables where never meant to be responsive back in the 70's when they were created. And you'd probably run out of options when trying to do more advanced stuff with them.
A modern approach to layouts in CSS is using something like flexbox. You could solve the problem like this:
<div class="container">
<div class="col">1</div>
<div class="col">2</div>
<div class="col">3</div>
<div class="col">4</div>
</div>
.container {
display: flex;
flex-wrap: wrap;
}
.container > .col {
background: tomato;
padding: 16px;
border: 5px solid black;
width: 50%;
}
#media (min-width: 599px) {
.container > .col {
width: 25% !important;
}
}
See it in action here: https://codepen.io/nicooga/pen/MEwZgZ. The key is flex-wrap: wrap, which allows elements overflowing into the next row if they exceed the container's size.
All you need to know about flexbox is here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/.
It seems there's a brand new grid system in native css that seems to do all that was great about flexbox and more: https://css-tricks.com/snippets/css/complete-guide-grid/.
The word you're looking for is "responsive", not "responsible."
How can i do that? i see a lot of responsible tables that transform 1 column and 4 rows into 4 columns and 1 row but i cant find one that works for me.
Most use Bootstrap columns to achieve this.
You can try this simple solution.
First, add a td placeholder where you want to break row (td with class resp)
<table class="insrtTable">
<tr>
<td><img src="img1.png"></td>
<td><img src="img2.png"></td>
<td class="resp"></td>
<td class=""><img src="img3.png"></td>
<td class=""><img src="img4.png"></td>
</tr>
</table>
then you apply the following styles to the table:
#media only screen and (max-width: 767px), (min-device-width: 768px) and (max-device-width: 1024px) {
thead, tbody, th, td.resp, tr {
display: block;
}
}
When you reduce the size of browser window under 768px it'll break 2 images on the first row and 2 on the second row.
You can apply this methodology either to more than 4 columns, setting placeholder where you need.
Responsive design cannot be made with tables. Use other ways. I suggest flex-container or float and clear.
This website is for flex containers.

How to make html table take up 100% of parent element width?

My table has a nested table for one of its rows. I would like both tables to take up 100% of the parent element width. How do I do that?
Demo
HTML
<div class="container">
<table>
<tr class="row">
row 1
</tr>
<tr class="requestDetails row">
<td>
<tr class="requestDetailsHeading">
<td>Headingname</td>
</tr>
<tr class="requestRow">
<td>name</td>
<td>date</td>
<td>age</td>
</tr>
</td>
</tr>
<tr class="row">
<td>gg</td>
<td>dd</td>
<td>ee</td>
</tr>
</table>
Drawing on the other answers, in a roundabout way, yes they do have an element of correctness, unfortunately none of them has the full story.
As Justinas points out, you're not nesting tables, what you're nesting are rows. While row nesting will indeed work, it is actually now not supported under the new HTML5 schemes.
This means that trying to do what you're doing, will simply not validate, and worse will refuse to render correctly on mobile devices.
Working with your existing code:
<div class="container">
<table>
<tr class="row">
row 1
</tr>
<tr class="requestDetails row">
<td>
<tr class="requestDetailsHeading">
<td>Headingname</td>
</tr>
<tr class="requestRow">
<td>name</td>
<td>date</td>
<td>age</td>
</tr>
</td>
</tr>
<tr class="row">
<td>gg</td>
<td>dd</td>
<td>ee</td>
</tr>
</table>
</div>
You can achieve what you're trying to do by adding a width of 100% to the table's style as others have already pointed out, and by adding a width:100% to requestDetailsHeading class.
However, I'm going to take a guess here, and looking at your other class names (specifically container and row) I suspect you might actually be using the Bootstrap CSS framework. If you're not then perhaps you might want to consider doing so, as it will make the task you're trying to do much easier and you'll have less fiddling about to do.
You can download the various CSS files from
http://getbootstrap.com/
And once you have a page set-up with BS in place, you can get the exact effect you want by using the following HTML
<div class="container">
<table class="table">
<tr> <!-- NOTE: Don't use the 'row' class here as BS3 has another use for that -->
<td colspan="3">
row 1
</td>
</tr>
<tr class="requestDetailsHeading">
<td colspan="3">HeadingName</td>
</tr>
<tr class="requestRow">
<td>Name</td>
<td>Date</td>
<td>Age</td>
</tr>
<tr class="requestData">
<td>gg</td>
<td>dd</td>
<td>ee</td>
</tr>
</table>
</div>
Even without bootstrap added however, you'll notice that I've simplified the HTML.
To get the effect you're looking for of a 100% row, above each row of data, you don't need to nest things the way you did, you simply just need to tell the td element how many columns it has to span, and as long as that is equal to the rest of the table, you'll end up with a 100% width header across separate columns. If you decide to use Bootstrap, then BS will take care of giving you a 100% table width, otherwise as others have mentioned simply add a width of "100%" to a class that controls the table itself.
Additional (But not required to solve your problem)
If you decide to use Bootstrap as your CSS framework, there is another way that you can achieve what you're trying to achieve, and that's to use the BS3 grid system.
Using 'container' s, 'row' s and 'col-md-xx' style classes, you could very easily do something like the following:
<div class="container">
<div class="row">
<div class="col-md-12">
Row Header Text Goes Here
</div>
</div>
<div class="row">
<div class="col-md-4">Name</div>
<div class="col-md-4">Date</div>
<div class="col-md-4">Age</div>
</div>
<div class="row">
<div class="col-md-4">gg</div>
<div class="col-md-4">dd</div>
<div class="col-md-4">ee</div>
</div>
</div>
Because of the way Bootstrap works, the container will automatically take up 100% of the center column (approx 1024 pixels) and each of your rows will take up the appropriate space in the 12 column grid that's available by default.
Your data rows are set to column widths of 4 grids, as 3 times 4 is 12, and it's easy to repeat the 'div' sections as needed in order to produce as many rows as needed.
Finally, if you use 'container-fluid' rather than 'container' in your outermost div, then your layout will span the entire width of the visible page.
The best part about going the bootstrap route however, is that everything you do using it is automatically responsive, and so will adapt and resize automatically for mobile and desktop as needed, especially if you start using a mixture of 'col-xx-yy' column types, where xx represents the device target size, and yy the number of grid columns you wish to consume.
Fiddle
table{
background-color:white;
width:100%;
}
You don't have nested tables. You have tr > td > tr > td that I think is not valid.
Also, first row don't have td element.
Simply apply width: 100% to all tables:
table {
background-color: white;
width: 100%;
}
.requestDetails {
background-color: red;
}
.container {
width: 600px;
background-color: green;
}
.row {
width: 100%;
background-color: blue;
}
<div class="container">
<table>
<tr class="row">
row 1
</tr>
<tr class="requestDetails row">
<td>
<tr class="requestDetailsHeading">
<td>Headingname</td>
</tr>
<tr class="requestRow">
<td>name</td>
<td>date</td>
<td>age</td>
</tr>
</td>
</tr>
<tr class="row">
<td>gg</td>
<td>dd</td>
<td>ee</td>
</tr>
</table>
</div>
Just add width: 100%; to the table in CSS.
Updated jsFiddle
Readup: CSS width | MDN
Just update your css like below:
.container table{
background-color:white;
width:100%;
}
If the width attribute is not set, table takes up the space it needs to display the table data. so you have to define the width of table.
so just define the width for table in CSS.
.row, table{
width:100%;
background-color:blue;
}

html table should overflow

I have a table with two columns. The first (which contains a menu) should have a
fixed width, while the second (containing some page content) can vary in width. The table should overflow the window (which it doesn't by default), because otherwise the browser reduces the width of the menu column if the content is very broad. But I cannot define a fixed width for the table (causing it to overflow) because I don't know the width of the content.
Overflow:scroll
does not seem to work with tables. I would be thankful for workarounds/solutions.
<table class="rootTableContent">
<tr>
<td id="rootTableMenu">
</td>
<td id="rootTableContent">
</td>
</tr>
The solution to this problem is to use proper CSS (Divs/Spans, etc) to layout your website as opposed to tables. I'm all for using tables to display tabular data and you'll see me arguing for them in places that they're valid, but this is not one of them.
This is easily done with something like this:
<div style="float:left; width: 150px">
Navigation Code Here
</div>
<div style="float: left">
Other Content Here
</div>
<div style="clear:both"></div>
Obviously, I'm oversimplifying this solution, you're going to have more specific code to deal with your layout (need more detail to help more specifically) But, it's important to use the right tools for the job.
As others have stated, please don't use <table> layouts. It's old, clunky, and confuses screen readers and other accessibility software.
If you absolutely insist on using your method, you can try this:
Live Demo
<style type="text/css">
div.wrap {
overflow-y: auto;
width: 75%;
}
div.wrap table {
border: 1px solid #000;
width: 100%;
}
div.wrap table td {
padding: 20px;
}
</style>
<div class="wrap">
<table class="rootTableContent">
<tr>
<td id="rootTableMenu">rootTableMenu</td>
<td id="rootTableContent">rootTableContent</td>
</tr>
</table>
</div>