How to make this table structure responsive - html

I'm trying to get a resposive table with a % and not hard coded pixel values, but the column in which I want to place the image gets very small when I resize the window.
HTML:
<TABLE BORDER=1 style="width: 90%; margin: 5%;">
<TR>
<TD width="70%">
<h4>ABOUT US</h4>
<p spellcheck="false" style="padding: 15px;">content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
</p>
<p>content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent</p>
<br/><br/><br/><br/><br/>
</TD>
<TD width="30%" rowspan=2><img src="images/foto.jpeg"></TD>
</TR>
<TR>
<TD>
<h4>MEET THE TEAM</h4>
<p>content</p>
</TD>
</TABLE>
jsFiddle
Is there any way to do this so that the table resize in such manner that the column 2 becomes row 3? If I would have to do the same thing using divs I would by okay with that.

Here's a simple rule when it comes to responsive web design (or any web design in general)...
Never
Ever
Ever
... Use tables to define the layout of a page. Tables are designed to present tabular data not to define the layout of a page. Use div elements instead
A useful link to bear in mind is http://shouldiusetablesforlayout.com/.

Tables shouldn't be used for layout (as #davblayn pointed out), but if you want/need to use them:
HTML:
<TABLE BORDER=1 style="width: 90%; margin: 5%;">
<TR>
<TD id='firstItem' width="70%">
<h4>ABOUT US</h4>
<p spellcheck="false" style="padding: 15px;">content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
</p>
<p>content contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent</p>
<br/><br/><br/><br/><br/>
</TD>
<TD width="30%" id='responsiveItem' rowspan=2><img src="images/foto.jpeg"></TD>
</TR>
<TR>
<TD>
<h4>MEET THE TEAM</h4>
<p>content</p>
</TD>
</TABLE>
CSS:
#media all and (max-width: 699px) and (min-width: 520px) {
td{
width:100%;
}
#firstItem{
display:block;
}
#responsiveItem{
float:left;
}
}
jsFiddle
Play with resizing the JSFiddle window, the column with the image drops down to become a row when there isn't enough room.

Don't use tables for layout. You can't easily do what you want with CSS anyway, since you'd have to override the display property for multiple elements.

If by "responsive" you mean does not go screwy when text does not fit, and maintains image size (images do not scale nicely), then you can use this:
<table border="1" width="800px" height="250" style="min-width:800px; width: 90%; margin: 5%;">
<tr>
<td>
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
</td>
<td style="height: 250px; width: 250px;" rowspan="2">CONTENT</td>
</tr>
<tr>
<td>
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
CONTENT CONTENT CONTENT CONTENT
</td>
</tr>
</table>
Basically, you need to set minimum space to accomodate the content, then you can scale outwards as much as you want.
While yes, tables are generally frowned upon, it is another form of "WHY WOULD YOU WANT TO DO THAT" and since the question particularly asks for tables, there is no need to completely change the question to satisfy your pet peeve.
Tables work fine for websites, the main issue with them is that they take a while to set up, and once they are set up, they stay that way forever, and changing them is impossible without total re-tabling.
This does not appear to the main issue here.

Related

Can you mimic the float right of a div with HTML Tables?

