#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS3 Images

Styling images with CSS means controlling how pictures size, crop, round and blend into your layout. An <img> arrives with its own natural width and height, which rarely fits your design. A handful of CSS properties make images responsive, keep their proportions, crop them neatly into fixed boxes, and apply effects like rounded corners or filters.

Making images fit their container

The first rule almost every stylesheet includes is:

img {
  max-width: 100%;
  height: auto;
  display: block;
}

max-width: 100% stops an image from ever being wider than its parent, so a large photo shrinks on a phone instead of overflowing. height: auto keeps the aspect ratio when the width shrinks. Using max-width rather than width means small images, such as icons, are not stretched up to fill the container.

Adding width and height attributes in the HTML is still a good idea. Browsers use them to reserve the right amount of space before the image loads, which stops the page from jumping as pictures appear. The CSS above then overrides the actual display size.

Cropping without distortion using object-fit

Sometimes every thumbnail in a grid must be exactly the same size, but your photos have different shapes. Setting both a fixed width and height would squash them. object-fit controls how the picture fills its box:

ValueEffect
fillDefault. Stretches to fill, ignoring proportions.
containFits the whole image inside, leaving empty space if shapes differ.
coverFills the box completely and crops the overflow.
noneKeeps the natural size, cropping if needed.
scale-downUses none or contain, whichever is smaller.

object-position chooses which part stays visible when cropping, for example object-position: top to keep faces in portrait photos.

.gallery img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 8px;
  transition: filter 0.2s, transform 0.2s;
}

.gallery img:hover {
  filter: brightness(1.1);
  transform: scale(1.02);
}

.logo-grey {
  filter: grayscale(100%);
  opacity: 0.8;
}

The aspect-ratio property gives each image a consistent shape based on its width, so you do not need to calculate heights for every screen size.

Effects: rounding, shadows and filters

border-radius rounds corners, and 50% on a square image makes a circle. box-shadow lifts an image off the page. The filter property applies effects such as grayscale(), blur(), brightness(), contrast() and sepia(), and several can be combined in one declaration. For decorative pictures, a CSS background-image with background-size: cover works similarly, but use a real <img> with alt text when the image carries meaning.

The mystery gap under images

By default, <img> is an inline element and sits on the text baseline. The browser leaves room below the baseline for letters like "g" and "y", which shows up as a few pixels of unexplained space under the image inside a card or link. Setting display: block on the image, or vertical-align: middle, removes it. If you see the gap only in some places, check whether those images are inside inline wrappers.

Key points

  • max-width: 100%; height: auto makes images responsive.
  • object-fit: cover plus aspect-ratio creates uniform thumbnails without squashing.
  • Use display: block to remove the baseline gap.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course