convert fixed tables to responsive for mobile - css

the URL is http://action.news
there are 3 columns in a fixed width table)
(i removed the script that loads on demand) to make things easier.
thank you kindly
what i have done:
added this code below
https://gist.github.com/hkirsman/3002480
added this per google instructions
<meta name=viewport content="width=device-width, initial-scale=1">
i have read HTML table with auto-fit for some columns, fixed width for others
but still cant get the page to be mobile friendly, the top part seems to realign

I've found this example on the Codepen so you can try it and adjust it to your needs:
Here's some code
HTML:
<table class="js-table participants-table">
<caption>
<p>Responsive Table</p>
</caption>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Picture</th>
</tr>
</thead>
<tbody>
<!--filled from javascript-->
</tbody>
</table>
CSS:
HTML {
font-size: 100%;
line-height: 1.5;
font-family: 'Raleway', sans-serif;
}
BODY {
margin: 0;
}
*, *:before, *:after {
box-sizing: border-box;
}
IMG {
max-width: 100%;
}
/*Style For Table*/
.participants-table {
table-layout: fixed;
border-collapse: collapse;
margin: 0 auto;
width: 90%;
}
.participants-table TD,
.participants-table TH {
border: 1px solid #b4adad;
}
.participants-table IMG {
width: 150px;
}
.participants-table THEAD {
display: none;
}
.participants-table TD {
display: block;
position: relative;
overflow-x: hidden;
text-overflow: ellipsis;
}
#media only screen and (min-width: 450px) {
.participants-table {
width: auto;
}
.participants-table TD,
.participants-table TH {
padding: .2em 1em;
}
.participants-table THEAD {
display: table-header-group;
}
.participants-table TD {
display: table-cell;
position: static;
}
.participants-table TD:before {
display: none;
}
}
link to the example

Related

Table cell conundrum: trying to vertically center a link inside a table cell whose entire cell area is link-clickable

As you can see, in order to make the entire table cell link-clickable, I used td a {display: block}. The problem is td a {vertical-align: middle} no longer works with display: block. Not sure how else to center it vertically. Any help would be greatly appreciated.
P.S. I avoided using line-height because my link needs to be multi-line.
table {
width: 300px;
border-collapse: collapse;
}
td {
border: 1px solid #000000;
height: 100px;
vertical-align: middle;
}
td a {
display: block;
height: 100px;
vertical-align: middle !important;
}
<table>
<tr>
<td>TEXT</td>
<td>LINK</td>
</tr>
</table>
You can use an auxiliar span and center it inside the anchor, so its content is free to move and align.
E.g.:
table {
width: 300px;
border-collapse: collapse;
}
td {
border: 1px solid #000000;
height: 100px;
vertical-align: middle;
}
td a {
display: flex;
height: 100%;
}
td a span {
height: fit-content;
align-self: center;
}
<table>
<tr>
<td>TEXT</td>
<td>
<a href="">
<span>
A VERY LONG LINK MAY BE LIKE THIS ONE, I GUES, RIGHT? HERE WE GO :D
</span>
</a>
</td>
</tr>
</table>
Added span in anchor tag and made a CSS change for the output
table {
width: 300px;
border-collapse: collapse;
}
td {
border: 1px solid #000000;
height: 100px;
vertical-align: middle;
}
td a {
display: table;
position: relative;
height: 100%;
width: 100%;
}
span {
display: table-cell;
text-align:center;
vertical-align: middle;
}
<table>
<tr>
<td>TEXT</td>
<td><span>LINK</span></td>
</tr>
</table>

How to add a fixed width of td table

I set a fixed width in td, but it doesn't work when the table is overflow. What I have tried is the below code.
td {
width: 192px;
}
It can resolve by following 2 steps.
1 - On table set width:100%
2 - On td of table use class like this when your data get overflow.
.dataRow
{
word-wrap: break-word;
word-break: break-all;
}
3 - after above 2, you can also set width as per your requirement, if needed.
You can change in css with td { width:20px} or you can change it inline <td style="width:20px">.
This can be achieve using display property.
Check below Snippet.
.wrapper {
width: 180px;
border: 1px solid #000;
}
.table-scroll {
width: 100%;
table-layout: fixed;
}
.table-scroll tbody {
display: block;
overflow: scroll;
}
.table-data {
border: 1px solid #0F0;
}
.table-detail {
border: 1px solid #F00;
}
td.first {
width: 200px;
display: inline-block;
}
<div class="wrapper">
<table class="table-scroll table-data">
<tbody>
<tr>
<td class="first">HEAD1</td>
<td>HEAD2</td>
<td>HEAD3</td>
<td>HEAD4</td>
</tr>
</tbody>
</table>
</div>