I'm trying to create an Email Template for my client. They are sending an HTML Newsletter. I originally had the layout perfect only to find out that Outlook and other email programs (Gmail, etc) do not support positioning like I need. The overall layout of the newsletter is as follows: (Forgive the ASCII Art)
---------------------------------------------------
| Header Image | Email Title |
| | |
----------------------------------------------------
| Date | Contents |
----------------------------------------------------
| Main Content | TOC |
| | Related links|
| | |
| |--------------|
| |
| |
----------------------------------------------------
| Footer Info |
| |
----------------------------------------------------
Because I need to use HTML Tables in order to get this positioning, I cannot wrap content into the section under the Related Links.
Is there a way to mimic the concept of a DIV with float:right (The way I originally implemented it) using HTML tables? My output now is the content stays in the left "Main Content" column and I get a long white strip down the right side under the "Related Links" section.
I've tried various CSS styles, but nothing seems to render properly in Outlook or GMail.
I have toyed around with the idea of leaving it up to the user to enter text until they reach the end of the right Content box and then start entering text in another entry, and then I stitch them together with a ColSpan of 2. However that seems overly complex for my users, and somewhat of a kluge.
It's fairly straightforward markup
<table border="0" cellspacing="1" cellpadding="0" style="width:750px;">
<tr style="height:205px">
<td style="width:500px;">
<img/>
</td>
<td style="width:250px;">
<span>Some Title</span>
</td>
</tr>
<tr style="height:22px">
<td style="width:500px;">NewsLetter Title</td>
<td style="width:250px;">Contents</td>
</tr>
<tr>
<td style="width:500px;">
Main content of newsletter
</td>
<td style="width:250px;">
Table of Contents Related Links
</td>
</tr>
<tr>
<td colspan="2">
Footer Info
</td>
</tr>
</table>
I would like the Main Content area to expand as needed, and once the content grows beyond the right "Contents" section, the main content will flow over into that column.
First, it should be pointed out that this isn't the natural behaviour in email clients.
You're going to see issues somewhere because you're effectively hacking together a solution. More detail below...
Points to consider:
As I commented on some of the previous answers - Divs can mimic what you want, but in Outlook, divs will blow out to 100%. Fixes such as calc widths etc aren't the solution to fix this. Tables will absolutely do the exact same job, without the drawback of having to add hack fixes such as Ghost tables just for Outlook.
Try not to use floats in email. They may work in some places but won't work everywhere. The align attribute (e.g. align="right") is what you're looking for. It's best to define these on table cells and the content inside will inherit this property, but when working with more than one table inside of the cell, it's best to define directly on the element. You can see this in my code below... My table is next to a group of text. Defining the align on the cell would force the text to right align, not great!
Because this isn't the natural behaviour, you're going to see an issue somewhere.
In the case of the code below, this removes the reliance on floats, divs and calc widths and uses tables and fixed widths, although these can be changed to percentages.
However, the group of text is messing with the colspan and widths of the table cells in Outlook. Specifically, it is blowing out the first cell, throughout the table and so it isn't adhering to the fixed with of 316px you've defined. *FYI - I've used a Litmus account to test this code in the big email clients including Gmail webmail, Gmail App (iOS), Outlook 2010/2013/2019, Outlook webmail, Outlook 365, Yahoo Webmail, Outlook App (iOS) and Apple Mail App (iOS).
<table border="0" cellspacing="0" cellpadding="0" style="border:1px solid #000; border-collapse:collapse; width:100%;">
<tr>
<td style="width:316px; font-size:0; padding:0; border:1px solid #000;"><img width="316" height="159" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT-c_x9ADUhNWyovPD0yjkNwzEvaHK7INZYTRwfRjLrHwGmNDns1g" style="display:block;" /></td>
<td style="border:1px solid #000; padding:3px;">Email Title</td>
</tr>
<tr>
<td style="padding:3px; border:1px solid #000;">Date</td>
<td style="padding:3px; border:1px solid #000;">Content</td>
</tr>
<tr>
<td colspan="2" style="padding:3px; border:1px solid #000;">
<table align="right" border="0" cellspacing="0" cellpadding="0" style="width:272px;">
<tr>
<td style="padding:3px; background:#000; color:#fff;">
<table border="0" cellspacing="0" cellpadding="0" style="width:100%;">
<tr>
<td>
TOC
</td>
</tr>
<tr>
<td>
Related Links
</td>
</tr>
</table>
</td>
</tr>
</table>
Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content Main Content
</td>
</tr>
<tr>
<th colspan="2" style="padding:3px; border:1px solid #000;">Footer Info</th>
</tr>
</table>
Without CSS you would want to do something like:
<table style='border:1px solid #000; border-collapse:collapse;'>
<thead>
<tr>
<th style='width:316px; font-size:0; padding:0; border:1px solid #000;'><img width='316' height='159' src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT-c_x9ADUhNWyovPD0yjkNwzEvaHK7INZYTRwfRjLrHwGmNDns1g' /></th>
<th style='border:1px solid #000; padding:3px;'>Email Title</th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan='2' style='padding:3px; border:1px solid #000;'>Footer Info</th>
</tr>
</tfoot>
<tbody>
<tr>
<td style='padding:3px; border:1px solid #000;'>Date</td>
<td style='padding:3px; border:1px solid #000;'>Content</td>
</tr>
<tr>
<td colspan='2' style='padding:3px; border:1px solid #000;'>
<table style='width:calc(100% - 316px); padding:3px; background:#000; color:#fff; float:right;'>
<tbody>
<tr>
<td>TOC</td>
</tr>
<tr>
<td>Related Links</td>
</tr>
</tbody>
</table>
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
Main Content Main Content Main Content Main Content
</td>
</tr>
</tbody>
</table>
Of course, you want to use CSS, if possible.

