How to make a responsive table in CSS - php

I have tried the piece of code mentioned below for a table, but it doesn't make it responsive. Can anyone make modifications to this code to make it responsive?
.content table {
border-collapse:collapse;
margin:0 0 1.625em;
width:100%;
font-size:90%
}
.content table h3 {
font-size:1em;
font-weight:300
}
.content tbody {
border-bottom:1px solid #444;
border-left:1px solid #444
}
.content tr:nth-child(odd) {
background-color:#eee
}
.content table th {
background-color:#0078d7;
color:#fff;
font-size:.85em
}
.content td,.content th {
vertical-align:top
}
.content th,.content td {
padding:3px 10px;
text-align:left;
border-top:1px solid #444;
border-right:1px solid #444
}
HTML
$menu .= '<table class="content"><thead><tr><th>Name</th><th>Description</th>‌​<th>Price</th></tr><‌​/thead>';
while ( have_rows('sections_items') ) : the_row();
// Your loop code
$menu .= '<tr><td>'.get_sub_field('dish_names').'</td><td>'.get_sub_f‌ield('dish_descripti‌​on').'</td><td>$ '.get_sub_field('dish_price').'</td></tr>';
endwhile;
$menu .= '</table> ';

What I did for showing table on mobile was either using bootstrap grid feature or modify table display style. I use them both depending on the content I want to show.
The key is to add #media query so that it changes based on screen size:
#media (max-width: 767px){
table{
//styles you want to change
}
}
If you have to use table elements, you can change the style of table and td elements to display: block on mobile and modify them from then.
Another way to do this which is not recommended is to create a whole new view for mobile only and hide the original table using media query. This creates duplicate data but gives you the freedom to custom mobile view at will.
example:
.mobile-table{ // or whatever class you want
display: none;
}
#media (max-width: 767px){
table{
display: none;
}
.mobile-table{
display: block;
}
}

If you want to resize your table according to the size of the window try using '#media' blocks so you can make sure your page looks exactly the same way you want
'#media' rule changes the css values according to the screen size
http://www.w3schools.com/cssref/css3_pr_mediaquery.asp
use this link to learn more

Try putting it in a container with overflow-x: auto, this should bring up a horizontal scroll bar for the table on screen resize. Here's an example
<div style="overflow-x:auto;">
<table class="content">
....
</table>
</div>
The above was answered keeping in mind the simple behavior of table "adding a scrollbar". My bad for not thinking and adding the advanced level of table responsiveness. If you are looking to have every row of your table to act as an individual table, when the screen size is too small, answer by Songqi Liu is the way to approach.
One more thing I would like to add to the styles relative for your table headers
td:nth-of-type(1):before { content: "Name"; }
td:nth-of-type(2):before { content: "Description"; }
td:nth-of-type(3):before { content: "Price"; }
This will set the headers to the left with the values on the right.
min-device-width and max-device-width are other attributes to interest if you are considering mobile. Check more here

Related

Styling RSS feed in CSS

