iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS Style Images

CSS gives you a lot of control over how images render — rounding, cropping, filtering, sizing — without ever editing the source file.

The most-used image properties

PropertyEffect
border-radiusRounded corners. 50% for circular avatars.
object-fitcover crops to fill; contain fits inside.
object-positionWhere the kept part sits.
aspect-ratioLock the shape regardless of source.
filterGreyscale, blur, brightness, hue-rotate…
mix-blend-modeBlend the image with the background.
clip-pathCrop the image to any shape.

Avatar in three lines

CSS
.avatar {
  width: 64px; height: 64px;
  border-radius: 50%;
  object-fit: cover;
}

Polaroid card

CSS
.polaroid img {
  aspect-ratio: 4 / 3;
  width: 100%;
  object-fit: cover;
  filter: saturate(1.1) contrast(1.05);
  border-radius: 4px;
}
Tip: Combine aspect-ratio with object-fit: cover for galleries — every tile keeps a consistent shape no matter the source image.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS Style Images</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Crop this thumbnail to a square without distortion.

.thumb { width: 120px; height: 120px; object-fit: ; }

Test yourself

Q1. Which value of `object-fit` crops without distortion?
Q2. Which property locks an image to a 4:3 shape?
Q3. Which property applies blur, brightness, or grayscale?

Discussion

Loading…