Outlook 2007 completely ignores width and height for elements in table cell

I’m going through the horror of trying to make HTML e-mail templates that look acceptable in Outlook, and quickly nearing the point of hara-kiri.
I have a basic table setup: three columns, with all content in the middle one. The columns on the side are just there to give spacing. The table has a width of 100% so it takes up the entire width of the reading window. So essentially this (with all the Outlook-specific crud left out):
<table>
<tbody>
<tr>
<td class="leftsidespacer"></td>
<td class="maincontent">
<p>All the content here</p>
<div class="thisisabox">
<p>Something here too</p>
</div>
</td>
<td class="rightsidespacer"></td>
</tr>
</tbody>
</table>
In any normal e-mail client, this is a piece of cake. You set a width on the middle column and that’s pretty much it. Outlook 2007 (and probably other versions) instead collapses all three columns so the middle column takes up 100% of the body width. Basically, setting a width on a table cell has no effect.
All right, so I fall back on really old-time ways of adding an image in the empty cells to force them to have some width. Ugly and stupid, but at least it sorta-kinda works.
The problem I’m facing now, which I mysteriously cannot find anyone even mentioning online, is that any element that I put inside a td always ends up being 100% of the width of the cell and the height of the content, no matter what I do.
The div with the class thisisabox in the example above, for example, always ends up being just one line of text in height and 100% of the table cell, even if I define it thus:
<div width="200" height="200"
style="display: block;
width: 200px;
height: 200px;
background: red;">
Everything in me screams that this should produce a 200 × 200 pixel red box, but it doesn’t. It just gets ignored completely.
As far as I can tell, there is nothing in my styles which ought to have any influence on this. The entirety of the styles declarations I have for the bits in the HTML snippet above is this:
table {
width: 100%;
margin: 0;
padding: 0;
}
table, tr, td {
border-collapse: collapse;
}
td {
padding: 35px 0;
border: 0;
}
(It gets inlined and HTML-attributified by the Premailer API before sending, so it’s not because the styles are only declared in the head.)
Is there some way of making Outlook notice specified width and height of elements inside a table cell?
Or am I missing something really obvious that’s making Outlook behave in this infuriating way?
Outlook does not work with div and it in some instances ignores padding.
https://www.campaignmonitor.com/css/box-model/padding/
The way to fix this is simple and it will work with every email client:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
</head>
<body>
<table width="200" height="200" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="leftsidespacer" width="30"></td>
<td class="maincontent" width="140">
<p>All the content here</p>
<div class="thisisabox">
<p>Something here too</p>
</div>
</td>
<td class="rightsidespacer" width="30"></td>
</tr>
</tbody>
</table>
</body>
</html>
I would create a style sheet and add the values which will be picked up by most modern email clients, but Outlook desktop versions like 2007-2016 require a few inline aids to function properly.
Edit: Base table in Outlook 2007
This is the base table in Outlook 2007 with no extra css that I posted above:
This image came out of Litmus.
I only used the code I posted above. If you are not seeing this, something in your CSS or HTML is causing an issue.
Good luck.
Here is something you can try.
Code:
<table cellpadding="0" cellspacing="0" width="200" height="200" bgcolor="#000000">
<tbody>
<tr>
<td height="200"></td>
<td valign="top" style="color:#ffffff;">
All content here
</td>
</tr>
</table>
Result in Outlook version 1803 (tested: 20/04/2018)
What I have done is added a height to the table element as well as one of the cells. You can either populate the left column with a spacer image or keep it as it is.
Note: You can make do without the left column if you wish but do add the height
Hope this is the answer you were looking for.

find the container width

