简体   繁体   English

CSS:1)如何使浮动元素的高度不受子元素的影响? 2)如何在带有浮动的元素上指定最小高度?

[英]CSS: 1) how to make height of float element not affected by child element? 2) how to specify min-height on an element with float?

So, I have been searching for the answer for a day, but I still do not get the answer.所以,我一直在寻找答案一天,但我仍然没有得到答案。 I am not very good at CSS.我不太擅长 CSS。

The Case案子

I want to create 2 equal square (1:1 ratio) side-to-side (100% width) with a little bit of margin.我想创建 2 个相等的正方形(1:1 比例)并排(100% 宽度),并留有一点边距。 Inside of the square, there are smaller square(s).在正方形内部,有较小的正方形。 The min-height of the big square should be the width of the big square.大正方形的min-height应该是大正方形的宽度。


What I have done我做了什么

The big square大广场

I have done the big square, and It look like this .我做了大广场,它看起来像这样 Even though it looks square, it is not.尽管它看起来是方形的,但它不是。 The ratio is still not 1:1.比例还不是1:1。 What is the best approach to fix this?解决此问题的最佳方法是什么?

The small square小广场

I have tried, and this is the result.我试过了,就是结果。 The quantity of the small square should not affect the height of its parent.小方块的数量不应影响其父级的高度。 What should I do to fix this?我应该怎么做才能解决这个问题?


The code编码

.html : .html

<head>

    <link href="style.css" rel="stylesheet">

</head>

<body>

    <big-box id="left-box" style="background: blue;">
        <small-box style="background: white">
            <div>test</div>
        </small-box>
    </big-box>

    <big-box id="right-box" style="background: green;"></big-box>

</body>

.css : .css

big-box {
    background: pink;
    padding: 0.5% 0.5% calc(48% - 4px) 0.5%;
    margin: 2px;
    float: left;
    width: calc(48% - 4px);
    color: white;
    position: relative;
}

small-box {
    padding: 6%;
    float: left;
    background: #f1f1f1;
    margin: 1%;
    display: flex;
    justify-content: center;
    align-items: center;
    color: black;
    position: relative;
}

big-box div {
    position: absolute;
}

have you tried to use: aspect-ratio: 1 / 1;您是否尝试过使用:纵横比:1 / 1; – the padding gives all the headache, as margin for the small box would not change the bounding box – 填充让人头疼,因为小框的边距不会改变边界框

.big-box {
aspect-ratio: 1 / 1;
background: pink;
color: white;
float: left;
margin: 2px;
position: relative;
width: calc(48% - 4px);
}

.small-box {
align-items: center;
aspect-ratio: 1 / 1;
background: #f1f1f1;
color: black;
display: flex;
float: left;
justify-content: center;
margin: 0.5% 0.5% calc(48% - 4px) 0.5%;
/* margin: 1%; */
padding: 6%;
position: relative;
}

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

相关问题 如何在带有百分比的子元素中使用最小高度? - how to use min-height in child element with percentage? 如何避免在float元素和高度上使用div换行 - How to avoid div wrap on float element and height 如何在不考虑元素高度的情况下浮动元素 - how to float element without considering elements height 如何设置画布元素的最小宽度、最大宽度、最小高度、最大高度 - how to set min-width, max-width, min-height, max-height for canvas element 将子元素填充到父元素的100%且最小高度设置为100% - make child element fill 100% of parent with min-height set to 100% 为什么当父元素具有最小高度/最大高度值但没有高度值时,子元素的 height: 100% 不适用? - Why does height: 100% on a child element not apply when the parent element has a min-height/max-height value but no height value? 如何获取父元素的最小高度/宽度并通过 CSS 设置为子元素的高度/宽度 - How to get min height/width of parent element and set to it's child height/width by CSS 最小高度为100%的html元素的子项(具有相对位置)不适用于高度为100% - Child (with relative positioning) of html-element with min-height 100% does not apply height 100% 浮动元素适合整个高度 - Float element that fit whole height 如何使CSS&#39;min-height&#39;属性无效? - How can I make the CSS 'min-height' property ineffective?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM