CSS Image Gallery
In this page:
.gallery img {
width: length;
height: auto;
}
.gallery {
display: flex | grid;
gap: length;
}
Gallery Grid Concept
एक image gallery visual cards को साफ़, structured grids में organize करती है। एक fluid layout structure define करके और अपनी images को styled cards के अंदर wrap करके, आप अपनी photos को किसी भी screen size पर साफ़ तरीके से aligned रख सकते हैं।
उदाहरण: The Gallery Grid Concept
<style>
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
}
img {
width: 100%;
height: 120px;
}
</style>
<div class="gallery">
<img src="https://placehold.co/150x120" alt="Photo">
<img src="https://placehold.co/150x120" alt="Photo">
</div>
Captions के साथ Image Cards को Style करना
अपनी gallery को interactive बनाने के लिए, आप अपनी images को styled captions या description texts के साथ जोड़ सकते हैं। image और description को एक styled card box में wrap करना जानकारी को अपने visitors के लिए साफ़ बना देता है।
उदाहरण: Styling Image Cards with Captions
<style>
figure {
margin: 0;
}
figcaption {
padding: 8px;
}
</style>
<figure>
<img src="https://placehold.co/200x120" alt="Photo">
<figcaption>A brief caption</figcaption>
</figure>
Interactive Hover Zoom Effects
आप अपनी image gallery को interactive और engaging बनाने के लिए custom hover animations (जैसे एक soft zoom effect) जोड़ सकते हैं। image को थोड़ा scale करके और card container के अंदर overflow को clip करके, आप एक polished, modern design बनाते हैं।
उदाहरण: Interactive Hover Zoom Effects
<style>
.card {
overflow: hidden;
}
.card img {
transition: transform 0.3s;
}
.card:hover img {
transform: scale(1.1);
}
</style>
<div class="card">
<img src="https://placehold.co/200x150" alt="Zooms on hover">
</div>
Flexbox के साथ Modern Galleries
Flexbox इस्तेमाल करना responsive image galleries बनाने का modern, standard तरीका है। यह आपके image cards को horizontally align करने और automatically नई lines में wrap होने देता है, किसी भी screen size के लिए perfectly adapt करते हुए।
उदाहरण: Modern Galleries with Flexbox
<style>
.gallery {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
img {
width: 150px;
}
</style>
<div class="gallery">
<img src="https://placehold.co/150x100" alt="Photo">
<img src="https://placehold.co/150x100" alt="Photo">
</div>
Gallery Images पर CSS Object-Fit इस्तेमाल करना
अगर आपकी gallery images के aspect ratios अलग-अलग हैं, तो वे grid में misaligned दिख सकती हैं। आप अपनी images को बिना distort किए अपने containers भरने के लिए automatically crop और scale करने के लिए CSS object-fit: cover property इस्तेमाल कर सकते हैं।
- object-fit: cover इस्तेमाल करते समय हमेशा अपने image elements पर एक defined height specify करें।
- Accepted values:
- fill — fit होने के लिए stretch होता है, distort हो सकता है (initial value)
- contain — aspect ratio रखते हुए अंदर fit होता है
- cover — aspect ratio रखते हुए box भरता है और crop करता है
- none — natural size
- scale-down — none और contain में से छोटा वाला
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Using CSS Object-Fit on Gallery Images
<style>
img {
width: 150px;
height: 150px;
object-fit: cover;
}
</style>
<img src="https://placehold.co/300x150" alt="Cropped to fit without distortion">
- अपनी gallery columns पर fixed pixel widths इस्तेमाल करना, जिससे mobile screens पर layout breakages होते हैं।
- hover zoom effects इस्तेमाल करते समय parent container पर overflow: hidden set करना भूल जाना, जिससे image बाहर फैल जाती है।
- image elements पर width और height dimensions छोड़ देना, जो slow-loading layout shifts पैदा करता है।
- Flexbox displays इस्तेमाल करके image cards को fluid, responsive grids में align करें।
- captions के लिए semantic figure और figcaption elements इस्तेमाल करके अपने gallery cards में context जोड़ें।
- बिना distortion के images को automatically crop और scale करने के लिए object-fit: cover इस्तेमाल करें, और responsive animations के लिए transition hover zoom effects लागू करें।
Standard image galleries और object-fit property हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline