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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the basic image styling demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has a single rule for one image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
.demo-img targets only the image with that class. width: 250px displays the picture 250 pixels wide, even though the original file is 400 pixels wide. Because height is not set, the browser keeps the original proportions, so the picture is not stretched.
border is a shorthand for width, style and color: 3px thick, solid line, blue. The border makes the real edge of the image visible, which helps you see the effect of the width. The } closes the rule.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: just one image.
class="demo-img" connects the image to the CSS rule. src loads a 400 by 300 pixel sample photo, which is then shrunk to 250px by the CSS. alt is the text read by screen readers and shown if the image fails to load, so every image should have one.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the image resizing demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo sets both dimensions of one image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The source photo is 600 pixels wide. width: 300px shows it at half that width.
The source photo is 400 pixels high, so height: 200px is also exactly half. Both dimensions are scaled by the same factor (the ratio stays 3:2), so the image looks natural. If you enter a height with a different ratio, such as 300px, the picture is stretched. To avoid that, set only width and use height: auto.
A blue border shows the exact 300 by 200 box. Note that the border adds to the outside of the size, because images use content-box sizing by default.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
The src file is 600 by 400 pixels. CSS reduces it to 300 by 200 on screen, but the browser still downloads the full file, so very large images can slow a page down.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the border, rounded corners and shadow demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo adds three decorations to one image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The image is displayed 250px wide. The height follows automatically, so the proportions are kept.
A 5px solid blue border frames the picture. A thicker border than in earlier examples makes the rounded corners easy to see.
border-radius: 20px rounds each corner with a 20px radius. It rounds the image and its border together. A larger value gives rounder corners, and 50% on a square image makes a circle.
box-shadow values: 0 horizontal offset, 5px vertical offset (shadow falls below), 12px blur, then the color. rgba(0,0,0,.3) is black at 30% opacity, which gives a soft shadow instead of a harsh one.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
A 500 by 360 sample photo. All the visual effects come from the CSS, and the image file itself is unchanged.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the common image properties demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo uses a separate class for each effect. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A circle needs a square box. Width and height are both 150px, and the source photo (picsum.photos/150) is also square, so nothing is distorted.
border-radius: 50% rounds each corner by half of the side, which turns a square into a circle. This is the usual style for profile pictures. The border follows the round shape.
0 4px 10px gray means no horizontal offset, 4px down, 10px of blur, in gray. Named colors are fine here, though rgba gives more control over how strong the shadow is.
opacity: .6 makes the image 60% visible, so the page background shows through. Values go from 0 (invisible) to 1 (fully visible).
:hover applies only while the mouse is over the image. Setting opacity: 1 restores full strength, so the image appears to light up. Move the mouse over the third image to see it.
A group selector gives the three images the same margin. The margin order is top, right, bottom, left: no top margin, 10px right, 10px bottom, no left. Images are inline, so they sit on the same line and the right margin separates them.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: three images, each with its own class.
Uses demo-circle. The square 150 by 150 source makes a true circle.
Uses demo-shadow. The number 151 in the URL only asks for a different sample photo.
Uses demo-fade, which starts semi-transparent and becomes solid on hover.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the image gallery demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. background: #f5f9ff gives the whole preview a very light blue color, so the white photo borders stand out.
display: flex places the images in one row. gap: 20px puts exactly 20px between them, so you do not need margins on every image.
flex-wrap: wrap lets images drop to a new line when the window is too narrow, instead of overflowing. margin-top: 30px separates the gallery from the heading.
Every photo is 220px wide, and the height follows the image ratio. border-radius: 12px rounds the corners.
A white border gives a print-like frame. The soft shadow lifts the photo off the light blue background.
transition: .3s makes any property that changes, such as size or border color, animate over 0.3 seconds instead of jumping instantly. It is written on the normal state so that the animation also works when the mouse leaves.
On hover, transform: scale(1.08) enlarges the photo by 8% without moving the other images, and border-color changes the white frame to blue. Both changes are animated by the transition above.
</style> closes the CSS.
</head> closes the head. The visible content follows.
<body> starts the visible content: a heading and a gallery of three images.
A heading above the gallery. It is a normal element, so it is outside the flex container.
This div is the flex container. The three images are its direct children, so they are laid out in a row.
All three images use the same demo-photo class, so one rule styles them all. The ?1, ?2 and ?3 at the end of the URLs only make the sample service return different pictures. Each has its own alt text.
</div> closes the gallery.
</body> closes the body.
</html> ends the document.
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>
<!DOCTYPE html> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the common mistake demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
A CSS comment, ignored by the browser and written for the reader. It labels the next rule as the wrong version.
250 has no unit. In CSS a length other than 0 needs one, such as px, % or em. Because this page has a DOCTYPE, the browser treats width: 250 as an error and ignores it, so the image shows at its natural 400px width instead of 250px. CSS gives no error message, which makes this mistake easy to miss.
A red border marks the incorrect example. The border is valid, so it still works. Only the broken declaration is ignored.
Another comment, labeling the correct version.
250px includes the unit, so the browser applies it and the image is shrunk to 250px wide.
A green border marks the correct example, so the two images are easy to tell apart.
font-weight: bold makes the captions above each image stand out.
</style> closes the CSS.
</head> closes the head. The visible content comes next.
<body> starts the visible content: two captions, each followed by an image.
Tells the learner that the image below uses width:250.
Uses demo-wrong. Its width is ignored, so it appears larger than the correct one, at the file’s natural size.
Tells the learner that the image below uses width:250px.
Uses demo-correct. It is the same photo, but 250px wide. Comparing the two shows the effect of the missing unit.
</body> closes the body.
</html> ends the document.
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.