Different 'div' heights in Chrome and Firefox - html

I have a table whose cells contain div elements with different content, so they have different heights. Take this fiddle as an example:
https://jsfiddle.net/6btarubL/2/
As you can see, the code is really simple:
HTML
<table>
<tr>
<td>
<div style="background-color: orange">
DIV
</div>
</td>
<td>
<div style="background-color: aqua">
Line 1<br>
Line 2
</div>
</td>
<td>
<div style="background-color: #fac">
10<br>
20<br>
30<br>
</div>
</td>
</tr>
<tr>
<td>
<div style="background-color: #8f5">
DIV
</div>
</td>
<td>
<div style="background-color: #cb1">
Line 2.1<br>
Line 2.2
</div>
</td>
<td>
<div style="background-color: #eda">
10<br>
20<br>
30<br>
</div>
</td>
</tr>
</table>
CSS
tr {
height: 100%;
}
td {
vertical-align: top;
min-width: 150px;
height: 100%;
}
td div {
height: 100%;
}
I'd like that the divs inside the cells took all the space, so they looked the same. Firefox does this, so its rendering is:
Chrome, on the other side, doesn't obbey the height : 100% df the divs, so the rendering is:
Then fun fact is that, if I remember correctly, Chrome was rendering it the same as Firefox until I updated to version 63 (I think I had version 59 before).
Any suggestions? Thanks!

Please Change your this Css Code and Check again. Chrome
td {
vertical-align: top;
min-width: 150px;
height:1;
}

HTML Change like below
<table>
<tr>
<td>
<div style="background-color: orange">
DIV
<br>
<br>
<br>
</div>
</td>
<td>
<div style="background-color: aqua">
Line 1<br>
Line 2<br>
<br>
</div>
</td>
<td>
<div style="background-color: #fac">
10<br>
20<br>
30<br>
</div>
</td>
</tr>
<tr>
<td>
<div style="background-color: #8f5">
DIV
<br>
<br>
<br>
</div>
</td>
<td>
<div style="background-color: #cb1">
Line 2.1<br>
Line 2.2<br>
<br>
</div>
</td>
<td>
<div style="background-color: #eda">
10<br>
20<br>
30<br>
</div>
</td>
</tr>
</table>

The solution whas this CSS:
<!-- THIS CODE DOESN'T WORK -->
tr {
display : flex;
height: 100%;
}
td {
vertical-align: top;
min-width: 150px;
height: 1;
}
td div {
height: 100%;
}
I'm right where I began. As #3rdthemagical pointed, the display : flex on <tr> breaks the layout and the columns aren't aligned. So, I did another test and these are the results:
WORKING CSS IN CHROME:
tr {
height: 100%;
}
td {
height: 1px;
}
div {
height: 100%;
background-color: aqua;
}
Sample output:
The above code looks like this in Firefox:
WORKING CSS IN FIREFOX:
tr {
height: 100%;
}
td {
height: 100%; /* <---------------- */
}
div {
height: 100%;
background-color: aqua;
}
But, in Chrome I have the problem for which I started this question, so it looks like this:
Who's right? Chrome or Firefox? Isn't there a cross-browser solution for this?
I'll keep on investigating...

You can use display flex
jsfiddle
CSS
tr {
height: 100%;
display: flex;
}
td {
vertical-align: top;
min-width: 150px;
}
td div {
height: 100%;
}
With flex, you can center elements, align vertically, reorder, you can do a lot of stuff.
I have already tested in mozilla, you can use prefixes to have more compatibility:
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;

Finally, the solution that worked for me was to use CSS conditionals and load different styles depending on the browser. I've explained it in this question:
Load different CSS rule depending on the browser in an Angular 4 component
Cheers,

Related

How to make img fill td completley

