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

标签 javascript html css flexbox

.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/

相关文章:

javascript - 原生 ios6 UIWebView 中的 Facebook Javascript 登录(WORKS ON SAFARI)

javascript - 如何使用react-router-dom <Link> 防止事件冒泡?

css - 在 react Bootstrap 中自定义组件样式

css - Firefox 上的样式滚动条

css - div定位——一个接一个的放

html - 如何使带边框(背景图像)的居中布局和页脚贴在底部?

javascript - 使用 D3 的自定义布局

javascript - 更改 url 位置的最佳方法

javascript - Amazon Cloudwatch Graphs - 提供类似图表的图书馆?

javascript - 如何在加载第 3 方 iframe 时删除加载 gif