On this page11 sections
Lecture 040: Styling Images: Effects, Filters & object-fit
| Category | 7: Advanced Styling |
| Lecture | 040 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
Images are rarely shown exactly as they're uploaded. CSS rounds their corners, makes them responsive, crops them neatly, applies filters such as greyscale or blur, adds hover overlays and opens them in a modal. This lecture covers every common technique.
Learning Objectives
- Round, frame and centre images, and make them responsive
- Crop and position images with
object-fitandobject-position - Apply
filtereffects - Add hover overlays, and open an image in a modal
1. Shapes, Frames and Centring
Example
<style>
img { margin: 6px; vertical-align: middle; }
</style>
<img src="https://picsum.photos/id/1027/120/120" alt="Portrait" style="border-radius: 12px;">
<img src="https://picsum.photos/id/1027/120/120" alt="Portrait" style="border-radius: 50%;">
<img src="https://picsum.photos/id/1027/120/120" alt="Portrait" style="border: 6px solid white; box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);">
<img src="https://picsum.photos/id/1027/120/120" alt="Portrait" style="padding: 4px; border: 1px solid #ced4da; border-radius: 6px;">
<img src="https://picsum.photos/id/1015/300/140" alt="River" style="display: block; margin: 12px auto;">border-radius: 50% on a square image makes a circle. display: block; margin: auto centres it.
Responsive: never wider than its container:
img { max-width: 100%; height: auto; }2. object-fit: Crop Without Stretching
When an image is forced into a box of a different shape:
Example
<style>
.fit { width: 140px; height: 140px; border: 1px solid #ced4da; margin: 4px; background: #f1f3f5; }
</style>
<img class="fit" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-fit: fill;">
<img class="fit" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-fit: contain;">
<img class="fit" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-fit: cover;">
<img class="fit" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-fit: none;">| Value | Result |
|---|---|
fill | Stretches to fit (default; may squash) |
contain | Whole image visible, with empty bands |
cover | Fills the box and crops the overflow (most useful) |
none | Original size, cropped by the box |
scale-down | The smaller of none and contain |
3. object-position: Which Part to Keep
Example
<style>
.crop { width: 120px; height: 120px; object-fit: cover; margin: 4px; }
</style>
<img class="crop" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-position: left;">
<img class="crop" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-position: center;">
<img class="crop" src="https://picsum.photos/id/1016/400/200" alt="Canyon" style="object-position: right;">4. Filters
Example
<style>
.f { width: 110px; margin: 3px; }
figure { display: inline-block; margin: 0; text-align: center; font: 11px monospace; }
</style>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: none;"><figcaption>none</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: grayscale(100%);"><figcaption>grayscale</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: blur(3px);"><figcaption>blur</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: brightness(140%);"><figcaption>brightness</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: contrast(180%);"><figcaption>contrast</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: sepia(100%);"><figcaption>sepia</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: hue-rotate(120deg);"><figcaption>hue-rotate</figcaption></figure>
<figure><img class="f" src="https://picsum.photos/id/1080/220/160" alt="Strawberries" style="filter: drop-shadow(4px 4px 6px rgb(0 0 0 / 0.5));"><figcaption>drop-shadow</figcaption></figure>Combine them with spaces: filter: grayscale(100%) blur(1px);. backdrop-filter: blur(8px)
blurs whatever is behind an element, giving a frosted-glass effect.
5. Hover Effects and Overlays
Example
<style>
.card { position: relative; width: 220px; overflow: hidden; border-radius: 12px; display: inline-block; margin: 4px; }
.card img { display: block; width: 100%; transition: transform 0.4s, filter 0.4s; }
.card .overlay { position: absolute; inset: 0; display: grid; place-items: center; color: white; font: 600 18px system-ui;
background: rgb(0 0 0 / 0.5); opacity: 0; transition: opacity 0.3s; }
.card:hover img { transform: scale(1.1); }
.card:hover .overlay { opacity: 1; }
.gray img { filter: grayscale(100%); }
.gray:hover img { filter: none; }
</style>
<div class="card"><img src="https://picsum.photos/id/1036/440/300" alt="Snowy forest"><div class="overlay">Zoom + overlay</div></div>
<div class="card gray"><img src="https://picsum.photos/id/1039/440/300" alt="Waterfall"></div>6. An Image Modal
Example: click the thumbnail:
<style>
.thumb { width: 160px; cursor: zoom-in; border-radius: 8px; }
dialog.lightbox { padding: 0; border: 0; background: transparent; max-width: 90vw; }
dialog.lightbox img { display: block; max-width: 100%; max-height: 80vh; border-radius: 8px; }
dialog.lightbox::backdrop { background: rgb(0 0 0 / 0.75); }
</style>
<img class="thumb" src="https://picsum.photos/id/1018/320/200" alt="Mountain lake" onclick="document.getElementById('lb').showModal()">
<dialog id="lb" class="lightbox" onclick="this.close()">
<img src="https://picsum.photos/id/1018/900/560" alt="Mountain lake, full size">
</dialog>A <dialog> (HTML Lecture 038) does the hard work: dimming, Esc to close, and focus handling.
Common Beginner Mistakes
1. Stretched images. Use object-fit: cover instead of forcing both width and height.
2. Giant images. A 4000px photo shown at 300px wastes data. Resize the files.
3. Hover-only information. Touch users can't hover. Don't hide essential text in overlays.
Practice Questions
- Make a square image into a circle.
- Which
object-fitvalue fills a box and crops the rest? - Turn an image black and white.
- How do you combine two filters?
- How do you show an overlay only on hover?
- What does
backdrop-filter: blur()blur?
Practice Answers
border-radius: 50%;coverfilter: grayscale(100%);- Separate them with a space:
filter: blur(2px) brightness(80%); - Give the overlay
opacity: 0, andopacity: 1on.card:hover .overlay. - The content behind the element.
Key Takeaways
border-radius, borders and shadows frame images;max-width: 100%makes them responsive.object-fit: cover+object-positioncrop neatly.filterapplies greyscale, blur, brightness and more;backdrop-filterblurs what's behind.- Overlays use
position: absolute+opacity; modals use<dialog>.