Responsive CSS table - html

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.

Related

Display rows of a table side by side

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.

HTML table show last column using media queries

Is there a way i can show only the first and last table column (last being the far right column) using CSS media queries?
<table>
<tr>
<td>Service</td>
<td class="big">one</td>
<td class="big">two</td>
<td class="big">three</td>
<td class="big">four</td>
<td class="big">five</td>
<td class="big">six</td>
<td class="big">seven</td>
</tr>
</table>
so in the above, how can i display just the first and last columns (Service and seven)
i have tried using media queries to hide the td with a class of big but i need a way of keeping the last one (i cannot remove the class because its generated programatically)
Hide all td elements by default, then in a media query show the ones you want
eg.
<style>
td.big {
display: none;
}
#media (max-width: 600px) {
td.big:first-child, td.big:last-child {
display: table-cell;
}
}
</style>
You can maybe use the nth-child selector property to hide all the columns you don't need.

How do I limit the number of cells on a row in CSS

I'm editing my forum for mobile devices. So, I'm using media queries to format the front page based on device width.
My issue is that all of the categories have this style, which puts the td elements all in a row.
<tr class="windowbg2">
<td class="icon windowbg">
</td>
<td class="info">
</td>
<td class="stats windowbg">
</td>
<td class="lastpost">
</td>
</tr>
I don't want to get into the PHP that creates the rows, and my attempts/searches so far haven't worked how I want.
I want have the icon td and the info td squished onto one row, and under it have the stats and lastpost tds. How would I separate this row into two using CSS alone?
HTML
<table>
<tr class="windowbg2">
<td class="icon_windowbg">
icon windowbg
</td>
<td class="info">
info
</td>
<td class="stats_windowbg">
stats windowbg
</td>
<td class="lastpost">
lastpost
</td>
</tr>
</table>
CSS
table ,td {
border: 1px solid #000;
}
table {
width: 100%;
}
td {
width: 49%;
float: left;
display: block;
}
The border on the table is just so you can see it. You can take that off.
DEMO
http://codepen.io/anon/pen/xbdjoW?editors=110
You'll want to use a grid framework like Twitter's bootstrap or Thoughtbots "Bourbon neat". These allow you to specify how large an area will be with a main container. Then how much an each element will take up in that container.
with bourbon neat you could use
tr {
display:block # you're probably better off using div's though
#include outer-container;
}
ele {
#include span-columns(6);
}
this would effectivily in a 12 column grid put the two side by side in a row if your elements were
<tr>
<td class="ele"></td>
<td class="ele"></td>
</tr>
you can use this logic to make the other ones span the full 12 columns in the next row.
of course this can all be done in regular css with max-widths and more. grids just have done alot of the work for you. bootstrap is even easier to use. i just prefer bourbons more hands-off approach.

Keep table at a set width and have overflow in mobile

I am struggling to keep a table within a table to stay at a set width and in mobile to have a horizontal scroll.
That way nothing gets squished and i can maintain readability. I am attempting to use pure CSS before resorting to javascript/jQuery.
Current example:
(LINK HAS BEEN REMOVED)
The DOM structure i currently have:
<table>
<tr>
<td>Vessel name and $</td>
<td>
<div class="container">
<table>
<tr>
<td>Sold</td>
<td>Sold</td>
<td>Available</td>
<td>Sold</td>
</tr>
</table>
</div>
</td>
<td>Proceed button</td>
</tr>
</table>
The table in the container i want to keep at a set width e.g 500px and when we go into mobile i'd like to be able to have a horizontal overflow so users can swipe/scroll through the availability for that particular vessel.
I have a development page which illustrates what i'd like to achieve, it also contains CSS that i've used so far (note, still tinkering).
(LINK HAS BEEN REMEOVED)
A possibility would be media queries, to make the div scrollable on small resolutions.
#media(max-width: 767px) {
.container {
overflow-x: scroll;
}
}

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>