0

.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

I'm trying to make the text div (.item:last-child) the same height as the image div (.item:first-child) using CSS, the image width can accommodate with different screen size, the text div can scroll. I can do it by JS/jQuery code (commented now), but don't know do it by pure CSS. Can anyone help me on this issue? Thank you.

Asons
  • 84,923
  • 12
  • 110
  • 165
Terry
  • 3
  • 3
  • I don't think this is a duplicate issue, there are no fixed width or height. The image has 40% width, and the text div has 20% width. They are aligned by flex-box justify-content: center. If you open the jsfiddle and drag the middle bar to resize it you will see the difference. – Terry Apr 13 '17 at 07:33
  • I misunderstood the question, so I reopened it. I know there is another post that would suite as a dupe, though I can't find right now, so I posted an answer for you. – Asons Apr 13 '17 at 08:27

1 Answers1

1

For that to work you need an extra element inside the last flex item, positioned absolute.

.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>
Asons
  • 84,923
  • 12
  • 110
  • 165