简体   繁体   English

即使父级已设置高度,元素也不会响应设置为百分比的高度

[英]Element won't respond to height set as percentage even though parent has set height

I have been puzzling over this bit of HTML/CSS for hours and I can't figure out where I'm going wrong. 数小时来,我一直在困惑着这段HTML / CSS,但我不知道哪里出了问题。 It is part of a more complicated layout, but I think I have isolated the problem. 它是更复杂的布局的一部分,但是我认为我已经隔离了这个问题。

This is my HTML: 这是我的HTML:

<table>
    <tr>
        <td>
            <div class = "outer">
                <div class = "middle">
                    <div class = "inner">
                        <svg viewbox = "0, 0, 250, 250">
                        </svg>
                    </div>
                </div>
            </div>
        </td>
    </tr>
</table>

...and this is my CSS: ...这是我的CSS:

* {
    margin: 0px;
    padding: 0px;
}
body,html {
    height: 100%;
    font-family: sans-serif;
    font-size: 12px;
    -webkit-box-sizing: border-box; 
    -moz-box-sizing: border-box;    
    box-sizing: border-box;      
    -webkit-text-size-adjust: none; 
}
table {
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
    min-width: 600px;
    /*table-layout: fixed;*/
    border-collapse: collapse;
    border-spacing: 0;
}

tr {
    background-color: #ffffff;
    height: 100%;
    height: calc(100% - 190px);
}

td {
    height: 100%;
    background-color: red;
    margin: 0;
}

.outer {
    width: 94%;
    width: calc(100% - 20px);
    height: 94%;
    height: calc(100% - 20px);
    background-color: blue;
    display: block;
    margin: 3%;
    margin: calc(10px + 0%);
    overflow-y: scroll;
}
.middle {
    display: white;
    background-color: lightblue;
    padding: 0px;
    margin: 0px;
    width: 100%;
    height: 100%;
    height: calc(100%  - 10px - 2.5rem);
    overflow-y: scroll;
}

.inner {
    background-color: pink;
    display: block;
    width: 40%;
    height: 100%;
    overflow: hidden;
}

svg {
    height: auto;
    max-height: 100%;
    width: auto;
    max-width: 100%;
    background-color: salmon;
}

If you remove the SVG, the divs and table behave as I would like - on window resize, the table doesn't exceed the size of the page, and the divs all scale accordingly. 如果删除SVG,则div和表的行为与我想要的一样-在调整窗口大小时,表不会超过页面的大小,并且div均会相应缩放。

When you include the svg, what I would expect (and what I would want) is for the svg to scale to fit the .inner div, but not to exceed it or alter the flow of the layout in any way. 当包含svg时,我期望(以及我想要的)是svg缩放以适合.inner div,但不超过它或以任何方式改变布局的流程。 This is not what happens. 这不会发生。

I've checked this in Safari and in Firefox, and both give me unexpected but different results. 我已经在Safari和Firefox中进行了检查,两者都给我带来意想不到的不同结果。 In firefox, if the window shrinks beyond a certain point, the svg height expands and the whole window ends up scrolling. 在Firefox中,如果窗口缩小到某个点以上,则svg高度会扩大,整个窗口最终会滚动。

In Safari, the svg height scales as the width changes, but the width doesn't scale as the height changes. 在Safari中,svg高度会随着宽度的变化而缩放,但是宽度不会随着高度的变化而缩放。

This seems like a pretty simple thing and I don't really know where I'm going wrong. 这看起来很简单,我真的不知道我要去哪里。 I am sure it is an obvious rookie error of some sort. 我确信这是某种明显的菜鸟错误。 I have tried every combination of max/min height/width on the svg. 我已经尝试过SVG上最大/最小高度/宽度的每种组合。

Thanks for your help! 谢谢你的帮助!

EDIT: If I get rid of the table (remove the table, td, tr) everything works as expected. 编辑:如果我摆脱表(删除表,td,tr),一切按预期工作。 Equally, if I get rid of the svg and keep the table, everything works as expected. 同样,如果我摆脱了svg并保留了表格,一切都会按预期进行。

As stated here , you can't set a width/height value of auto for an svg element. 如此处所述 ,您不能为svg元素设置auto的宽度/高度值。 Use 100% for the height and width instead like this: 使用100%作为高度和宽度,如下所示:

svg {
    height: 100%;
    max-height: 100%;
    width: 100%;
    max-width: 100%;
    background-color: salmon;
}

If the problem still persist, try adding the height and width property as an inline style declaration to the svg and see if that fixes the problem: 如果问题仍然存在,请尝试将heightwidth属性作为内联样式声明添加到svg中,看看是否可以解决问题:

<svg style="width:100% max-width:100%; height: 100%; max-height: 100%;" viewbox = "0, 0, 250, 250"></svg>

you missed to give height on tbody 你错过了给于高度tbody

tbody {
    height: 100%;
}

Actually browser automatically add tbody in your table if you not add. 实际上,如果不添加,浏览器会自动在表中添加tbody。 you can see this in inspect element 你可以在检查元素中看到这一点

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

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