← Back to Chapters

CSS Image Styling

?️ CSS Image Styling

⚡ Quick Overview

Learn how to style images using CSS to enhance your web pages. You will see how to round images, create thumbnails, make images responsive, build polaroid-style cards, and control transparency with opacity.

Well-styled images improve the visual design, readability, and user experience of your website.

? Key Concepts

  • border-radius – rounds image corners or creates perfect circles.
  • Thumbnails – small framed images using borders, padding, and hover effects.
  • Responsive images – images that scale nicely on all screen sizes using max-width: 100%.
  • Polaroid cards – image cards with shadows and captions for a photo-gallery feel.
  • opacity – controls how transparent an image appears.

? Rounded & Circle Images

You can use the border-radius property to create rounded corners or circular images.

? View Code Example – Rounded & Circle Images
/* Apply rounded corners and circle shapes to images */
img.rounded {
  border-radius: 8px;
  width: 200px;
  height: 250px;
}

img.circle {
  border-radius: 50%;
  width: 200px;
  height: 200px;
}

? Live Output Preview

Rounded image: has slightly curved corners using border-radius: 8px;.

Circle image: becomes a perfect circle using border-radius: 50%; with equal width and height.

Example:

Rounded Image Circle Image

?️ Thumbnail Images

Thumbnails are small preview images often used in galleries. You can create them using borders, padding, and hover effects.

? View Code Example – Thumbnail Images
/* Style images as neat thumbnails with hover shadow */
img.thumbnail {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 5px;
  width: 150px;
}

img.thumbnail:hover {
  box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);
}

? Live Output Preview

The image appears smaller with a light border and padding. On hover, a soft shadow makes it look clickable and modern.

Example:

Thumbnail Image

? Responsive Images

Responsive images automatically adjust their size to fit the screen width. This prevents overflow and horizontal scrolling.

? View Code Example – Responsive Image
/* Make images shrink to fit smaller screens while keeping aspect ratio */
img.responsive {
  max-width: 100%;
  height: auto;
}

? Live Output Preview

The responsive image will never be wider than its container. On smaller screens, the image shrinks proportionally.

Example:

Responsive Image

?️ Polaroid Images / Cards

You can create a polaroid-style effect by wrapping an image inside a card with a shadow, background color, and caption.

? View Code Example – Polaroid Card
/* Create a polaroid-style image card with caption */
div.polaroid {
  width: 80%;
  max-width: 300px;
  background-color: white;
  box-shadow:
    0 4px 8px 0 rgba(0,0,0,0.2),
    0 6px 20px 0 rgba(0,0,0,0.19);
  margin: 20px auto;
  text-align: center;
  padding-bottom: 10px;
  border-radius: 6px;
}

div.polaroid img {
  width: 100%;
  border-radius: 6px 6px 0 0;
}

? Live Output Preview

The image appears inside a white card with a drop shadow, like an old-school polaroid photo, with a caption at the bottom.

Example:

Polaroid Image
Beautiful Nature

✨ Transparent Images (Opacity)

The opacity property controls transparency. Values range from 0 (fully transparent) to 1 (fully opaque).

? View Code Example – Transparent Image
/* Reduce image visibility using opacity */
img.transparent {
  opacity: 0.5;
  width: 300px;
}

? Live Output Preview

With opacity: 0.5;, the image looks faded. This is useful for backgrounds or hover effects.

Example:

Transparent Image

? Tips & Best Practices

  • Always use descriptive alt text for images for better SEO and accessibility.
  • Use border-radius to create softer, friendlier images that match modern UI design.
  • Combine opacity with hover effects to create interactive image transitions.
  • Test your responsive images on multiple screen sizes to ensure they look good everywhere.
  • Keep image file sizes optimized to avoid slow page loads.

? Try It Yourself

  • Create an image gallery using responsive images and thumbnail styling.
  • Experiment with different border-radius values to make rounded and circular images.
  • Add hover shadows to your thumbnails for a clean, modern effect.
  • Use opacity and transition to create a fade-in effect on hover.
  • Build a simple “photo album” page using multiple polaroid-style image cards.