Responsive button table with percentages - html

I'm making a web-based calculator, which I styled with CSS. Now I want to make this responsive so it is usable on smartphones. The following is a part of my HTML
<table class="buttonTable">
<tbody>
<tr>
<td>
<input class="button_standard" type="button" value="7"></input>
</td>
<td>
<input class="button_standard" type="button" value="8"></input>
</td>
<td>
<input class="button_standard" type="button" value="9"></input>
</td>
<td>
<input class="button_operator" type="button" value="รท"></input>
</td>
</tr>
<tr>
<td>
<input class="button_standard" type="button" value="4"></input>
</td>
<td>
<input class="button_standard" type="button" value="5"></input>
</td>
...
</tr>
...
</table>
What I'm trying to achieve for screen sizes of about 450px and below is that the buttons become reponsive, and start using percentages (it would be weird to use percentages for desktop screen sizes, as it would then create buttons with widths of about 500px).
This is a picture of what it looks like on desktops or other large screens (alot of the surrounding white has been cut away):
I have my CSS set up as follows (showing only relevant things):
table {
text-align: center;
margin: 10px auto;
}
td {
width: 70px;
height: 50px;
padding: 2px;
}
input[type="button"] {
width: 100%;
height: 100%;
...
}
#media (max-width: 450px) {
td {
width: 25%;
}
...
}
As I want the buttons to be 25% of the whole screen each, I set the width to 25%. You would expect the buttons to be 25% each, so covering the whole screen but with a margin of 2px. This is what happens instead:
What am I missing that causes this? I already tried styling the buttons themselves (the input[type="button"] elements) with the width of 25% but that was even worse.
What's interesting is that when the screen width hits the table width like shown above, the elements actually do change dynamically until they hit their minimum width defined by the text inside and such.
I really don't understand what's going on here.

The problem is with the table element. It doesn't know how wide to be, so it will make itself as small as possible, and then the tds will be 25% of the table's width.
Change the width of the table to be 100%, and you will be good.

I would change the table to be full width of the parent, as well as the input, and then put a max-width of say 25em-30em on the parent.
That would look something like this:
table, input {
width:100%;
}
main {
max-width: 27.5em;
margin: 0 auto;
padding: 0 1em; // for mobile
}

Related

Setting a table's td height not working in Firefox

I've got a table with the contents of the cells completely filling them. I gave the content a fixed width and a height of 100% so that elements that get bigger are still able to grow the cells. The cells also have a minimum height via the simple height attribute so the 100% height of the content has an effect. In Chrome and Edge everything works fine, but in Firefox the cells don't grow:
Chrome:
Firefox:
If you want to try yourself:
table {
border-spacing: 8px;
font-size: 14px;
}
td {
height: 50px;
}
td div {
height: 100%;
width: 200px;
background-color: green;
}
<table>
<tr>
<td>
<div>
This div is normal sized.
</div>
</td>
<td>
<div>
This div is normal sized.
</div>
</td>
</tr>
<tr>
<td>
<div>
This div is also normal sized but should size accordingly.
</div>
</td>
<td>
<div>
This div is very very big so it gets higher and should affect other divs in the same row. But not in Firefox apparently.
</div>
</td>
</tr>
</table>
Not sure if this a bug in Firefox or a feature in Chrome, but the way I understood table sizing is that table elements cannot have a fixed size. Instead their width and height attributes are used as a min-width / min-height and they grow according to their content. Is there a quick workaround or should I rebuild the table in flexbox layout?
Update
By the way, when I instead set a fixed height on the row / tr and height: 100%; on the td, it works in Firefox. But then it's broken in Chrome...
I managed to find a workaround. I added the lines
#-moz-document url-prefix() {
tr {
height: 50px; // Your min height
}
td {
height: auto;
}
}
to my css and now it seems to display correctly in firefox and chrome. Not very clean but it works.
Apparently Chrome was to adopt Firefox behaviour with tables in Version 50 but reverted because it broke too many layouts. The trick applying height: 100%; to the tds worked because all percent sizes are automatically translated to auto. Makes much more sense then.

How to let the height of an element follows the height of next element automatically without table element?

