.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
我正在尝试使用 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>
我是一名优秀的程序员,十分优秀!