On this page11 sections
  1. Learning Objectives
  2. 1. Shapes, Frames and Centring
  3. 2. object-fit: Crop Without Stretching
  4. 3. object-position: Which Part to Keep
  5. 4. Filters
  6. 5. Hover Effects and Overlays
  7. 6. An Image Modal
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 040: Styling Images: Effects, Filters & object-fit

Category7: Advanced Styling
Lecture040 of 057
Estimated video length14 min
TargetCSS3 · any modern browser
LevelIntermediate

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-fit and object-position
  • Apply filter effects
  • Add hover overlays, and open an image in a modal

1. Shapes, Frames and Centring

Example

html
<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:

css
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

html
<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;">
ValueResult
fillStretches to fit (default; may squash)
containWhole image visible, with empty bands
coverFills the box and crops the overflow (most useful)
noneOriginal size, cropped by the box
scale-downThe smaller of none and contain

3. object-position: Which Part to Keep

Example

html
<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

html
<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

html
<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:

html
<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

  1. Make a square image into a circle.
  2. Which object-fit value fills a box and crops the rest?
  3. Turn an image black and white.
  4. How do you combine two filters?
  5. How do you show an overlay only on hover?
  6. What does backdrop-filter: blur() blur?

Practice Answers

  1. border-radius: 50%;
  2. cover
  3. filter: grayscale(100%);
  4. Separate them with a space: filter: blur(2px) brightness(80%);
  5. Give the overlay opacity: 0, and opacity: 1 on .card:hover .overlay.
  6. The content behind the element.

Key Takeaways

  • border-radius, borders and shadows frame images; max-width: 100% makes them responsive.
  • object-fit: cover + object-position crop neatly.
  • filter applies greyscale, blur, brightness and more; backdrop-filter blurs what's behind.
  • Overlays use position: absolute + opacity; modals use <dialog>.