Fitting width and height of html page contents to screen

I have created a table with rows in html and defined it's width and height parameters in css using pixel(px).
I'm using a 15.6" display pc to run it and everything goes well, but when i open the page in smaller screen pc, the table exceeds the screen margin.
Which unit should I use to define the width & height parameters so that, it will always fit the display of any media screen that opens the page?
I always use Width: 1-100vw and height: 1-100vh; on everything (<p>,<div>,font-size etc.)
You will also need to add in min-height: 100vh; and min-width: 100vw; to the html,body css. else it won't work.
This WILL ensure that it works properly. I used to use %, but in the end it didn't work out with scaling to the re-size ^^
See this for more info: https://css-tricks.com/viewport-sized-typography/
You could set the width of the body to 100%. Another suggestion is to use
1vw = 1% of viewport width
1vh = 1% of viewport height
1vmin = 1vw or 1vh, whichever is smaller
1vmax = 1vw or 1vh, whichever is larger
can read about it here
Further read into responsive web design might help you in the future.
Ex:
#import "https://fonts.googleapis.com/css?family=Montserrat:300,400,700";
.rwd-table {
margin: 1em 0;
min-width: 300px;
}
.rwd-table tr {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
}
.rwd-table th {
display: none;
}
.rwd-table td {
display: block;
}
.rwd-table td:first-child {
padding-top: .5em;
}
.rwd-table td:last-child {
padding-bottom: .5em;
}
.rwd-table td:before {
content: attr(data-th) ": ";
font-weight: bold;
width: 6.5em;
display: inline-block;
}
#media (min-width: 480px) {
.rwd-table td:before {
display: none;
}
}
.rwd-table th, .rwd-table td {
text-align: left;
}
#media (min-width: 480px) {
.rwd-table th, .rwd-table td {
display: table-cell;
padding: .25em .5em;
}
.rwd-table th:first-child, .rwd-table td:first-child {
padding-left: 0;
}
.rwd-table th:last-child, .rwd-table td:last-child {
padding-right: 0;
}
}
body {
padding: 0 2em;
font-family: Montserrat, sans-serif;
-webkit-font-smoothing: antialiased;
text-rendering: optimizeLegibility;
color: #444;
background: #eee;
}
h1 {
font-weight: normal;
letter-spacing: -1px;
color: #34495E;
}
.rwd-table {
background: #34495E;
color: #fff;
border-radius: .4em;
overflow: hidden;
}
.rwd-table tr {
border-color: #46637f;
}
.rwd-table th, .rwd-table td {
margin: .5em 1em;
}
#media (min-width: 480px) {
.rwd-table th, .rwd-table td {
padding: 1em !important;
}
}
.rwd-table th, .rwd-table td:before {
color: #dd5;
}
<h1>RWD List to Table</h1>
<table class="rwd-table">
<tr>
<th>Movie Title</th>
<th>Genre</th>
<th>Year</th>
<th>Gross</th>
</tr>
<tr>
<td data-th="Movie Title">Star Wars</td>
<td data-th="Genre">Adventure, Sci-fi</td>
<td data-th="Year">1977</td>
<td data-th="Gross">$460,935,665</td>
</tr>
<tr>
<td data-th="Movie Title">Howard The Duck</td>
<td data-th="Genre">"Comedy"</td>
<td data-th="Year">1986</td>
<td data-th="Gross">$16,295,774</td>
</tr>
<tr>
<td data-th="Movie Title">American Graffiti</td>
<td data-th="Genre">Comedy, Drama</td>
<td data-th="Year">1973</td>
<td data-th="Gross">$115,000,000</td>
</tr>
</table>
<p>← Drag window (in editor or full page view) to see the effect. →</p>
Ref: LINK

CSS Bootstrap fixed header

