简体   繁体   English

如何保持父 div 的纵横比但允许子元素填充所有垂直空间?

[英]How to maintain aspect ratio of parent div but allow children to fill all vertical space?

I am trying to build a responsive box where the aspect ratio of the box is maintained and children fill all vertical space.我正在尝试构建一个响应框,其中保持框的纵横比并且儿童填充所有垂直空间。

So far, if I give the children a fixed height, the parent no longer maintains its aspect ratio but then I can't give the children any hight at all.到目前为止,如果我给孩子一个固定的高度,父母不再保持它的纵横比,但是我根本不能给孩子任何高度。 There seem to be a few answers on stack overflow but can't get them to completely work in my case.堆栈溢出似乎有一些答案,但在我的情况下无法让它们完全工作。 Here is my code in React:这是我在 React 中的代码:

ReactDOM.render(
    <div
        style={{
            width: '100%',
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center'
        }}
    >
        <div
            style={{
                width: 350,
                height: 250,
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                backgroundColor: 'white',
            }}
        >
            <Diagram />
        </div >
    </div>,
    document.getElementById('root'),

The component:组件:

export default function Diagram(): ReactElement {
    return (
        <div className="h__diagramContainer">
            <div className="h__columnContainer">
                <div
                    className="h__diagramColumn"
                    style={{
                        border: '1px solid blue',
                        height: '100%',
                    }}
                />
                <div
                    className="h__diagramColumn"
                    style={{
                        border: '1px solid blue',
                        height: '100%',
                    }}
                />
                <div
                    className="h__diagramColumn"
                    style={{
                        border: '1px solid blue',
                        height: '100%',
                    }}
                />
            </div>
        </div>
    );
}

scss: scss:


.h__diagramContainer {
    border: 1px solid green;
    width: 100%;
}

.h__diagramContainer:after {
    padding-top: 70%;
    display: block;
    content: '';
}

.h__columnContainer {
    display: flex;
}

.h__diagramColumn {
    width: calc(100% / 3);
}

.h__node {
    width: 20px;
    height: 20px;
}

How can I make the child divs take up all available vertical space so they can be used as flex containers?如何让子 div 占据所有可用的垂直空间,以便它们可以用作弹性容器?

Try setting the heights of the parent containers.尝试设置父容器的高度。

From MDN docs on CSS height来自MDN 文档 CSS 高度

The percentage is calculated with respect to the height of the generated box's containing block.该百分比是相对于生成框的包含块的高度计算的。 If the height of the containing block is not specified explicitly (ie, it depends on content height), and this element is not absolutely positioned, the value computes to auto.如果包含块的高度没有明确指定(即,它取决于内容高度),并且该元素不是绝对定位的,则该值计算为 auto。

So right now your parent container has a height of 250px but its direct child, .h__diagramContainer , has height auto.所以现在你的父容器的高度为 250px,但它的直接子.h__diagramContainer具有自动高度。 So further down when .h__diagramColumn has height set to 100%, it's essentially 100% of whatever content is already in there.因此,当.h__diagramColumn将高度设置为 100% 时,它基本上是已经存在的任何内容的 100%。

Instead, if you just set height 100% for .h__diagramContainer and .h__columnContainer then your .h__diagramColumn children will be 100% of the top level parent's full 250px height.相反,如果您只是为.h__diagramContainer.h__columnContainer设置了 100% 的高度,那么您的.h__diagramColumn子项将是顶级父项的 250px 高度的 100%。

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

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