Academic Block

CSS Image Styling Get Certified

What is CSS Image Styling?

CSS Image Styling is the process of changing the appearance of images using CSS. You can control an image’s width, height, border, border radius, shadow, opacity, alignment, and hover effects. Styling images makes websites more attractive, responsive, and professional.

Why Use CSS Image Styling?

CSS allows you to improve the appearance of images without editing the image itself. You can create responsive images, rounded corners, circular profile pictures, image galleries, hover effects, and modern card layouts using only CSS.

Basic Image Styling Syntax

What this code does: Sets an image to a fixed width of 250px with a blue border so you can see its size clearly.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    width: 250px;
    border: 3px solid #007BFF;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/400/300" alt="Sample Image">
</body>
</html>

Step 1: Resize an Image

Use the width and height properties to control the size of an image. For responsive images, set the width to 100% and the height to auto.

What this code does: Forces a larger photo to display at exactly 300px by 200px using width and height.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    width: 300px;
    height: 200px;
    border: 3px solid #007BFF;
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/600/400" alt="Landscape">
</body>
</html>

Step 2: Add Borders and Rounded Corners

CSS lets you add borders, rounded corners, and shadows to make images more attractive.

What this code does: Adds a thick blue border, rounded corners with border-radius, and a soft drop shadow to the image.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-img {
    width: 250px;
    border: 5px solid #007BFF;
    border-radius: 20px;
    box-shadow: 0 5px 12px rgba(0,0,0,.3);
}
</style>
</head>
<body>
<img class="demo-img" src="https://picsum.photos/500/360" alt="Nature">
</body>
</html>

Common Image Styling Properties

The following example demonstrates some of the most commonly used CSS properties for styling images.

What this code does: Shows three effects side by side: a circular image, an image with a shadow, and a faded image that becomes fully visible on hover.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-circle {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    border: 3px solid #007BFF;
}
.demo-shadow {
    width: 150px;
    box-shadow: 0 4px 10px gray;
}
.demo-fade {
    width: 150px;
    opacity: .6;
}
.demo-fade:hover {
    opacity: 1;
}
.demo-circle, .demo-shadow, .demo-fade {
    margin: 0 10px 10px 0;
}
</style>
</head>
<body>
<img class="demo-circle" src="https://picsum.photos/150" alt="Circle">
<img class="demo-shadow" src="https://picsum.photos/151" alt="Shadow">
<img class="demo-fade" src="https://picsum.photos/152" alt="Opacity">
</body>
</html>

Complete CSS Image Styling Example

What this code does: Builds a centred image gallery with rounded, shadowed photos that grow slightly and get a blue border when you hover over them.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    background: #f5f9ff;
}
.demo-gallery {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    margin-top: 30px;
}
.demo-photo {
    width: 220px;
    border-radius: 12px;
    border: 3px solid white;
    box-shadow: 0 4px 12px rgba(0,0,0,.25);
    transition: .3s;
}
.demo-photo:hover {
    transform: scale(1.08);
    border-color: #007BFF;
}
</style>
</head>
<body>
<h2>Image Gallery</h2>
<div class="demo-gallery">
<img class="demo-photo" src="https://picsum.photos/220/160?1" alt="Image 1">
<img class="demo-photo" src="https://picsum.photos/220/160?2" alt="Image 2">
<img class="demo-photo" src="https://picsum.photos/220/160?3" alt="Image 3">
</div>
</body>
</html>

Advantages of CSS Image Styling

Advantage
Benefit
Description
Responsive Images
Better Viewing
Images adjust to different screen sizes.
Modern Design
Professional Look
Borders, shadows, and hover effects improve appearance.
Better User Experience
Interactive Website
Hover effects make webpages more engaging.
Easy Customization
Flexible Styling
Images can be styled without editing the original file.
Reusable Code
Easy Maintenance
The same styles can be applied to multiple images.

Common Mistakes

Beginners often forget to specify image dimensions, stretch images by using incorrect width and height values, forget the alt attribute, or apply fixed image sizes that don’t work well on smaller screens.

What this code does: Compares a width written without a unit (250, ignored by the browser) with the correct 250px, so you can see the difference in size.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
/* Incorrect: missing unit, so the browser ignores it */
.demo-wrong {
    width: 250;
    border: 3px solid #dc3545;
}
/* Correct: includes the px unit */
.demo-correct {
    width: 250px;
    border: 3px solid #28a745;
}
.demo-label {
    font-weight: bold;
}
</style>
</head>
<body>
<p class="demo-label">Incorrect (width:250)</p>
<img class="demo-wrong" src="https://picsum.photos/400/300" alt="Sample">
<p class="demo-label">Correct (width:250px)</p>
<img class="demo-correct" src="https://picsum.photos/400/300" alt="Sample">
</body>
</html>

Best Practices

  • Always include the alt attribute for every image.
  • Use responsive images with max-width:100% and height:auto.
  • Compress images to improve page loading speed.
  • Use border-radius and box-shadow sparingly for a clean design.
  • Apply hover effects only when they improve user experience.
  • Use consistent image sizes in galleries and cards.
  • Choose high-quality images with appropriate dimensions.
  • Test image layouts on desktop, tablet, and mobile devices.

Web Resources on CSS Image Styling

1. MDN Web Docs : CSS images
2. MDN Web Docs : background-image CSS property
3. Florida International University : Using CSS classes to style images on your website
4. Dev.to : CSS Styling Images, From Thumbnails to Polaroids, A Complete Guide

๐Ÿงช Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

๐Ÿ“ CSS Code
๐Ÿ‘๏ธ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.