动态文本div的宽度为20%,并且应与flexbox中的图像div具有相同的高度

时间:2017-04-13 05:59:09

标签: javascript html css css3 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>
&#13;
&#13;
&#13;

jsfiddle link

expected result enter image description here

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

1 个答案:

答案 0 :(得分:1)

要使其工作,您需要在最后一个弹性项目中添加一个额外的元素,位于绝对位置。

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