div overflow issue when nested in table cell (firefox) - html

THIS IS THE FIDDLE
HTML:
<input id="APP" type="button" value="Append"/>
<div id="wrapper">
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 100%; height: 30px; background-color: rgb(230,230,230)">
</td>
</tr>
<tr>
<td style="height: 100%;" align="center">
<div id="ContentWrapper">
</div>
</td>
</tr>
</table>
</div>
CSS:
#wrapper{
width: 80%;
height: 300px;
background-color: rgb(25,25,25);
}
#wrapper table{
width: 100%;
height: 100%;
}
#wrapper table td{
vertical-align: middle;
}
#ContentWrapper{
width: 98%;
height: 95%;
border: 1px solid blue;
color: rgb(255,255,255);
text-align: left;
overflow-y: auto;
}
jQuery:
$("#APP").on("click",function(){
$("#ContentWrapper").append("Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>Test<br/>");
});
If you test this in Chrome or any other browser you'll see that it works as it should. But in Firefox, if you keep pressing the "Append" button, the div's height will change with the content despite of the fact that the div's overflow is set to auto.
I know this would work if I would set the div's dimensions in px instead of percentage, but I don't want to do that. I made that fiddle as an example to pinpoint the issue but in my original code the wrapper that holds the table is responsive and I have to keep the dimensions in percentage.

Interesting find. It smacks of a bug in Firefox.
However, there is a workaround: assign an explicit height to the tbody in the table as well.
#wrapper table, #wrapper tbody {
width: 100%; height:100%;
}
See updated fiddle. Note that I also changed some other properties, because the browsers got confused by the top td being 30px high and the bottom one 100%. I changed that to 10% and 90%; you may have to use other values in your situation. (Maybe use calc(...) for one of them.)

Related

Vertically center element after title with dynamically loaded text all within a static container