Is it possible in bootstrap to create a table as:
Fixed headers and scroll-able content like here
The columns need to size proportionaly to the content of the columns (wider/smaller column depanding on content)
scroll-able x-axis if more columns that can be displayed
columns have min and max width
So far I found only a solution that would require to know the number of columns to size correctly with a flex approach like here .
table {
box-sizing: border-box;
-moz-box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: stretch;
height: 500px; /* this can vary */
}
table * {
box-sizing: inherit;
-moz-box-sizing: inherit;
}
thead {
display: flex;
flex-direction: column;
align-items: stretch;
}
tbody {
overflow-y: scroll;
display: inline-block;
}
thead > tr, tbody > tr, tfoot > tr {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
thead, tfoot {
flex-shrink: 0;
}
th, tbody td {
width: 20%; /* this can vary */
overflow-x: hidden;
text-overflow: ellipsis;
display: inline-block;
}
tfoot {
display: inline-block;
}
tfoot td {
width: 100%;
display: inline-block;
}
Included is an example of what you might be looking for. I've included how to implement filtering and sorting as well. Keep in mind I just scrapped this together so I haven't had time to test anything, but this is roughly the idea of what you might be looking for?
HTML File:
<div class="wrapTable">
<table class="head table-bordered">
<thead>
<tr>
<td ng-click="sortType = 'id'; sortReverse = !sortReverse">
Id
<span ng-show="sortType == 'id' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
<span ng-show="sortType == 'id' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
</td>
<td ng-click="sortType = 'name'; sortReverse = !sortReverse">
Name
<span ng-show="sortType == 'name' && !sortReverse" class="glyphicon glyphicon-chevron-down"></span>
<span ng-show="sortType == 'name' && sortReverse" class="glyphicon glyphicon-chevron-up"></span>
</td>
</tr>
</thead>
<tr>
<td><input ng-model="dataText.id"></td>
<td><input ng-model="dataText.name"></td>
</tr>
</table>
<div class="scrollableBody">
<table class="table table-bordered table-striped">
<tbody>
<tr ng-repeat="data in dataList | orderBy:sortType:sortReverse| filter:dataText">
<td>{{data.id}}</td>
<td>{{data.name}}</td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
$scope.sortReverse = false;
$scope.sortType = 'id'; // set the default sort type
$scope.dataText = '';
$scope.dataList=....some dynamic JSON data request
</script>
CSS File:
.wrapTable {
height: 100%;
overflow-x: auto;
}
.wrapTable table { /*set wrap for table*/
max-width: 10000px;
table-layout: fixed;
width: 100%;
display: inline-table;
}
table tr td {
margin: 0;
padding: 5px;
width: 200px; /*width of each column*/
word-wrap: break-word;
}
table.head tr td {
background: #000000;
color: white;
height: 40px;
}
.scrollableBody {
height: 550px;
min-width: inherit;
overflow-y: scroll !important;
position: absolute; /* <-- here is what is important*/
}
.scrollableBody::-webkit-scrollbar {
width: 0em;
height: 2em;
}
I forgot to mention that inside the wrapTable you can set a fix width and declare scrollable-x if needed.

Set table to 70% but td to 50%?

I got this code:
<table>
<tr>
<td>PeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeter</td>
<td>GriffinGriffinGriffinGriffinGriffinGriffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
table {
margin: 0 auto;
border-spacing: 15px;
width: 70%;
min-height: 250px;
}
td {
padding: 10px;
background-color: gray;
}
I want to set table to 70% of the total window width which I did. I also want the td to be 50% of the 70% inside the table, the td width should be static.
You can see the problem here
The only reason your code wasn't working is due to the giant single string.
Add the below to cause it to break, and keep the correct width:
td {
background-color: #808080;
padding: 10px;
width: 50%;
max-width: 50%; /* to limit the width to 50% as well */
word-break: break-all; /* cause any string that is too long to be broken so will fit */
}
Here's a fiddle for your reference: http://jsfiddle.net/9ftqy/
Set table-layout:fixed; to table and fix your problem
fiddle http://jsfiddle.net/S37BB/
new css
table {
margin: 0 auto;
border-spacing: 15px;
width: 70%;
min-height: 250px;
table-layout:fixed;
}
td {
padding: 10px;
background-color: gray;
width:50%;
overflow:hidden;
}
html
<table>
<tr>
<td>PeterPeterPeterPeterPete rPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeter</td>
<td>GriffinGriffinGriffinGriffinGriffinGriffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
There are couple of things you should understand from your code...
TD width should always be fixed to 50% --- however normal tendency is re-size the cell per content which resides in it.
table {
table-layout: fixed;
}
td {
width: 50%;
}
You want to force cell content to wrap within provided space and not to overflow once #1 is completed.
td {
word-wrap: break-word;
}
Here is complete code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Fixed Table</title>
<STYLE TYPE="text/css">
table {
margin: 0 auto;
border-spacing: 15px;
width: 70%;
min-height: 250px;
table-layout: fixed;
}
td {
padding: 10px;
background-color: gray;
width: 50%;
word-wrap: break-word;
}
</STYLE>
</head>
<body>
<table>
<tr>
<td>PeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeterPeter</td>
<td>GriffinGriffinGriffinGriffinGriffinGriffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
</body>
</html>
Hope this helps you.
Ashok

Resources