How can I match the text width to the width of a dynamically sized image/title?
P粉741678385
P粉741678385 2024-04-06 10:04:43
0
2
615

See this code pen: https://codepen.io/allen-houng/pen/XGMjMr?editors=1100#0

    <div>
      <img src="https://res.cloudinary.com/djcpf0lmv/image/upload/v1549372650/Templates/yoga.jpg" data-radium="true" style=";">
      <div class="description">
        Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum   venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
      </div>
    </div>

I have a parent div and two child divs - an image and a description. I resize the image based on the viewport height, which means the width will be dynamic and responsive. How can I resize the corresponding sibling div .description to match the width of the image without JavaScript?

In other words, how do I transform:

Enter this:

P粉741678385
P粉741678385

reply all(2)
P粉008829791

You can simply set the width of the image to 100%. Just add "width:100%;" into your img style tag to test like this:

<div>
  <img src="https://res.cloudinary.com/djcpf0lmv/image/upload/v1549372650/Templates/yoga.jpg" data-radium="true" style="width: 100%;">
  <div class="description">
    Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
  </div>
</div>

Or put it into a class:

.img-full {
  display: block;
  width: 100%;
}
<div>
  <img src="https://res.cloudinary.com/djcpf0lmv/image/upload/v1549372650/Templates/yoga.jpg" data-radium="true" class="img-full">
  <div class="description">
    Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
  </div>
</div>

"display: block" simply ensures that your img is always in it's own block, regardless of width. That is, your text won't appear next to it, only below or above it.

P粉030479054

Make the container inline-block (or any configuration that shrinks to fit, such as table, inline-grid, inline-flex, float, absolute, etc.) then force the text width to 0, thus defining the width of the container via the image (the text does not affect the width), then use min-width

Force the width to 100% again

.parent {
  background: pink;
  display:inline-block;
}

img {
  display: block;
  max-height: 70vh;
}

.description {
  width:0;
  min-width:100%;
}
<div class="parent">
    <img src="https://picsum.photos/id/1004/900/600">
  <div class="description">
    Fusce consequat. Nulla nisl. Nunc nisl. Duis bibendum, felis sed interdum   venenatis, turpis enim blandit mi, in porttitor pede justo eu massa. Donec dapibus. Duis at velit eu est congue elementum.
  </div>
</div>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template