Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

css - Table cells height calculated differenly in IE11

The problem I face is IE 11 seem to have inconsistent <td> inner height across single <tr> while other browsers keep it the same.

Here's a pen illustrating my problem: http://codepen.io/anon/pen/emKEBZ

In my layout I have an absolutely positioned pseudo-element (green border) which I want to display on a (outside) <td>. I'd like it to be always as high as the whole <tr> it is in. The content of <td>s is dynamic - I have no control over it's size (like I do in the pen).

I gave it height: 100%, assuming that every <td> in a row has the same height.

td {
  position: relative;
}

td:before {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  left: -5px;
  width: 3px;
  height: 100%;
  background-color: green;
}

And yeah, that height calculates to the same value across all of the cells in the same row in Firefox and Chrome:

Chrome and Firefox

but to different height for each cell in IE 11:

Ineternet Explorer 1

The problem seems to be that in height: 100% IE refers to the inner height (the one inside the padding) of the containing <td>, while other browsers take total height (height + padding + border). And even then, the inner height of all <td>s along one <tr> is identical in Firefox, while it isn't in IE. Is any of those approaches wrong? Is there a way to force IE to work like other browsers do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally managed to do that. Here is the code, hope it helps. Fiddle here.

var spans = document.querySelectorAll( "span.bar" ),
count = spans.length;
while ( count-- ) {
   spans[count].style.height = spans[count].parentElement.offsetHeight + "px";
}
    
body {
    padding:15px;
}
table {
    border: 1px solid black;
    border-spacing: 10px;
    cell-spacing: 0;    
}

tr {
    border: 1px solid red;
}

td {
    vertical-align:center;
    position:relative;
    box-sizing: border-box;
    position: relative;
    border: 1px solid black;
}

td .bar:first-child,
td .bar:last-child {
    display: block;
    background: green;
    width: 3px; 
    left: -5px;
    height: 100%;
    position: absolute; top: 0; 
    z-index: 1;
}

p {
    margin: 0;
    background-color: #dedede;
    padding: 0px;
}

.tall {
    height: 100px;
}

.medium {
    height: 60px;
}

.short {
    height: 30px;
}
<table cellspacing="1" cellpadding "0" border="0">
    <tr>
        <td>
            <span class="bar"></span><p class="tall">Save me!</p><span class="bar"></span>
        </td>
        <td>
            <span class="bar"></span><p class="medium">From problems</p><span class="bar"></span>
        </td>
        <td>
            <span class="bar"></span><p class="short">With IE</p><span class="bar"></span>
        </td>
    </tr>
</table>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...