I have a discussion with a friend here, an example can be seen. Does the container have a width on 500px or 580px? By the content we mean from the sides of the banner picture, and a straight line all the way throughout the text.
We looked in the inspect window, but we cannot find where the width should be.
The site is made in tables, because the setup is for email newsletters.
An example where the width is set is in the html:
<!-- Top Picture Start -->
<table class="row background-color__blue">
<tr>
<td class="center img-position" align="center">
<center>
<table class="container">
<tr>
<td class="wrapper last">
<table class="twelve columns">
<tr>
<td>
<a class="remove-banner-space" href="http://google.dk"><img width="580" height="300" src="http://d21vu35cjx7sd4.cloudfront.net/dims3/MMAH/crop/586x293%2B0%2B95/resize/580x290%5E/quality/90/?url=http%3A%2F%2Fs3.amazonaws.com%2Fassets.prod.vetstreet.com%2F4a%2Ff0%2Fc29d3f434ae6abd19f5433140124%2Fborder-collie-AP-XO4EXW-590sm52314.jpg" alt="Test" /></a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</td>
</tr>
</table>
<!-- Top Picture End -->
In the CSS it should also be set to 580px:
table.container {
background: #fefefe;
width: 580px;
margin: 0 auto;
Margin: 0 auto;
text-align: inherit;
}
In the browser tools/inspector (at least in Firefox) there is (among others) a tab called "calculated" (not sure about the exact English term since in my case it's in another language). This shows you the calculated (i.e. actual) width, with everything that's added up to it: margin, border, padding, inner width - a good representation of the CSS box model.

HTML table dynamic height for one cell

Is it possible to have a table that distributes the cell heights like the following?
______________________________________
| fixed height |
|____________________________________|
| |
| |
| dynamic height |
| |
|____________________________________|
| fixed height |
|____________________________________|
So the height of the first and the last cells are fixed, the center cell should be dynamic and scrollable.
Currently I have the following code but it doesn't work:
<table style="max-height:200px">
<tr>
<td height="30px">First element</td>
</tr>
<tr>
<td>
<!-- Div for scroll functionality if content is too high -->
<div style="overflow:auto">
Dynamic content which needs to be scrolled sometimes
</div>
</td>
</tr>
<tr>
<td height="30px">last element</td>
</tr>
</table>
EDIT: it doesn't need to be a table it is just important that the center element has a dynamic height and is scrollable
EDIT: I know that I can specify the height of the element to be scrolled - then it works. But I want the height of the element to be dynamic
You might want this:
1) I gave height:auto to table, and here width:80% to table is optional.
2) The Div inside td is given height:60px;(just random to bring up scroll-bar), along with overflow:auto;.
3) The first and second td's have specific height:30px;
Check this out:
<table style="height:auto; width:80%;" border=1>
<tr>
<td height="30px">First element</td>
</tr>
<tr>
<td>
<!-- Div for scroll functionality if content is too high -->
<div style="overflow:auto; height: 60px;">
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
</div>
</td>
</tr>
<tr>
<td height="30px">last element</td>
</tr>
</table>
Update : No Specific Height
Here in a update i used max-height
<table style="height:auto; width:75%; margin:0px auto;" border=1>
<tr>
<td height="30px">First element</td>
</tr>
<tr>
<td>
<!-- Div for scroll functionality if content is too high -->
<div style="overflow:auto; max-height:100px;">
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
Dynamic content which needs to be scrolled sometimes
</div>
</td>
</tr>
<tr>
<td height="30px">last element</td>
</tr>
</table>
Note: If you want to use % for max-height give height and width equals 100% to your body.

Two columns, fixed fluid with 100% height

How can I achieve the following effect without the use of a table?
Example:
http://enstar.nl/example.php (The example may not be visible at the moment, the nameservers should have been changed, but my hosting isn't that fast in updating them. Should be working later today. I apologize for the inconvenience)
All methods require a header and/or a footer. I don't want that.
What I want is the following:
Pure CSS, no tables
2 columns, fixed fluid (in that order)
if the content hasn't reach the bottom of the viewport, than extend the columns to it. Else extent to the content (so like a sticky footer)
A table at 100%x100% does this naturally. But I really don't want to use a table for this.
Any ideas?
EDIT:
Current HTML
<table width="100%" height="100%" cellpadding="0" cellspacing="0">
<tr>
<td id="navigation" valign="top" align="left">
</td>
<td id="content" valign="top" align="left">
</td>
</tr>
</table>
to set a two column there are a couple of options if you don't want to use tables
<div id="wrapper" style="height: 100%;">
<div style="background-color: green;">
<div id="leftCol" style="float: left; width: 200px;">testing</div>
<div style="background-color: red; margin-left: 200px;">
<div id="rightCol" style="height: 900px;" >testing testing testing testing testing testing testing</div>
</div>
<div class="clear"></div>
</div>
</div>
As long as the text inside the rightCol is longer than that in the left col (which can be handled by a min-height on the element) then you shouldn't have any problems with it scaling.
This also nullifies the need for the Javascript to set the second width. The reason is it is set to the width of the parent div which by default is 100% since you margin the red column left 200px it slides the display section over so you can see your left column.