How To Scale And Crop Images With CSS Object-fit | DigitalOcean
Có thể bạn quan tâm
Real-World UI Component Examples
Card Layout with Uniform Image Sizes
E-commerce and content cards often require images of different aspect ratios to fit identical containers:
<div class="card-grid"> <div class="card"> <img src="product-1.jpg" alt="Product 1" class="card-image"> <h3>Product Title</h3> <p>Description text</p> </div> <div class="card"> <img src="product-2.jpg" alt="Product 2" class="card-image"> <h3>Product Title</h3> <p>Description text</p> </div> </div> .card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 1.5rem; } .card-image { width: 100%; height: 200px; object-fit: cover; object-position: center; border-radius: 8px 8px 0 0; }This ensures all product images fill their 200px-tall containers uniformly, cropping as needed while maintaining aspect ratios.
Responsive Hero Image
Hero sections need images that fill the viewport width while maintaining a consistent height:
<header class="hero"> <img src="hero-image.jpg" alt="Hero banner" class="hero-image"> <div class="hero-content"> <h1>Welcome to Our Site</h1> <p>Compelling headline text</p> </div> </header> .hero { position: relative; width: 100%; height: 60vh; overflow: hidden; } .hero-image { width: 100%; height: 100%; object-fit: cover; object-position: center center; } .hero-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center; color: white; z-index: 1; }The image covers the entire hero container at any viewport size, with content overlaid on top.
Avatar Grid with Circular Cropping
User avatars in social feeds or team pages benefit from object-fit combined with border-radius:
<div class="avatar-grid"> <img src="user1.jpg" alt="User 1" class="avatar"> <img src="user2.jpg" alt="User 2" class="avatar"> <img src="user3.jpg" alt="User 3" class="avatar"> </div> .avatar-grid { display: flex; gap: 1rem; } .avatar { width: 60px; height: 60px; object-fit: cover; object-position: center top; /* Focus on faces */ border-radius: 50%; border: 2px solid #fff; }Circular avatars maintain consistent sizing regardless of source image dimensions, with faces positioned in the visible area.
Gallery with Mixed Aspect Ratios
Image galleries often contain photos with varying aspect ratios that need to display uniformly:
<div class="gallery"> <img src="photo1.jpg" alt="Photo 1" class="gallery-item"> <img src="photo2.jpg" alt="Photo 2" class="gallery-item"> <img src="photo3.jpg" alt="Photo 3" class="gallery-item"> </div> .gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; } .gallery-item { width: 100%; height: 250px; object-fit: cover; object-position: center; border-radius: 4px; cursor: pointer; transition: transform 0.2s; } .gallery-item:hover { transform: scale(1.05); }All gallery images fill their grid cells uniformly, creating a cohesive visual layout.
Từ khóa » Html Image Stretch
-
Html - How Do You Stretch An Image To Fill A
While Keeping The ...CSS Object-fit Property - W3Schools
How To Resize An Image In HTML?
HTML Stretch Background Image
How To Stretch And Scale An Image In The Background With CSS
How To Auto-Resize The Image To Fit An HTML Container - W3docs
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
How To Resize An Image With HTML - Computer Hope
Css Stretch Image To Fill Div Code Example - Code Grepper
Make Image Stretch To Fit Div Code Example
Object-fit - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
Object-fit | CSS-Tricks
Stretch Background Image
How To Make An Image Stretch Full Width Css?
Copyright © 2022 | Thiết Kế Truyền Hình Cáp Sông Thu