for example, I want an image next to textfield, and I want the height of textfield follows the height of image, I tried:
<table>
<tr style="height:auto;">
<td style="height:100%;">
<input style="height:100%;"/>
</td>
<td>
<img src="https://www.gravatar.com/avatar/65756ce7bab4d76ac10456972dd9f21d?s=96&d=identicon&r=PG&f=1"/>
</td>
</tr>
</table>
which I use avatar as sample image here, and I want the height of textfield
would change automatically when I change another image which has different height.
I think the html code above is not simple, and I don't want to use table element to do that, is there any simpler way to do this?
Here is an example, assuming I understand your question:
HTML:
<div>
<input>
<img
src="https://www.gravatar.com/avatar/65756ce7bab4d76ac10456972dd9f21d?
s=96&d=identicon&r=PG&f=1"/>
</div>
CSS:
input
{
height: 90px;
float: left;
}
div
{
float:left;
}
img
{
float: left;
margin-left: 10px;
}
and here is a fiddle: http://jsfiddle.net/8t146hsg/7/

Horizontal Scrollbar on tree of tables

I have a div, and inside of that div I have a table. Inside of the table I have many rows that contain a click-able span, a checkbox, a label, and a div. The click-able span will get data from the server and append a new table following the same structure to the current TD's div. This is repeated until there is no data left to retrieve.
My issue is that at small resolutions, the labels text will go wonky, slipping under the checkboxes and spans like so:
Ideally, if the labels extend past the table I would like to add a horizontal scroll-bar, though I'm not entirely sure how. My CSS:
#media (max-width: 500px) {
#div-myTableWrapper{
/*EMPTY*/
}
#myTable{
border-collapse: separate;
background-color:#d8d8d8;
border: 2px solid black;
border-radius: 15px;
padding-left: 10px;
width:100%;
overflow: auto;
min-width:250px;
}
.subTable{
margin-left:15px;
}
}
Any help is appreciated. I'm seeing this issue on a resolution of 300px wide.
EDIT 1:
HTML Snippet:
<table id="myTable">
<tr>
<td>
<span class="openBoxNode"></span>
<input type="checkbox" class="CheckBox">
<label class="Label">Deep1</label>
<div id="Deep1">
<table class="subTable">
<tr>
<td>
<span class="openBoxNode"></span>
<input type="checkbox" class="CheckBox">
<label class="Label">Deep2</label>
<div id="Deep2">
etc...
You should be able to keep the checkbox and label from wrapping to the next line by adding the style white-space:nowrap; to the td elements. This should force it to be wider then the containing div and then the scrollbar should appear.
Add max-height: 500px to #myTable.
You will get a vertical scroll bar when the height increases more than 500px and a horizontal scroll bar if the width increases more than 250px.

table - td width in percentage with overflow not working

I am trying to build a table that contains a td which has a width set in percentage and when overflown a horizontal scrollbar.
Unfortunately I don't manage to make this happen.
http://jsfiddle.net/ne45s2wf/1/
HTML
<div class="container">
<table>
<tbody>
<tr>
<td>cell 1
</td>
<td>cell 2
</td>
<td class="too-long">cell 3 loooooooooooooooooooooooooooooooooooooooooooong
</td>
</tr>
</tbody>
</table>
</div>
CSS
.container {
position: relative;
max-width: 500px;
background-color: red;
}
table {
width: 100%;
}
td.too-long {
background-color: darkgreen;
display: inline-block;
width: 20%;
overflow-x: scroll;
}
First thing I wonder is what is the td-width in percentage relative to? And is it possible to set it to be relative to the table?
I would set a maximum width in percentage for the td with overflow hidden. While this works for the td, the parent containers do not align their width to the td child when its width is set with percentage. The parents width is as if the child did not have any width set. Furthermore the table now is not "responsive" any more.
I would take a look at bootstrap. I am not sure exactly what you mean but it seems like you are having trouble with your tables overflowing. Bootstrap has responsive tables which will scroll in the way you specify at small sizes. Take a look at this:
http://getbootstrap.com/css/#tables-responsive

responsive images max-width property in newsletter email

I designed a responsive email with several elements, each one with a main image.
To prevent images breaks in iphone resolution and higher, I have:
img { max-width: 320px !important; height: auto !important; }
But then, images that are smaller than 320px scale to this width. I fixed that with:
img { max-width: 100% !important; width: auto !important; height: auto !important; }
This works for the main images, but then the business logo resizes to 100% of its parent container.
How I get the best of each solution?
HTML coding for emails is a PAIN
See this chart: https://www.campaignmonitor.com/css/ (you might have to scroll down the page, but it's there)
There is really little to no consistency between email clients as to what they will recognize. I would recommend just coding a simple page that 'squishes' nicely.
First, your style will applied to all images, including your logo. Since you don't have specific class for images. Second, for email template you should use inline styles and using table for layouts. Example:
<table style="width: 100%; max-width: 600px; margin: 0 auto;">
<tr>
<td>
<img src="main.jpg" style="width: auto; max-width: 100%">
</td>
</tr>
<tr>
<td align="center">
<img src="logo.png">
</td>
</tr>
</table>