I have a flex container that changes width according to the resolution.
Inside it there is a div that has a relative width to it with flex items inside that also change relatively.
Some of these flex items are images with text positioned in different areas.
What units should I use for this text so that it will appear as it’s part of the image and changes together with the image?
* {
padding: 0;
margin: 0;
}
.flex_container {
display: flex;
flex-direction: flex-column;
width: 50%;
}
.flex_image {
position: relative;
width: 80%;
}
.flex_image img {
width: 100%;
}
.flex_image_text {
position: absolute;
bottom: 0;
}
.flex_image_text p {
color: red;
font-size: 5rem;
font-weight: 700;
}
<div class="flex_container">
<div class="flex_image">
<img src="https://images.pexels.com/photos/1049298/pexels-photo-1049298.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1">
<div class="flex_image_text">
<p>
Hello
</p>
</div>
</div>
</div>
In this snippet you can see that when you resize the screen resolution, the text size doesn’t change because the units aren’t relative.
But what are the correct units that will make it look like it’s "part of the image"?
4
Answers
As your image is sized relatively you can size the font e.g. in relation to vw units too.
If you just want your font size to skill with the image, try using vw instead of rem with your font size like so:
vw directly correlates with your viewport’s size
You can use something like this:
Where
Following up on the comment thread with @a-haworth and @stackerito, here’s a solution that uses container query units.
As the number of cards in a row changes from 1 to 2 to 3 as you resize the window, the text resizes appropriately relative to the width of the card.