This is driving me crazy and I don't think it is even possible at this point, but I would like to consult the Internet Gods before I give up.
Visual representation of what I want:
I want the text to align at the top-left of the container, and the image to align itself in the center of the space that is leftover after (if) the text wraps.
Please see demo
I'm using flexbox and margin: 0 auto to get the centering I want, and position: absolute on the h2 element works fine if the image is long, but looks terrible with tall image.
Is there some flexbox magic I can use to take into account the height of the dynamically loaded h2 element and make the image centered within the rest of the free space?
Constraints:
The parent container must be 200px by 200px.
Image can not be distorted (stretched).
I was able to find my own solution by combining #Claudio's table idea and some position absolute magic.
Here is the solution demo with both tall and wide images:
DEMO
HTML:
<div class="main-box">
<table>
<tr>
<td class="box-title">
<h2>
My title here. :) add some more text
</h2>
</td>
</tr>
<tr>
<td class="box-img">
<img src="//i.imgur.com/NOzWHFF.png" alt="Google">
</td>
</tr>
</table>
</div>
<div class="main-box">
<table>
<tr>
<td class="box-title">
<h2>
My title here. :) add some more text
</h2>
</td>
</tr>
<tr>
<td class="box-img">
<img src="https://upload.wikimedia.org/wikipedia/commons/e/e0/Long_March_2D_launching_VRSS-1.jpg" alt="Google">
</td>
</tr>
</table>
</div>
CSS:
*{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.main-box{
width: 200px;
height: 200px;
border: 2px solid #000;
background-color: #FFF;
position: relative;
}
.main-box table{
width: 100%;
height: 100%;
border-spacing:0;
}
.main-box .box-title{
background-color: rgba(0, 0, 0, 0.1);
height: 1px;
}
.main-box .box-title h2{
margin: 0;
padding: 5px;
font-family: Helvetica, Arial, sans-serif;
}
.main-box .box-img{
text-align: center;
vertical-align: middle;
padding: 5px;
position: relative;
}
.main-box .box-img img{
max-width: 100%;
max-height: 100%;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
It is not impossible, nothing is inpossible. :)
So, I was trying hard do something that work, and after spend much time thinking and testing... The solution!! Unfortunately, is using table (I'm sorry, but its the only way to do it only with HTML and CSS).
The base of the structure is simple. We have a main div with the dimentions that we want (200px X 200px). Inside it, we have a table with two lines (One to our title, and the other to our beautiful img)
After we style the structure, removing border-spacing... removing margin of h2 and things like that. We need (here is the trick) apply height: 1px; to our first line of the table (The line of the title).
Why height: 1px;?
Well, the table element applies its size following the content. We applying height: 1px;, the line will not get literally with 1px of height, it means that the line will take the less height as possible respecting the content. Doing so, the other line (the line of the image) will get all the leftover space to it, and after it's only apply vertical-align: middle; and text-align: center; in order to centering the image.
After all the explanation... the code:
https://jsfiddle.net/u61y62r8/
I hope it can help you. :)
That should do the trick.
<div class="container">
<div class="inner">
<h2>This is a title</h2>
</div>
<div class="inner-2">
<img src="https://thesingleplayermode.files.wordpress.com/2013/05/pokemon-banner.jpg">
</div>
</div>
CSS:
.inner {
width: 100%;
height: 50px;
}
.inner-2 {
height: calc(100% - 50px);
display: flex;
justify-content: center;
align-items: center;
}
.img {
max-width: 100%;
max-height: 100%;
}

width or height of child element doesn't change in CSS

Here is my DOM:
<html>
<body>
<table>
<tr>
<td>
hello
</td>
</tr>
</table>
</body>
</html>
and my CSS:
html, body {
height: 100%;
width: 100%;
}
table {
height: 100%;
width: 100%;
}
td {
border: 1px solid gray;
height: 10%;
width: 10;
}
What I want to do is to re-size the height and width of the TD element using percentage. But this code doesn't work. I understand that the size of a child element will inherit the size of it's parent element. So TD will inherit the size from TABLE and then TABLE from BODY or HTML parent elements. My code doesn't do that. But if I do width: 10%; on TABLE, then it gets 10% of the width of the BODY/HTML element. Same as with the height: 10%. But why doesn't it work on TD tag?
td tags are forced to take up all of the remaining space in their parent.
So, your width: 10%; is completely ignored by the layout.
See this non-working JSFiddle Demo.
But, if we add some display: inline-block; to the td, then it fixes the problem.
See this (now) working JSFiddle Demo.
I suggest you add another td tag, and give it a width of 90%
<table>
<tr class="tr1">
<td class=td1>
hello
</td>
<td class="td2"></td>
</tr>
<tr class="tr2">
<td></td>
</tr>
</table>
CSS:
html, body {
height: 100%;
width: 100%;
margin:0;
}
table {
height: 100%;
width: 100%;
}
td.td1 {
border: 1px solid gray;
height: 10%;
width: 10%;
}
td.td2{
border: 1px solid gray;
width: 90%;
}
tr.tr1{
height:10%;
}
tr.tr2{
height:90%;
}
For the height, you will need to add another tr row, and give it a 90%. Give the first row a 10% height like you wanted to do with the td - http://jsfiddle.net/R5uRW/6/

Fluid and Fixed Columns Table

I have a table on my layout that has 5 columns, 3 of them should be fixed width in px and the other 2 should be fluid.
It sounded simple at first, but the problem is the two fluid columns should behave differently.
The last column should stretch as much as it can to fit its contents, so they are never hidden, but shouldn't ever leave empty space. And the middle column should occupy all the free space it can find, but also overflow to hidden in case the last one needs to grow larger.
I tried to make this work with css, but I couldn't manage to make it work... Is there a way to do this with pure css or I need js?
EDIT
That's what I got so far:
HTML
<table>
<tr>
<td class="fixed">fixed</td>
<td class="fixed">fixed</td>
<td class="fluid hidden">fluid</td>
<td class="fixed">fixed</td>
<td class="fluid visible">this content should always be visible</td>
</tr>
</table>
CSS
table{
width: 100%;
table-layout: fixed;
}
td{
padding: 10px;
white-space: nowrap;
}
.fixed{
background-color: #ddd;
width: 60px;
}
.fluid{
background-color: #aaa;
}
.visible{
}
.hidden{
overflow:hidden;
}
http://jsfiddle.net/KzVbX/
It works almost as expected. Except for the last column.
Maybe I can help, maybe not.
First, I would use divs instead of tr/td. I honestly don't have a need for using tables since CSS was introduced, and I'm rather surprised that some people still do. But there could be a reason, so please do not take that as criticism.
If you use divs, then edit this section of your code:
.visible {
overflow:visible;
min-width: 210px;
}
That will make sure that the div is at least 210 pixels wide no matter what. It should work.
BTW, if this is the only table on the page and that div or td is unique in the sense that it has a minimum height, then you may want to use an id instead of a class. That will make your code cleaner and more elegant.
Hope this helps.
If you don't need wrapping do this:
td{
padding: 10px;
}
If wrap is desired, you need to change width of table to auto and add min-width parameter.
table{
width: auto;
min-width: 100%;
table-layout: fixed;
}
Try this and see if it is close to what you are looking for:
DEMO - http://jsfiddle.net/WGpB3/
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td style="width:60px;"> </td>
<td style="width:60px;"> </td>
<td style="overflow:hidden;"> </td>
<td style="width:60px;"> </td>
<td style="overflow:visible;"> </td>
</tr>
Made changes to CSS file
*DEMO - http://jsfiddle.net/KzVbX/2/
table{
width: 100%;
table-layout:fixed;
}
td{
padding: 10px;
}
.fixed{
background-color: #ddd;
width: 60px;
}
.fluid{
background-color: #aaa;
}
.visible{
overflow:visible;
}
.hidden{
overflow:hidden;
max-width:20%;
white-space:nowrap;
}

Why does increasing padding in this <a> increase the vertical margin?

This is probably the most unusual CSS behavior I have ever seen:
I have an extremely simple table that consists of two cells - one with plain text and another with a link:
<div class="content">
<table>
<tr>
<td>
Hello, world!
</td>
<td>
Hello, world!
</td>
</tr>
</table>
</div>
I have also applied the following CSS to the table:
div.content {
background-color: green;
height: 100px;
}
table td {
background-color: red;
height: 50px;
}
table td a {
background-color: orange;
box-sizing: border-box;
display: block;
height: 100%;
padding: 8px;
width: 100%;
}
When rendered in Chrome 28, I see the following:
Why is there a large amount of red above and below the link? I have specified height: 100%; for the link, so it should be taking up the full height of the <td>, which has an explicit height.
It's definitely an issue with the box-sizing:border-box attribute. My guess is that putting that inside a table cell (which is treated differently then a div) is confusing the browser. Often, new techniques + old techniques don't mix.
I would suggest doing the following:
table td a {
background-color: orange;
display: block;
height: 100%;
padding: 8px;
}
The width:100% was unneeded since the table cell already expanded to the text width + padding width. For some reason, it doesn't seem to add the padding to the height 100% with the table cell (go figure, weirdness with tables! lol). If you need it to expand to a larger table cell width, I would suggest then putting the width:100% back but then ditch the horizontal padding (i.e. put padding:8px 0px;).
As far as I think its the box-sizing attribute causing this, change your css to:
table td a {
background-color: black;
height: 100%;
display:block;
margin: 0;
padding: 0px;
width: 100%;
padding-top: 12px;
}
Hope that helps;
Add This Code to table td:
display:inline-block;
because There is some difference between tables and divisions in box modeling.
you must set display-block on any none-block element for apply box-model style.
Try setting height in px for a as
table td a {
background-color: orange;
box-sizing: border-box;
display: block;
height: 50px;
padding: 8px;
width: 100%;
}
here's an example of a jury-rig: http://jsfiddle.net/rTAwd/
We're using a line height to adjust the cell's height, so we don't need to mess with vertical alignment, and relying on a wrapper div to provide our background and padding.
<div class="content">
<table>
<tr>
<td>Hello, world!</td>
<td>
<div> Hello, world!</div>
</td>
</tr>
</table>
</div>
css
div.content {
background-color: green;
height: 100px;
}
table td {
background-color: red;
}
table td div a {
line-height: 2em;
}
table td div {
background-color: orange;
padding: 8px;
}
I think its a bug, i had the same issue a while ago, if you want the text to vertically align in the middle, instead of using display:block on the <a> tag use display:table and use border-spacing instead of padding, like this:
table td a {
background-color: orange;
display: table;
height: 100%;
border-spacing: 8px 13px;
}
I removed the width:100% too since it will do it by default, you can see an example here.
border-spacing is the CSS property for cellpadding.

Overflow: auto does not work in Firefox

I have a table. Its <td> have overflow: auto.
The width is set to be 100px. In Firefox only, text that exceeds 100px is not hidden and replaced with a scrollbar.
How can I hide content and have a scrollbar when it exceeds the width of its container?
http://jsfiddle.net/be6tM/10/
this question from here maybe solve your problem
nickb answer: "Try wrapping it in a <div>. I'm pretty sure the overflow attribute is not defined for a <td> element, at least in HTML4 it's not."
try to put your overflow:auto to the wrapper hope this can help you
pre, div {
width:100%;
overflow: auto !important;
}
working demo
The easier way to do this would be to add this to the Html
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
and this to the CSS
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
Working Example:
div {
overflow:auto;
}
td {
border: 1px solid rgb(0,0,0);
min-width: 100px;
max-width: 100px;
}
<table>
<tr>
<td class="first">
<div>Don ovonMrLongNameIsMe!!!</div>
</td>
</tr>
</table>