I have the following RSS feed:
https://jsfiddle.net/yhtf36a1/
Right now, it gets displayed one below the other.
How do I use CSS to make it display two items per row, so that it looks like the following?
I tried using display:inline-block but that didn't work.
Use the following CSS:
.entry-wrapper {
float:left;
width: 48%;
background: lightblue;
margin: 1%
}
See the example here: jsfiddle.net/GillesCoeman/5e8z00z1
Hope this helps
*{font-family:arial}
.rssRow{display:flex}
.entry-wrapper{display:inline-block; margin:.25em;background:#f3f3f3; border:3px solid #f1f1f1; background:#FFF;padding:10px;border-radius:5px;}
.entry-wrapper:hover{background:#edfdff;border:3px solid #f4f4f4}
.entry-image{float:left;}
.entry-text{float:left;}
.entry-title h4{margin:0; font-size:1.5em;font-weight:bold;}
.entry-title h4 a {color:#333; text-decoration:none;}
.entry-title h4 a:hover{text-decoration:underline;}
.entry-date{color:#999; padding-bottom:.75em;font-size:.75em}
Preview at https://jsfiddle.net/itsselvam/yhtf36a1/3/
Let me know for any query on this

How to prevent PHP pushed HTML tables exceeding the length of the page

I am still a novice and would appreciate any assistance you can provide.
I am trying to prevent an HTML table from exceeding the length of the page. I am currently parsing a csv with PHP and looping the array information into an HTML table.
The table is to be shown on a static 1080p screen so the table itself needs to resize by shrinking cells instead of going off the page horizontally or vertically. I dont mind how squashed the cells become as they are colour coded with CSS.
I have too many lines of code to post here so I will just post my CSS style code which I have attempted to use to contain my table.
<style>
html,body {
background-color: #F4F4F4
margin: 0;
padding: 0;
height: 100%;
border: 0;
}
table{
height:100%;
width:100%;
overflow: hidden;
white-space: nowrap;
}
td {
border: 1px solid black;
resize: both;
overflow: hidden;
}
th {
resize: both;
overflow: auto;
}
</style>
I have tried different variations of the above to no avail e.g. overflow settings, resize settings etc this is just my current test.
I do not care whether I have to use HTML, CSS, Javascript, JQuery or PHP for this. I would rather not have to completely start over however.
Try to include this for your tags:
td.wordbreak {
word-break: break-all;
width: NNNpx;
}
This should sort the problem of cells spiting out your table horizontally.
Replace the NNN by a number in pixels, that could be a fraction of the total you need.
So let's say you had only 2 columns, it would be:
table {
table-layout: fixed; width: 100%;
}
td
{
word-break: break-all;
width: 540px;
}
try to use
table {
table-layout: fixed;
width: 100%;
}
For more check Fixed Table Layouts.
Hope this will help.

CSS display divs below each other after screen gets to 700px

I have this HTML:
<div class="dashboard_wrap">
<div>orders</div>
<div>porting</div>
<div>contact</div>
</div>
that displays 3 divs, here is the CSS:
.dashboard_wrap {
padding:10px;
}
.dashboard_wrap div {
border-left:1px solid black;
padding:10px;
width: 50%;
height:200px;
margin-bottom:50px;
overflow-y:scroll;
float: left;
}
.dashboard_clear:after {
content: "";
display: table;
clear: both;
}
#media all and (max-width: 700px) {
div.wrap div {
width: 100%;
float: none;
}
}
I am using PHP so only certain users can see certain divs. If a user can only see the first 2 divs, how can i make them 50% each rather than 40%?
There is no need to use php or javascript for this. You can use basic html and css for this.
You can check the html fiddle for this: http://jsfiddle.net/4WaX4/1/
All the css which you need is this:
.dashboard_wrap {
display:table;
min-width:500px;
background:#00ff00;
}
.dashboard_items {
display:table-row;
}
.dashboard_items div{
display:table-cell;
border:1px solid #ff0000;
}
#media all and (max-width: 700px) {
div.dashboard_items div {
width: 100%;
display:block;
}
}
And the html looks as follows:
<div class="dashboard_wrap">
<div class="dashboard_items">
<div>orders</div>
<div>porting</div>
</div>
</div>
<div class="dashboard_wrap">
<div class="dashboard_items">
<div>orders</div>
<div>porting</div>
<div>contact</div>
</div>
</div>
Very simpel and quick. When you resize the result window in jsfiddle you see that the divs become 100% relative to the outer div (500px).
I hope this is the solution youre looking for...
You can specify the class of the wrapper based on the number of items inside.
CSS classes for each variant will handle the style automatically.
If however the number of divs can extend beyond expected numbers, then dynamic inline styles may be your solution.
<div class="dashboard_wrap has3">
<div>orders</div>
<div>porting</div>
<div>contact</div>
</div>
<div class="dashboard_wrap has2">
<div>orders</div>
<div>porting</div>
</div>
<style>
.dashboard_wrap div {
border-left:1px solid black;
padding:10px;
height:200px;
margin-bottom:50px;
overflow-y:scroll;
float: left;
}
.dashboard_wrap.has2 div {
width: 50%;
}
.dashboard_wrap.has3 div {
width: 33%;
}
</style>
When the page gets rendered, only two divs will be visible. What you need to do is use a client-based language i.e. javascript or jQuery, to manipulate what is visible on screen.
Use a simple check to see what divs are visible or use php to generate a value which you can hide in the page to make it easier to resize the divs like:
<input type='hidden' id='divs_visible' value='" . $divs_visible ."' />
then using jQuery:
$(document).ready(function() {
var divsvis = $("#divs_visible").val();
if(divsvis == 2)
{
// resize the divs
}
});
EDIT
You can also render all the divs, then using jQuery and the value you've placed in the hidden input, you can simply hide the div you do not need with:
$("#div_to_be_hidden").hide();

Can :hover apply to generic element tags specific to a parent div?

I am using a bunch of divs (created with PHP) to generate a block of clickable elements. What I need to do is apply some styles to these generic elements, rather than to specific ones, yet using the code below seems to be invalid.
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div:hover {
background-color: blue;
}
<div id="Container">
<div style="background-color: red; width: 100px; height: 100px;">
</div>
http://jsfiddle.net/XD2eZ/
So I am not sure if it is an issue that a generic div element cannot be styled as a sub-element AND have a :hover attribute that operates properly. I know that classes or id's can be specified to handle this, but have thousands of unique divs. I also cannot use
#Container:hover div{ background-color: blue;}
As it ALSO seems to be invalid, but I need to select the one element from a block, and not all at once.
Any ideas here? Thanks in advance.
This will work if you remove the background color from the HTML, and apply it using css:
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div {background-color: red;}
#Container div:hover {
background-color: blue;
}
Example: http://jsfiddle.net/XD2eZ/1/
The reasone is CSS Specificity - a style attribute rule is much more specific (stronger) than an ID + element rule.

using php to add div class property

I am using WAMP. I want to take background image URL from my database and want to show this in div class 'box'. I tried it by followed way but couldn't succeed. the last background image is appearing on each box while I want to show different images. Code I am using is
<?php
$feild_set = get_all_feilds();
while($feild = mysql_fetch_array($feild_set)) {
$url = $feild['background_image_url'];
echo "<style>
.box {
width: 300px ;
height: 100px;
background-image: $url;
background-visibility: visible;
border: 1px #00FF33;
margin-bottom: 10px;
display: inline-table;
margin-right: 10px;
}
</style>";
echo "<div class=\"box\">";
echo "<a href=\"content.php?feild=" . $feild['id'] . "\" ><block_holder>{$feild['menu_name']}</block_holder></a>";
echo "</div>";
}
?>
Thanx in advance
Try this:
<?php
echo "
<style>
.box {
width: 300px ;
height: 100px;
background-visibility: visible;
border: 1px #00FF33;
margin-bottom: 10px;
display: inline-table;
margin-right: 10px;
}
</style>";
$field_set = get_all_feilds();
while ($field = mysql_fetch_assoc($field_set)) {
echo '
<div class="box" style="background-image:'.htmlspecialchars($field['background_image_url']).';">
<a href="content.php?feild='.htmlspecialchars($field['id']).'">
<block_holder>'.htmlspecialchars($field['menu_name']).'</block_holder>
</a>
</div>';
}
?>
What have I done?
Placed the declaration of the .box CSS class outside the loop so it is only output once
Changed mysql_fetch_array() to mysql_fetch_assoc() as it is less confusing and more efficient
Removed the background-image: property from the .box class
Added a background-image: property to an inline style= attribute for each div and wrapped the URL of the image in url() (this has been undone as it seems the URLs are stored in the database with this already done
Passed data from the database through htmlspecialchars() before outputting it
Corrected the spelling of feild to field where it can be done without breaking the rest of your code
Some general indentation and quote tidying and readability fixes
As it was, your CSS .box class was declared more than once. Because of the cascading nature of CSS, only the values used in the last declaration would have been used - each declaration of a property overrides the last, which is why you were only seeing the last image. You also would not need to declare those details more than once - the whole idea of a class is that you can declare it once and use it multiple times. If you want element-specific properties, use IDs or inline styles (preferably IDs, but I have used inline styles here for simplicity).

Categories