Skip to main content

Kombiniertes Aspect-ratio

Durch die 2021 in Safari erhaltene Unterstützung für aspect-ratio entsteht die Möglichkeit, mit nur ein paar wenigen Zeilen CSS mehrere Bilder schön anzuzeigen.



4:3
2:3
3:2
16:9
1:1

Curious why this works? Check out my Blog post.

Plus das CSS dazu:

.row {
  display: flex;
  gap: 1rem;
}

.item {
  flex-basis: 0;
  aspect-ratio: var(--ratio);
  flex-grow: calc(var(--ratio));
}

/* Additional Styling */

body {
  background: #003;
  color: #fff;
  padding: 2rem;
  padding-bottom: 4rem;
}

.row {
  max-width: 50rem;
  border: 1px dashed rgb(255 255 255 / 0.4);
  margin-inline: auto;
  padding: 0.5rem;
}

.row + .row {
  margin-top: 1rem;
}

.item {
  background: #f09;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  position: relative;
}

.item + .item { background: #09f;}

.item + .item + .item { background: #90f;}

footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  text-align: center;
  background: #002;
  color: rgb(255 255 255 / 0.6);
  
  a {
    color: #f09;
  }
  
  p {
    margin: 1rem;
  }
}