Text Overlay bei Bildern: "live"-Version komplett falsch (CSS)?

1 Antwort

  1. Entferne die Attribute für Höhen und Breiten
  2. Pack die Bilder in einen eigenen Container (ration-wrap)
  3. Gib dem Container ein position: relative
  4. Füg ein <span> Element als Label ein
  5. Positioniere das <span> absolute innerhalb des Containers

HTML:

<div class="row">
  <div class="ratio-wrap">
    <img src="path/image1.jpg" alt="desc">
    <span class="label">Neu</span>
  </div>
  <div class="ratio-wrap">
    <img src="path/image2.jpg" alt="desc">
  </div>
</div>

CSS:

*, *::before; *::after {
  box-sizing: border-box;
  padding: unset;
  margin: unset;
}

img {
  width: 100%;
  height: auto;
}

.ration-wrap {
  max-width: 200px;
  aspect-ratio: 1;
  object-fit: cover;
  position: relative;
}

.ratio-wrap .label {
  position: absolute;
  inset: 0.5rem 0.5rem auto auto;
  color: #fff;
  background-color: #000;
  border-radius: 5px;
  font: 13px sans-serif;
}

LG medmonk

Woher ich das weiß:Berufserfahrung – UI/UX Designer, Full-Stack Developer