Πώς να διατηρήσετε την αναλογία πλάτους και ύψους
- Προηγούμενη σελίδα Σκίαση κύλισης
- Επόμενη σελίδα Ανταποκρίσιμος ενσωματωμένος πλαισίο
Μάθετε πώς να χρησιμοποιείτε το CSS για τη διατήρηση της αναλογίας πλάτους και ύψους των στοιχείων.
Αναλογία πλάτους και ύψους
Δημιουργία στοιχείων που μπορούν να προσαρμοστούν εύκολα σε μέγεθος, διατηρώντας παράλληλα το αναλογία πλάτους και ύψους (4:3, 16:9 κ.λπ.):

What is Aspect Ratio?
The aspect ratio of an element describes the proportional relationship between its width and height. Two common video aspect ratios are 4:3 (the universal video format of the 20th century) and 16:9 (the universal format for high-definition television and European digital television as well as YouTube videos).
How to Implement - Height equals Width
Step 1 - Add HTML:
Use container elements, such as <div>
If you want it to contain text, add a child element:
<div class="container"> <div class="text">Some text</div> <!-- If you want text inside the container --> </div>
Step 2 - Add CSS:
for padding-top
Add percentage values to maintain the aspect ratio of the DIV. The following example will create a DIV with a 1:1 aspect ratio (height and width are always equal):
Example - 1:1 Aspect Ratio
.container { background-color: red; width: 100%; padding-top: 100%; /* 1:1 Aspect Ratio */ position: relative; /* If you want text inside */ } /* If you want text inside the container */ .text { position: absolute; top: 0; left: 0; bottom: 0; right: 0; }
Other Aspect Ratios
Example - 16:9 Aspect Ratio
.container { padding-top: 56.25%; /* 16:9 Aspect Ratio (9 divided by 16 equals 0.5625) */ }
Example - 4:3 Aspect Ratio
.container { padding-top: 75%; /* 4:3 Aspect Ratio (3 divided by 4 equals 0.75) */ }
Example - 3:2 Aspect Ratio
.container { padding-top: 66.66%; /* 3:2 Aspect Ratio (2 divided by 3 equals 0.6666) */ }
Example - 8:5 Aspect Ratio
.container { padding-top: 62.5%; /* 8:5 Aspect Ratio (5 divided by 8 equals 0.625) */ }
Πρότυπο CSS aspect-ratio
In newer browsers, you can simply use CSS aspect-ratio
Property:
Example - 3:2 Aspect Ratio
.container { aspect-ratio: 3 / 2; }
The numbers in the table indicate the first browser version that fully supports this property.
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
88 | 88 | 89 | 15 | 74 |
- Προηγούμενη σελίδα Σκίαση κύλισης
- Επόμενη σελίδα Ανταποκρίσιμος ενσωματωμένος πλαισίο