I want to fill the img into the td (red border), I do not care if it gets stretched. The table has the black border.The tr has no border.
(https://i.stack.imgur.com/VKleK.jpg)
I have tried using
width: 100%;
height: 100%;
but to no avail.
By using width: 100%; we are starching that image and by writing display: block we are displaying that image.
For better understanding, you can check this example.
.fillbg {
background-color: red;
}
img {
width: 100%;
display: block;
}
<table>
<tr>
<td class='fillbg'>
<img src="//via.placeholder.com/350x150" class="lazyloaded" data-ll-status="loaded">
</td>
<td class='fillbg'>
Test
</td>
<td class='fillbg'>
Test3
</td>
</tr>
</table>

div aligned with paragraph in html cell

I'm trying to create a table where in every cell, there is a small coloured square next to a text.
However, I want the square and the text to be on the same line, and I cannot do it.
Sadly I'm not a css or html master, I've tried many alignment options I found on this and other sites, but none of them worked.
You can look at a minimal example below to understand what I'm talking about.
Is there any way to do it in css? Thank you
.badge{
background-color: #000000;
width: 1em;
height: 1em;
border-radius: 25%;
}
<html>
<body>
<table border="1">
<tr>
<td>
<div class="badge"></div> not horizontally aligned text
</td>
</tr>
</table>
</body>
</html>
Instead of nesting a <div> for the badge, you could create a ::before pseudo element for each <td> and make it inline-block so the pseudo element stays inline with the text content. This way you can ensure each table data element will have the small colored square before the cells text content.
.badge::before {
content: "";
background-color: #000000;
width: 1em;
height: 1em;
border-radius: 25%;
display: inline-block;
vertical-align: middle;
}
/* Optionally, give the cells different colors */
.badge.two::before {
background-color: #ae7;
}
.badge.three::before {
background-color: #f06;
}
<html>
<body>
<table border="1">
<tr>
<td class="badge one">
some text in cell 1
</td>
<td class="badge two">
some text in cell 2
</td>
<td class="badge three">
some text in cell 3
</td>
</tr>
</table>
</body>
</html>
If you instead want to keep the same HTML structure, you could make the <td> a flexbox with display: flex to ensure the content is aligned in a row format (side-by-side). Using align-items will define how items are aligned along the cross axis.
.badge{
background-color: #000000;
width: 1em;
height: 1em;
border-radius: 25%;
}
td {
display: flex;
align-items: center;
}
<html>
<body>
<table border="1">
<tr>
<td>
<div class="badge"></div> not horizontally aligned text
</td>
</tr>
</table>
</body>
</html>
Use flexbox:
.badge {
background-color: #000000;
width: 1em;
height: 1em;
border-radius: 25%;
margin-right: 1em;
}
td {
display: flex;
align-items: center;
}
<html>
<body>
<table border="1">
<tr>
<td>
<div class="badge"></div>horizontally aligned text
</td>
</tr>
</table>
</body>
</html>

Table-cell has different height on chrome if image content has height in decimals

I have a table with several columns. On chrome, the height of the table-cell (td) with an image inside varies when image height is in decimals (e.g. 76.54px) On firefox and IE this works fine and all tds have same height.
Please see the following fiddle:
https://jsfiddle.net/sstzg0rh/3/
Height of the column with image is few point less pixels then the other columns. This works fine on firefox and all tds have same height. Why chrome is showing different behavior with column height and how to fix this
<div class="container-row">
<div class="container">
<table>
<thead>
<tr>
<th>Title</th>
<th>Image</th>
<th>Text</th>
</tr>
</thead>
<tbody>
<tr>
<td>
ABCDEFG
</td>
<td>
<img src="http://via.placeholder.com/74x90" alt="This is a no image">
</td>
<td>
ABCDEFG
</td>
</tr>
<tr>
<td>
ABCDEFG
</td>
<td>
<img src="http://via.placeholder.com/74x90" alt="This is a no image">
</td>
<td>
ABCDEFG
</td>
</tr>
</tbody>
</table>
</div>
body {
line-height: 1.5;
}
img {
max-width: 72px;
}
table {
border-collapse: collapse;
border-spacing: 0;
width: 100%;
}
tr {
min-height: 80px;
width: 100%;
border-bottom: 1px solid red;
}
td {
white-space: nowrap;
vertical-align: top;
}
I thought your img elements was a inline elements that led to the problem.
The solution i thought was
img{
display:block;
}

CSS Hover 1 Div Affects Other non-Child Divs

Here is some mock code. Basically I have no control over the table elements ONLY the div's inside the td's. I need to be able to hover over any of the div's in the row and they all hover to the same state. Can this be done?
Fiddle
HTML and CSS:
.one {
background-color: #0000FF;
width: 200px;
height: 30px;
}
.two {
background-color: #ff0000;
width: 200px;
height: 30px;
}
.three {
background-color: #00FF00;
width: 200px;
height: 30px;
}
/*.one:hover, .two:hover, .three:hover {
background-color: #000;
}*/
.row1:hover {
background-color: #000;
}
<table>
<tr>
<td>
<div class="row1 one">
</div>
</td>
<td>
<div class="row1 two">
</div>
</td>
<td>
<div class="row1 three">
</div>
</td>
</tr>
</table>
In CSS there is no parent selector yet. Therefore, you can't do this directly.
However, you can try using :hover on the nearest common ancestor:
tr:hover .row1 {
background-color: #000;
}
.one {
background-color: #0000FF;
width: 200px;
height: 30px;
}
.two {
background-color: #ff0000;
width: 200px;
height: 30px;
}
.three {
background-color: #00FF00;
width: 200px;
height: 30px;
}
tr:hover .row1 {
background-color: #000;
}
<table>
<tr>
<td>
<div class="row1 one"></div>
</td>
<td>
<div class="row1 two"></div>
</td>
<td>
<div class="row1 three"></div>
</td>
</tr>
</table>
Note it's not exactly the same: if you hover the border between two cells, they will change color even if you aren't hovering any .row1.
I don't think that's possible using just CSS, given that you have no control / access whatsoever to the table or tr above. If you do have some access (or can say for sure that the divs will be wrapped in a tr, try this code:
(basically, put a rule on the grandfather tr)
tr:hover > td > div {
background-color: black;
}
https://jsfiddle.net/zbqzu21r/
Weird idea:
You have the parent tr which you cannot control. Try making a table and nesting it inside the td. I'm assuming you can easily control anything done on this table. So, put your selectors on this table, and be done with it.
.mytable:hover tr > td > .row1 {
background-color: black;
}
<tr>
<td>
<table class="mytable">
<tr>
<td>
<div class="row1 one">
</div>
</td>
</tr>
<tr>
<td>
<div class="row1 two">
</div>
</td>
</tr>
<tr>
<td>
<div class="row1 three">
</div>
</td>
</tr>
</table>
</td>
</tr>

Add scroll to inner div

I have:
<div id = "first" style="overflow-y:auto;">
<td colspan="2" style="background: #EFF4F8;"><div
id="second" style='height: 100px; word-wrap: break-word; overflow-y: auto; width: 100%; min-width: 580px; line-height: 200%;'>
<pre style="white-space: pre-wrap;">${some_text}</pre>
</div></td>
</div>
if "some_text" width is larger than my screen so I have scroll on all screen, I want the scroll only for div ="second" or for my pre tag. The problem is that I can't change div="first", I have to use it as is. Can you help me?
Thanks,
Your markup seems wrong, but this should be your solution. Let me know how it turns out and I will update the answer to your needs. And use CSS:
CSS
#first {
overflow-y:auto;
}
#second {
height: 100px;
word-wrap: break-word;
overflow-y: scroll;
width: 100%; /* This might still mess up the overflows of parent- and child-tags in different browsers */
min-width: 580px; /* This does not work well in all browsers */
line-height: 200%;
}
#second pre {
white-space: pre-wrap;
}
HTML
<div id="first">
<table>
<tbody>
<tr>
<td colspan="2" style="background: #EFF4F8;">
<div id="second">
<pre>${some_text}</pre>
</div>
</td>
</tr>
</tbody>
</table>
</div>
You want to scroll across the width, that is a horizontal scrollbar.
You should be adding overflow-x