gpt4 book ai didi

html - 使用 flex 和 line-height+vh 时如何对齐尺寸?

转载 作者:行者123 更新时间:2023-12-04 18:55:08 48 4
gpt4 key购买 nike

我收到了 pretty close to what I want但是我认为 32vh 正在破坏我的体型。

当我使用 img {height: 100%;}它们变得太大了,所以我安定下来 32vh;然而,左右 div 相差几个像素。如果放大一点(按 ctrl+'+'),它会特别明显。它发生在 Firefox 和 chrome 上。我尝试在图像上使用 50% 和 100% 的高度,并在 testB 上放置相对位置,但似乎没有帮助。如何使两个大小相等的高度?我不介意右侧的两个 div 之间是否有额外的像素,但它们必须与左侧的高度相同,即 div a

body * {
box-sizing: border-box;
color: white;
}

.test {
margin: 0 auto;
position: relative;
height: 65vh;
width: 80vw;
display: flex;
text-align: center;
}

.testA {
background-color: black;
width: 61.5%;
display: inline-block;
padding-right: 18px;
line-height: 65vh;
}

.testB {
background-color: green;
flex-grow: 1;
line-height: 32vh;
position: relative;
}

.testC {
background-color: royalblue;
vertical-align: top;
margin-bottom: 10px;
}

.testD {
flex-grow: 1;
background-color: purple;
vertical-align: bottom;
margin-top: 10px;
}

.test img {
vertical-align: middle;
}

.testA img {
height: 100%;
}

.testC img {
vertical-align: top;
height: 32vh;
}

.testD img {
vertical-align: bottom;
height: 32vh;
}
<div class="test">
<div class="testA">
a
</div>
<div class="testB">
<div class="testC">
c
</div>
<div class="testD">
d
</div>
</div>
</div>
<hr>
<div class="test">
<div class="testA">
<img src="https://placehold.it/300x500">
</div>
<div class="testB">
<div class="testC">
<img src="https://placehold.it/300x500">
</div>
<div class="testD">
<img src="https://placehold.it/300x500">
</div>
</div>
</div>
<hr>
<div class="test">
<div class="testA">
<img src="https://placehold.it/500x300">
</div>
<div class="testB">
<div class="testC">
<img src="https://placehold.it/500x300">
</div>
<div class="testD">
<img src="https://placehold.it/500x300">
</div>
</div>
</div>

最佳答案

我希望这是你所期望的:

<style>
body * {
box-sizing: border-box;
color: white;
}

.test {
margin: 0 auto;
position: relative;
height: 65vh;
width: 80vw;
display: flex;
}

.testA {
background-color: black;
width: 61.5%;
display: inline-block;
padding-right: 18px;
line-height: 65vh;
text-align: center;
}

.testB {
background-color: green;
flex-grow: 1;
display: flex;
flex-direction: column;
line-height: 32vh;
text-align: right;
position: relative;
}

.testC {
display: flex;
align-items: center;
justify-content: center;
background-color: royalblue;
vertical-align: top;
}

.testD {
display: flex;
align-items: center;
justify-content: center;
background-color: purple;
flex-grow: 1;
vertical-align: bottom;
}

.testA img {
height: 100%;
}

.testC img {
vertical-align: top;
height: 32.5vh;
}

.testD img {
vertical-align: bottom;
height: 32.5vh;
}
</style>

<div class="test">
<div class="testA">
a
</div>
<div class="testB">
<div class="testC">
c
</div>
<div class="testD">
d
</div>
</div>
</div>
<hr>
<div class="test">
<div class="testA">
<img src="https://placehold.it/300x500">
</div>
<div class="testB">
<div class="testC">
<img src="https://placehold.it/300x500">
</div>
<div class="testD">
<img src="https://placehold.it/300x500">
</div>
</div>
</div>
<hr>
<div class="test">
<div class="testA">
<img src="https://placehold.it/500x300">
</div>
<div class="testB">
<div class="testC">
<img src="https://placehold.it/500x300">
</div>
<div class="testD">
<img src="https://placehold.it/500x300">
</div>
</div>
</div>

关于html - 使用 flex 和 line-height+vh 时如何对齐尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59855278/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com