简体   繁体   English

文字长度+表格单元格+百分比高度 - 这里发生了什么?

[英]Text length + table-cell + percentage height - what is going on here?

I have a div using display: table-cell which contains a child div... both use percentage-based dimensions. 我有一个使用display: table-cell的div display: table-cell包含一个子div ...都使用基于百分比的维度。 When that child div contains more than a certain amount of text (depending on font-size), it ignores its height rule. 当该子div包含超过一定数量的文本(取决于字体大小)时,它将忽略其高度规则。

Here's an isolated test-case 这是一个孤立的测试用例

Clicking the "toggle text" button will demonstrate the issue. 单击“切换文本”按钮将演示此问题。 Its more pronounced in Chrome & Safari but Firefox also exhibits the issue. 它在Chrome和Safari中更为明显,但Firefox也出现了这个问题。

I would expect the .child div's height to be 40px... but instead it is roughly 290px (in Chrome & Safari). 我希望.child div的高度为40px ......但它大约是290px(在Chrome和Safari中)。 Likewise, .table should have a height of 240px. 同样, .table的高度应为240px。

HTML: HTML:

<div class="main">
    <div class="item">
        <div class="table">
            <div class="table-cell">
                <div class="child">
                    <div class="child-inner">
                        <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Etiam porta sem malesuada magna, Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

CSS: CSS:

*,
*:after,
*:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

.main {
    position: relative;
    margin: 20px;
    width: 320px;
    height: 480px;
}

.item {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background: #eee;
    overflow: hidden;
}

.table {
    display: table;
    position: absolute;
    top: 2.083333333333333%;
    left: 50%;
    height: 50%;
    width: 50%;
}

.table-cell {
    position: relative;
    display: table-cell;
    height: 100%;
    width: 100%;
    vertical-align: middle;
}

.child {
    position: relative;
    overflow: hidden;
    left: 0%;
    height: 16.666666666%;
    width: 93.75%;
    background-color: rgba(255, 255, 255, 0.5);
}

.child-inner {
    height: 100%;
    width: 100%;
    padding: 10px;
    overflow: hidden;
    overflow-y: auto;
}

p {
    margin: 0;
    padding: 0;
}

I tried wrapping the growing element in a div that is using absolute positioning, it seemed to have the effect you're asking for: http://jsfiddle.net/fschwiet/2RgPV/ . 我尝试将生长元素包装在使用绝对定位的div中,它似乎具有您要求的效果: http//jsfiddle.net/fschwiet/2RgPV/ See the "fixme" div that was added. 查看已添加的“fixme”div。 The idea is that the fixme div kind of seals whatever is happening within it from whatever table logic lives outside. 我们的想法是,无论表格逻辑在外面生成什么,它都会密封其中发生的任何事情。

.fixme {
    position:absolute;
    top:0px;
    bottom:0px;
    right:0px;
    left:0px;
    overflow:hidden;
}

It appears the padding on .child-inner is having adverse affects. 似乎.child-inner上的填充有不利影响。 I removed the padding and it now respects the height. 我删除了填充物,它现在尊重高度。 http://jsfiddle.net/cwardzala/Z27S9/1/ http://jsfiddle.net/cwardzala/Z27S9/1/

.child-inner {
    height: 100%;
    width: 100%;
    /* padding: 10px; */
    overflow: hidden;
    overflow-y: auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM