🖼️ CSS Images

Master image styling with opacity, filters, sprites, and responsive techniques

← Back to Content Next Topic: CSS3 Transforms →

🎯 Why Image Styling Matters

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.

CSS Image Capabilities:

  • ✅ Control opacity and transparency
  • ✅ Apply filters (blur, grayscale, brightness)
  • ✅ Create image hover effects
  • ✅ Use CSS sprites for performance
  • ✅ Build responsive image galleries
  • ✅ Optimize images for different screens

📱 Responsive Images

Make images adapt to different screen sizes and containers.

Basic Responsive Image:

/* Make image responsive */
img {
    max-width: 100%;
    height: auto;
}

/* Specific container */
.container img {
    width: 100%;
    height: auto;
    display: block;
}

Object Fit Property:

/* 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%;
}

Object-Fit Visual Examples:

cover

Covers area

contain

Fits within

fill

Stretches

👁️ Image Opacity

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;
}

Interactive Opacity Demo (Hover Me!):

← Hover to see full opacity

Fixed Opacity Examples:

Opacity: 1.0, 0.7, 0.5, 0.3

🎨 CSS Filters

Apply visual effects to images without requiring image editing software.

Common Filters:

/* 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));
}

Combining Multiple Filters:

/* 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);
}

Filter Visual Examples:

Original

Grayscale

Sepia

Blur

Brightness

Invert

Interactive Filter Demo (Hover for Color!):

✨ Image Hover Effects

Create interactive image effects on hover.

Zoom Effect:

/* 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);
}

Grayscale to Color:

img {
    filter: grayscale(1);
    transition: filter 0.3s;
}

img:hover {
    filter: grayscale(0);
}

Overlay with Text:

.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;
}

Tilt Effect:

img {
    transition: transform 0.3s;
}

img:hover {
    transform: rotate(-5deg) scale(1.05);
}

Interactive Hover Effects (Try them!):

Zoom Effect

Tilt Effect

🎮 CSS Sprites

Combine multiple images into one file to reduce HTTP requests and improve performance.

What are Sprites?

CSS sprites are a collection of images combined into a single image file. You use CSS background-position to display specific portions.

Benefits:

  • ✅ Fewer HTTP requests = faster loading
  • ✅ Reduced bandwidth usage
  • ✅ Better for icons and small images
  • ✅ Improved page performance

Using Sprites:

/* 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;
}

Sprite Concept Visualization:

Single Sprite Sheet:

🏠
👤
⚙️
🔍

1 HTTP Request

vs 4 separate requests

🖼️ Image Gallery

Create responsive image galleries with CSS Grid or Flexbox.

Grid Gallery:

/* 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);
}

Masonry-Style Gallery:

/* 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; }
}

Gallery Example:

Hover over images to see the scale effect

⭕ Rounded & Circular Images

/* 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;
}

Shape Examples:

10px, 20px, 50% (circle), 50% with border

💼 Practical Real-World Examples

Example 1: Profile Picture

.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);
}

Example 2: Product Card

.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);
}

Example 3: Hero Background Image

.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);
}

Example 4: Lazy Loading with Placeholder

/* 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; }
}

📝 Test Your Knowledge - MCQ

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?

💪 Practice Questions

  1. Make an image responsive using max-width and height properties.
  2. Create a circular profile picture (150px × 150px) with white border and shadow.
  3. Apply a grayscale filter to an image that becomes color on hover.
  4. Build a responsive image gallery using CSS Grid with 3 columns that adapts to mobile.
  5. Create a zoom hover effect where the image scales to 1.1 when hovered.
  6. Implement a CSS sprite for 4 icons with hover states.
  7. Create a hero section with a fixed background image (parallax effect).

📌 Summary

  • Responsive images: max-width: 100%; height: auto;
  • object-fit: Controls how images fit containers (cover, contain, fill)
  • Opacity: Values from 0 (transparent) to 1 (opaque)
  • Filters: blur(), grayscale(), sepia(), brightness(), contrast(), saturate(), hue-rotate(), invert()
  • Hover effects: Combine transforms, filters, and opacity for interactivity
  • CSS Sprites: Combine multiple images to reduce HTTP requests
  • Circular images: border-radius: 50% on equal width/height
  • Image galleries: Use CSS Grid or Flexbox with responsive columns
  • Performance: Use lazy loading and optimize images for web

🔑 Key Takeaways

📱 Responsive Design

Always use max-width: 100% and height: auto to ensure images scale properly across devices. Use object-fit for precise control over image sizing.

🎨 Creative Effects

CSS filters provide powerful image effects without requiring image editing software. Combine multiple filters for unique styles.

⚡ Performance

CSS sprites reduce HTTP requests and improve page load times for icon-heavy websites. Consider lazy loading for images below the fold.

✨ Interactivity

Hover effects enhance user engagement and provide visual feedback. Use transitions for smooth, professional-looking animations.

← Back to Content Next Topic: CSS3 Transforms →