Μετάβαση στο κύριο περιεχόμενο
← 🎨 CSS as a design system·Ενότητα B6 · Μάθημα 4
ΕργασίαBuild three boxes with locked ratios. .box-square (aspect-ratio: 1, width: 200px, bg #fee). .box-video (aspect-ratio: 16/9, width: 100%, max-width: 320px, bg #eef). .box-portrait (aspect-ratio: 3/4, width: 150px, bg #efe).

αναλογία διαστάσεων: διακοπή των υπολογισμών ύψους

75 XP6 min
Θεωρία

Lock the proportions

.video { aspect-ratio: 16 / 9; width: 100%; }
.square { aspect-ratio: 1; }
.portrait { aspect-ratio: 3 / 4; }

Set EITHER width OR height; the browser computes the other. No more "padding-bottom: 56.25%" hacks.

When this saves you

  • Video / iframe embeds. YouTube needs 16:9. Twitch needs 16:9. Vimeo needs 16:9. aspect-ratio sets it once.
  • Image cards where you want consistent height regardless of source-image dimensions:
.card-image {
  aspect-ratio: 4 / 3;
  width: 100%;
  object-fit: cover;     /* crop to fit, don't squish */
}
  • Hero blocks with text overlay — the text always has the same proportional vertical space.

With img tags (use the attributes too)

Even when you set CSS aspect-ratio, the <img> tag should have its OWN width + height attributes for CLS prevention (A7 lesson 7). Browser uses the attributes BEFORE the image loads. CSS aspect-ratio is the canonical answer for non-image elements.

Sub-properties

aspect-ratio: auto 16 / 9 — "intrinsic if the element has natural dimensions, else 16:9." Useful for <img> fallback behavior.

🔒

Εγγράψου για να αρχίσεις να προγραμματίζεις

Η θεωρία είναι ανοιχτή για όλους. Ο επεξεργαστής, η ζωντανή προεπισκόπηση και ο έλεγχος ξεκλειδώνουν με 7-ήμερη δωρεάν δοκιμή — απαιτείται κάρτα.

Εγγραφή — δωρεάν δοκιμή →

Τα πρώτα 10 μαθήματα κάθε διαδρομής είναι δωρεάν. Δεν χρειάζεται κάρτα.

← ΠροηγούμενοΕπόμενο μάθημα →

Μια συμβουλή Python ή web την ημέρα — με email

Σύντομο, γραμμένο στο χέρι, χωρίς spam. Διαγραφή με ένα κλικ.