there is 2 ways to do it, 1. id using flex , simple and effective, 2nd is using the position absolute, which you were trying
The problem with this is, that when you use position absolute to inner items, then they behave like floating items and their height and width doesn't add to parent's height or width, in your case the main container does not have heigh to center the text and you are facing this issue
Solution: Try using min-height or height on parent container, I have used 100vh
to make its height equal to body height.
.badge-container {
position: relative;
text-align: center;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.badge-card {
top: -10px;
position: absolute;
left: -10px;
z-index: 9;
height: 100%;
width: 100%;
object-fit: cover;
}
.badge-text-centered {
color: black;
font-weight: bolder;
font-size: 14px;
z-index: 10;
}
<div class="badge-container">
<img class="badge-card"
src="https://homepages.cae.wisc.edu/~ece533/images/fruits.png"
alt="Grapefruit slice atop a pile of other slices">
<div class="badge-text-centered">New<br> Image</div>
</div>
Check this second option, the approach which you were using,
.badge-container {
position: relative;
text-align: center;
height: 100vh;
}
.badge-card {
top: -10px;
position: absolute;
left: -10px;
z-index: 9;
object-fit: cover;
height: 100%;
width: 100%;
}
.badge-text-centered {
position: absolute;
top: 50%;
left: 50%;
color: black;
font-weight: bolder;
font-size: 14px;
transform: translate(-50%, -50%);
z-index: 10;
}
<div class="badge-container">
<img class="badge-card"
src="https://homepages.cae.wisc.edu/~ece533/images/fruits.png"
alt="Grapefruit slice atop a pile of other slices">
<div class="badge-text-centered">New<br> Image</div>
</div>