|
1 | 1 | ---
|
2 |
| -title: styling <img> elements and example layouts |
3 |
| -description: styling <img> elements and example layouts |
| 2 | +title: New CSS features for images |
| 3 | +description: New CSS features for images |
4 | 4 | sidebar_sort_priority: 1000
|
5 | 5 | date_published: '2023-03-29'
|
6 |
| -date_modified: '2023-03-31' |
| 6 | +date_modified: '2024-02-07' |
7 | 7 | ---
|
8 | 8 |
|
9 |
| -# Styling `<img>` elements and example layouts |
| 9 | +Modern CSS empowers developers with concise, effective tools for robust image handling: |
10 | 10 |
|
11 |
| -## Styling `<img>` elements |
| 11 | +- **Aspect Ratio:** |
| 12 | + The `aspect-ratio` property enforces fixed proportions, stabilizing layouts by preventing unexpected shifts while images load. |
| 13 | + ```css |
| 14 | + .container { |
| 15 | + aspect-ratio: 16/9; |
| 16 | + } |
| 17 | + ``` |
12 | 18 |
|
13 |
| -TODO: |
| 19 | +- **Object Fit & Object Position:** |
| 20 | + Use `object-fit` (e.g., `cover`, `contain`) and `object-position` to precisely control image scaling and alignment within their containers, eliminating awkward cropping or distortion. |
| 21 | + ```css |
| 22 | + img { |
| 23 | + object-fit: cover; |
| 24 | + object-position: center; |
| 25 | + } |
| 26 | + ``` |
| 27 | + |
| 28 | +- **Image-Set for Resolution Adaptability:** |
| 29 | + The `image-set()` function offers resolution switching for background images, ensuring optimal clarity on high-DPI devices. |
| 30 | + ```css |
| 31 | + .hero { |
| 32 | + background-image: image-set( url( "hero.png") 1x, url( "[email protected]") 2x); |
| 33 | + } |
| 34 | + ``` |
| 35 | + |
| 36 | +- **Additional Enhancements:** |
| 37 | + `image-rendering` fine-tunes scaling algorithms for crisp output (ideal for pixel art), while `filter` enables dynamic visual effects directly in CSS. |
| 38 | + |
| 39 | +These features significantly streamline image presentation, reducing reliance on JavaScript hacks and complex workarounds, thus markedly easing everyday development tasks. |
0 commit comments