0

I'm pretty new to CSS and I'm trying to build a Card-Component.

My problem is that the div with the text always adjusts to the height of the image and not the other way around. My goal is to have the image scale down in height so that it is exactly as high as the text-divs minimal height.

The Text-Content will be dynamic, so the height of the text-box will vary

current state: Image

Planned result: Image

Here's my current code:

* {
  box-sizing: border-box;
}

.wrapper {
  /* width: 768px; */
  margin-left: auto;
  margin-right: auto;
}

.flex {
  display: flex;
  align-items: center;
  flex-direction: row;
}

.flex-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex-wrap: wrap;
  width: 50%;
  max-width: 50%;
  margin: 10px;
}

.card {
  padding: 50px;
  background: green;
}
img {
  width: 100%;
  object-fit: fill;
}
<section>
  <div class="wrapper">
  <div class="flex">
    <div class="flex-item card">
      <p>Some example Text</p>
    </div>
    <div class="flex-item image">
      <img src="https://dummyimage.com/600x400/000000/fff.png" />
    </div>
  </div>
  </div>
</section>

JSFiddle code

Any guidance is much appreciated!

uff123
  • 3
  • 2

1 Answers1

1

Specify a height on your flex-item then use height: inherit on your image. I used the height that the card was rendering at to demonstrate.

* {
  box-sizing: border-box;
}

.wrapper {
  /* width: 768px; */
  margin-left: auto;
  margin-right: auto;
}

.flex {
  display: flex;
  align-items: center;
  flex-direction: row;
  height: 100vh;
}

.flex-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex-wrap: wrap;
  width: 50%;
  max-width: 50%;
  margin: 10px;
  height: 155px;
}

.card {
  padding: 50px;
  background: green;
}

img {
  width: 100%;
  object-fit: fill;
  height: inherit;
}
<section>
  <div class="wrapper">
    <div class="flex">
      <div class="flex-item card">
        <p>Some example Text</p>
      </div>
      <div class="flex-item image">
        <img src="https://dummyimage.com/600x400/000000/fff.png" />
      </div>
    </div>
  </div>
</section>
Kameron
  • 10,240
  • 4
  • 13
  • 26
  • The content in the box will be dynamic, so this won't work in that case. Added this to my post now. – uff123 Apr 06 '22 at 16:14