Master image styling with opacity, filters, sprites, and responsive techniques
Images are crucial visual elements on websites. CSS provides powerful properties to control image appearance, create effects, optimize performance with sprites, and ensure responsive behavior across devices.
Make images adapt to different screen sizes and containers.
/* Make image responsive */
img {
max-width: 100%;
height: auto;
}
/* Specific container */
.container img {
width: 100%;
height: auto;
display: block;
}
/* Control how image fits container */
img {
width: 300px;
height: 200px;
object-fit: cover; /* Covers area, may crop */
object-fit: contain; /* Fits within, may have space */
object-fit: fill; /* Stretches to fill */
object-fit: none; /* Original size */
object-fit: scale-down; /* Smaller of none or contain */
}
/* Object position */
img {
object-fit: cover;
object-position: center; /* top, bottom, left, right, or % */
object-position: top right;
object-position: 25% 75%;
}
Covers area
Fits within
Stretches
Control the transparency of images and create hover effects.
/* Opacity values: 0 (transparent) to 1 (opaque) */
img {
opacity: 1; /* Fully visible (default) */
opacity: 0.7; /* 70% visible */
opacity: 0.5; /* 50% visible */
opacity: 0; /* Fully transparent */
}
/* Hover effect - fade in */
img {
opacity: 0.7;
transition: opacity 0.3s ease;
}
img:hover {
opacity: 1;
}
/* Image overlay effect */
.image-container {
position: relative;
}
.image-container::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
opacity: 0;
transition: opacity 0.3s;
}
.image-container:hover::after {
opacity: 1;
}
← Hover to see full opacity
Fixed Opacity Examples:
Opacity: 1.0, 0.7, 0.5, 0.3
Apply visual effects to images without requiring image editing software.
/* Blur */
img {
filter: blur(5px);
}
/* Brightness (0 = black, 1 = normal, >1 = brighter) */
img {
filter: brightness(0.5); /* Darker */
filter: brightness(1.5); /* Brighter */
}
/* Contrast */
img {
filter: contrast(1.5); /* More contrast */
filter: contrast(0.5); /* Less contrast */
}
/* Grayscale (0 = color, 1 = full grayscale) */
img {
filter: grayscale(1); /* Black & white */
filter: grayscale(0.5); /* 50% gray */
}
/* Sepia (vintage effect) */
img {
filter: sepia(1); /* Full sepia */
filter: sepia(0.5); /* 50% sepia */
}
/* Saturate (color intensity) */
img {
filter: saturate(2); /* More vibrant */
filter: saturate(0); /* No color */
}
/* Hue Rotate */
img {
filter: hue-rotate(90deg); /* Shift colors */
}
/* Invert */
img {
filter: invert(1); /* Negative effect */
}
/* Drop Shadow */
img {
filter: drop-shadow(5px 5px 10px rgba(0,0,0,0.5));
}
/* Multiple filters (space-separated) */
img {
filter: grayscale(1) brightness(1.2) contrast(1.1);
}
/* Instagram-style filters */
.filter-vintage {
filter: sepia(0.5) contrast(1.2) brightness(0.9);
}
.filter-dramatic {
filter: contrast(1.8) saturate(0.8) brightness(0.9);
}
.filter-cool {
filter: saturate(1.5) hue-rotate(-15deg) brightness(1.1);
}
/* Hover effect - grayscale to color */
img {
filter: grayscale(1);
transition: filter 0.3s;
}
img:hover {
filter: grayscale(0);
}
Original
Grayscale
Sepia
Blur
Brightness
Invert
Create interactive image effects on hover.
/* Container for overflow control */
.image-zoom {
overflow: hidden;
border-radius: 10px;
}
.image-zoom img {
transition: transform 0.3s ease;
}
.image-zoom:hover img {
transform: scale(1.1);
}
img {
filter: grayscale(1);
transition: filter 0.3s;
}
img:hover {
filter: grayscale(0);
}
.image-overlay {
position: relative;
overflow: hidden;
}
.image-overlay img {
display: block;
width: 100%;
transition: transform 0.3s;
}
.overlay-text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 1.5rem;
opacity: 0;
transition: opacity 0.3s;
}
.image-overlay:hover img {
transform: scale(1.1);
}
.image-overlay:hover .overlay-text {
opacity: 1;
}
img {
transition: transform 0.3s;
}
img:hover {
transform: rotate(-5deg) scale(1.05);
}
Zoom Effect
Tilt Effect
Combine multiple images into one file to reduce HTTP requests and improve performance.
CSS sprites are a collection of images combined into a single image file. You use CSS background-position to display specific portions.
Benefits:
/* Sprite sheet setup */
.icon {
width: 32px;
height: 32px;
background-image: url('sprite-sheet.png');
background-repeat: no-repeat;
display: inline-block;
}
/* Position to show specific icon */
.icon-home {
background-position: 0 0;
}
.icon-user {
background-position: -32px 0;
}
.icon-settings {
background-position: -64px 0;
}
.icon-search {
background-position: -96px 0;
}
/* Hover effect - second row */
.icon-home:hover {
background-position: 0 -32px;
}
Single Sprite Sheet:
1 HTTP Request
vs 4 separate requests
Create responsive image galleries with CSS Grid or Flexbox.
/* Responsive grid gallery */
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
}
.gallery img {
width: 100%;
height: 300px;
object-fit: cover;
border-radius: 10px;
transition: transform 0.3s;
}
.gallery img:hover {
transform: scale(1.05);
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
/* Column-based masonry */
.masonry {
column-count: 3;
column-gap: 15px;
}
.masonry img {
width: 100%;
margin-bottom: 15px;
border-radius: 10px;
break-inside: avoid;
}
/* Responsive columns */
@media (max-width: 1024px) {
.masonry { column-count: 2; }
}
@media (max-width: 768px) {
.masonry { column-count: 1; }
}
Hover over images to see the scale effect
/* Rounded corners */
img {
border-radius: 10px;
}
/* Circle (perfect for avatars) */
img {
width: 150px;
height: 150px;
border-radius: 50%;
object-fit: cover;
}
/* Bordered circle */
img {
width: 150px;
height: 150px;
border-radius: 50%;
border: 5px solid white;
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
/* Half-rounded (pill shape) */
img {
border-radius: 50px;
}
10px, 20px, 50% (circle), 50% with border
.profile-pic {
width: 150px;
height: 150px;
border-radius: 50%;
object-fit: cover;
border: 4px solid white;
box-shadow: 0 5px 20px rgba(0,0,0,0.2);
transition: transform 0.3s;
}
.profile-pic:hover {
transform: scale(1.1);
}
.product-card {
overflow: hidden;
border-radius: 10px;
}
.product-card img {
width: 100%;
height: 300px;
object-fit: cover;
transition: transform 0.3s;
}
.product-card:hover img {
transform: scale(1.05);
}
.hero {
background-image: url('hero.jpg');
background-size: cover;
background-position: center;
background-attachment: fixed; /* Parallax effect */
min-height: 100vh;
position: relative;
}
/* Optional overlay */
.hero::before {
content: "";
position: absolute;
inset: 0;
background: rgba(0, 0, 0, 0.4);
}
/* Placeholder while loading */
img {
background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
background-size: 200% 100%;
animation: loading 1.5s infinite;
}
@keyframes loading {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
/* Fade in when loaded */
img.loaded {
animation: fadeIn 0.3s;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
Q1: What makes an image responsive?
Q2: Which filter creates a black and white effect?
Q3: How do you make a square image circular?
Q4: What is the purpose of CSS sprites?
Q5: Which object-fit value covers the entire container?
max-width: 100%; height: auto;border-radius: 50% on equal width/heightAlways use max-width: 100% and height: auto to ensure images scale properly across devices. Use object-fit for precise control over image sizing.
CSS filters provide powerful image effects without requiring image editing software. Combine multiple filters for unique styles.
CSS sprites reduce HTTP requests and improve page load times for icon-heavy websites. Consider lazy loading for images below the fold.
Hover effects enhance user engagement and provide visual feedback. Use transitions for smooth, professional-looking animations.