gpt4 book ai didi

javascript - 动态文本 div 的宽度为 20%,高度应与 flexbox 中的图像 div 相同

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:52 25 4
gpt4 key购买 nike

.container {
display: flex;
justify-content: center;
}

.item:first-child {
width: 40%;
background: pink;
padding: 10px;
margin: 10px;
height: min-content;
}

.item:last-child {
width: 20%;
margin: 10px;
padding: 10px;
background: pink;
overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="item">
<img src="https://www.w3schools.com/css/img_fjords.jpg" width="100%">
</div>
<div class="item">
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
</div>
</div>

jsfiddle link

expected result enter image description here

我正在尝试使用 CSS 使文本 div (.item:last-child) 与图像 div (.item:first-child) 的高度相同,图像宽度可以适应不同的屏幕尺寸,文本div 可以滚动。我可以通过 JS/jQuery 代码(现在评论)来做到这一点,但不知道通过纯 CSS 来做到这一点。谁能帮我解决这个问题?谢谢。

最佳答案

为此,您需要在最后一个 flex 元素中添加一个绝对定位的额外元素。

.container {
display: flex;
justify-content: center;
}

.item:first-child {
width: 40%;
background: pink;
padding: 10px;
margin: 10px;
height: min-content;
}

.item:last-child {
position: relative;
width: 20%;
margin: 10px;
padding: 10px;
background: pink;
}

.item:last-child .scroll {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
}
<div class="container">
<div class="item">
<img src="https://www.w3schools.com/css/img_fjords.jpg" width="100%">
</div>
<div class="item">
<div class="scroll">
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
<p>This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.This is the test paragraph.</p>
</div>
</div>
</div>

关于javascript - 动态文本 div 的宽度为 20%,高度应与 flexbox 中的图像 div 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43384567/

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