Why Does an Image Without Width and Height Shift the Page?

Image missing width height layout shift bugs happen when the browser cannot reserve image space before the file loads.

CSS Layout Shift Fix

Why Does an Image Without Width and Height Shift the Page?

An image without width and height can shift the page because the browser does not know the image’s final footprint during the first layout pass.

The image may look normal after it loads, but the damage happens earlier. The text, card grid, buttons, or sidebar are placed before the image size is known. Then the image arrives, takes height, and pushes everything below it.

This is the image missing width height layout shift problem. It is not mainly about image quality or cropping. It is about whether the layout has a stable box before the network finishes downloading the media file.

  • width
  • height
  • layout shift
  • CLS

Test before the image arrives

Throttle the network, reload the page, and look at the empty area where the image should appear. If there is no stable box before the image file downloads, the image can shift the page.

Related: Try this in the FrontFixer Live Inspector.

Open Live Inspector

What the bug looks like

Content jumps when an image appears, even though the final image looks fine.

Why it happens

The browser did not know the image ratio before the first layout.

What usually fixes it

Add image dimensions or reserve the same ratio with CSS.

Why missing image dimensions create layout shift

The browser lays out a page before every resource is downloaded. Text can be measured immediately. CSS can be applied. But an image without width and height does not tell the browser how much space it should reserve.

When the image file finally arrives, the browser learns its natural dimensions and updates the layout. That update can move paragraphs, buttons, cards, ads, related posts, and anything below the image. The result is a visual jump.

The fix is to separate image loading from image sizing. The file can load later, but the image box should exist earlier. That is why modern responsive image systems still include dimensions, ratio wrappers, or stable media shells.

Loading is not layoutThe browser needs a size before it needs the final pixels.
Dimensions create ratioWidth and height let the browser calculate space.
CSS can reserve spaceAn aspect-ratio wrapper can also protect the layout.
Better mindsetReserve the box first, then load the image.
Error 1

The image tag has no width or height attributes

This is the classic image missing width height layout shift bug. The image eventually loads correctly, but the browser has no early ratio to reserve. The page starts compact, then expands when the image appears.

Broken code

No dimensions
<img
  src="hero.jpg"
  alt="Feature preview">

Broken visual result

Image claims space late
image height appears late
shift
The layout has to change after the image dimensions are discovered.

Correct code

Dimensions included
<img
  src="hero.jpg"
  width="1200"
  height="675"
  alt="Feature preview">

Fixed visual result

Ratio reserved
reserved 16/9 media space
stable
The browser calculates the image ratio before the image finishes loading.
Error 2

The CSS wrapper reserves the wrong shape

Sometimes the image has dimensions, but the wrapper forces a different shape. That can still create layout movement because the reserved space and the real component design do not match.

Broken code

Wrong wrapper height
.media {
  height: 80px;
}

.media img {
  width: 100%;
  height: auto;
}

Broken visual result

Wrapper too short
reserved short box
actual image needs more height
The wrapper reserves one height, but the final image needs another.

Correct code

Wrapper owns ratio
.media {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Fixed visual result

Shape matches design
reserved wrapper ratio
final image fills same box
The component reserves the exact shape it will use after the image loads.
Error 3

Card images load with different natural heights

A card grid can shift when thumbnails do not share a controlled media box. One card image arrives tall, another arrives short, and the entire row changes after the first render.

Broken code

Natural heights control cards
.card img {
  max-width: 100%;
  height: auto;
}

Broken visual result

Cards jump unevenly
shortCard A
tallCard B shifts row
lateCard C
Natural image heights take control of the card grid after loading.

Correct code

Card media shell
.card-media {
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.card-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Fixed visual result

Cards stay aligned
4/3Card A
4/3Card B
4/3Card C
Each card reserves the same media slot before its image paints.
Error 4

A carousel or gallery measures slides before images load

Sliders and galleries often calculate height before images finish loading. If the images have no dimensions, the carousel may start short and then jump, crop, or resize when the slides finally reveal their real height.

Broken code

Carousel waits for images
.slide img {
  width: 100%;
  height: auto;
}

Broken visual result

Slide height changes
The slider changes height after images reveal their natural dimensions.

Correct code

Slides reserve media ratio
.slide-media {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.slide-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Fixed visual result

Slides stay predictable
Every slide has a stable media frame before the image appears.
Premium patterns

Three production-minded image dimension patterns

Premium image systems do not rely on the image file arriving in time. They define the visual box first, then let the image fill that box when it is ready.

Premium code example 1

Article hero dimensions
<figure class="hero-media">
  <img
    src="hero.jpg"
    width="1200"
    height="675"
    alt="Article preview">
</figure>

.hero-media img {
  width: 100%;
  height: auto;
  display: block;
}

Premium visual result 1

Article rhythm stays locked
premium
Hero image owns its footprint

The headline, media, and paragraph spacing stay stable while the image loads.

1200 × 675 reserved
The HTML dimensions create the ratio early.
Pattern 1 is ideal for article hero images, landing sections, and tutorial screenshots.

Premium code example 2

Product card media shell
.product-media {
  aspect-ratio: 4 / 5;
  overflow: hidden;
}

.product-media img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Premium visual result 2

Product cards stay equal
premium
Ecommerce images reserve product space

All product cards share a predictable media shell before images load.

4/5Product A
4/5Product B
4/5Product C
Pattern 2 is ideal for ecommerce grids, portfolio cards, recipe cards, and directory listings.

Premium code example 3

Feed thumbnail dimensions
.feed-row {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  gap: 16px;
}

.feed-thumb {
  aspect-ratio: 1 / 1;
  overflow: hidden;
}

Premium visual result 3

Feed row does not jump
premium
Search results reserve thumbnails

The row knows the thumbnail size before the image file arrives.

1/1 thumb
Pattern 3 is ideal for feeds, related posts, search results, and compact media rows.

Fast practical rule

Every important image should have a stable footprint before it loads. Use HTML width and height when you know the source dimensions, and use a CSS aspect-ratio wrapper when the component needs a controlled design shape.

Debug checklist

  • Inspect the image tag and check whether width and height are missing.
  • Throttle the network and reload the page to see the empty image slot.
  • Check whether the browser reserves space before the image file downloads.
  • Use display:block to avoid inline image spacing surprises.
  • Use a ratio wrapper when the design needs a controlled crop or fixed visual shape.
  • Check product cards, search results, galleries, and article hero images first.
  • Compare the reserved shape with the final rendered image shape.
  • Do not depend on fast internet to hide layout instability.
Best first moveAdd real image dimensions and reload with network throttling.
Most common causeThe image has no early ratio, so the page lays out without it.
Most sneaky causeA CSS wrapper reserves the wrong shape for the final image.
Better mindsetImages can load later, but their layout boxes should not.

What width and height actually do

The width and height attributes do not force the image to stay that exact pixel size on every screen. When the CSS says max-width:100% or width:100%, the image can still scale responsively.

Their real job is to give the browser the image’s ratio early. A 1200 by 675 image tells the browser to reserve a 16:9 space even before the pixels finish downloading. That early ratio is what prevents the page from jumping.

This is why a responsive image can have fixed numeric attributes and still behave responsively. The attributes describe the source ratio. The CSS controls the rendered size.

Why this is different from lazy loading shift

Lazy loading image layout shift is about delayed image loading. This fix is narrower: it focuses on the missing dimension data itself. An image can shift the page even without lazy loading if the browser cannot reserve its size during the first layout.

That prevents cannibalization between both fixes. This article answers the dimension problem. The lazy loading article answers the delayed loading strategy problem.

When dimensions are not enough

Width and height attributes give the browser a natural ratio, but the surrounding layout still matters. A fixed-height wrapper, a carousel script, a grid card, or an art-directed mobile image can still override the expected result.

When that happens, keep the image dimensions and fix the component shell. The strongest production pattern is not “HTML dimensions or CSS ratio.” It is often both: image dimensions for the browser, and a stable wrapper for the design system.

Final takeaway

Image missing width height layout shift happens because the browser cannot reserve the image’s final space during the first layout pass. The file may load correctly, but the page still jumps if the image box was not known early.

Add width and height when possible. Use aspect-ratio wrappers when the component needs a designed media shell. Keep lazy loading, responsive images, and image grids stable by making the layout footprint predictable before the image appears.

Want more fixes like this?

Browse more CSS image, layout shift, aspect-ratio, responsive media, and card layout debugging guides in the FrontFixer library.

Why Is My Image Cropped Wrong on Mobile?

Image cropped wrong on mobile problems usually happen when a desktop image is forced into a mobile container with object-fit:cover, a fixed height, the wrong object-position, or a background image with the wrong focal point.

Mobile Image Crop Fix

Why is my image cropped wrong on mobile?

An image cropped wrong on mobile can cut off a face, product, logo, headline area, or important detail. The image is not necessarily broken. The browser is usually doing exactly what the CSS asked: filling a narrow mobile box with cover. The fix is to control the image ratio, focal point, object position, and mobile crop rules instead of letting the browser guess.

  • object-fit
  • object-position
  • aspect-ratio
  • background-position

What the bug looks like

The subject disappears, a face is cut off, a product is too zoomed in, or only the wrong side of the image is visible.

Why it happens

The mobile container has a different shape than the image, so the browser crops the image to fill the box.

What usually fixes it

Use a better aspect ratio, adjust object-position, avoid tiny fixed heights, or provide a mobile-specific crop.

Error 1

object-fit:cover is cropping the wrong part

object-fit:cover is not bad. It is often the right choice for cards and hero images. But on mobile, a narrow container can crop the subject if the focal point stays in the wrong place.

Broken code

Default center crop
.hero-image {
  width: 100%;
  height: 220px;
  object-fit: cover;
  object-position: center center;
}

Broken visual result

Important area is cut
bad crop
Mobile hero

The crop fills the box but cuts away the focal point.

Cover fills the container, but the default center crop does not protect the subject.

Correct code

Control focal point
.hero-image {
  width: 100%;
  height: 220px;
  object-fit: cover;
  object-position: center 28%;
}

Fixed visual result

Subject stays visible
focal point
Mobile hero

The crop still fills the box, but the important area stays in view.

object-position tells the browser which part of the image should stay visible.
Error 2

The mobile image container is too short

A short fixed-height container makes the crop more aggressive. If the image has important vertical detail, a tiny mobile height can remove the top, bottom, or center of the subject.

Broken code

Fixed short height
@media (max-width: 640px) {
  .image-wrap {
    height: 120px;
    overflow: hidden;
  }

  .image-wrap img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

Broken visual result

Container forces harsh crop
too short
Article card

The image box is too short for the visual content.

A short mobile container can crop too much even when the image width is responsive.

Correct code

Use aspect ratio
.image-wrap {
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.image-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Fixed visual result

Crop has room
aspect ratio
Article card

The image keeps a consistent shape without being squeezed flat.

aspect-ratio is usually safer than a tiny fixed height for responsive images.
Error 3

background-size:cover uses the wrong background position

Background images have the same crop problem as normal images. If you use background-size:cover without adjusting background-position, mobile can show the wrong part of the image.

Broken code

Background centered blindly
.hero {
  background-image: url(hero.jpg);
  background-size: cover;
  background-position: center;
}

Broken visual result

Wrong part of background
wrong focus
Background hero

The background fills the section, but the subject is not where mobile needs it.

Background cover needs a mobile-aware focal point, not just center center.

Correct code

Mobile background focal point
.hero {
  background-image: url(hero.jpg);
  background-size: cover;
  background-position: center top;
}

@media (max-width: 640px) {
  .hero {
    background-position: 35% center;
  }
}

Fixed visual result

Background focuses correctly
positioned
Background hero

The mobile background position keeps the important part visible.

For background images, background-position is your focal point control.
Error 4

You need a mobile-specific crop, not just CSS positioning

Sometimes the desktop image is simply the wrong shape for mobile. If the important content is spread across a wide image, no amount of object-position will make every detail fit in a narrow crop.

Broken code

One image for every screen
<img
  class="hero-image"
  src="desktop-wide-image.jpg"
  alt="Product preview"
>

Broken visual result

Desktop crop does not translate
wrong source
Product image

The desktop composition is too wide for mobile.

One desktop image may not be enough when mobile needs a different composition.

Correct code

Use picture source
<picture>
  <source
    media="(max-width: 640px)"
    srcset="mobile-crop.jpg"
  >
  <img
    class="hero-image"
    src="desktop-wide-image.jpg"
    alt="Product preview"
  >
</picture>

Fixed visual result

Mobile gets the right crop
mobile source
Product image

The mobile version is composed for the mobile container.

<picture> lets you serve a mobile crop when CSS alone cannot protect the subject.
Premium pattern

A production-minded responsive image crop pattern

A reliable responsive image pattern controls the container ratio, image fitting behavior, focal point, and optional mobile source. It does not depend on one desktop crop magically working everywhere.

Premium code

Safe responsive image crop
<picture class="media">
  <source
    media="(max-width: 640px)"
    srcset="image-mobile.jpg"
  >
  <img
    class="media__image"
    src="image-desktop.jpg"
    alt="Descriptive image alt text"
  >
</picture>
.media {
  display: block;
  overflow: hidden;
  border-radius: 18px;
  aspect-ratio: 16 / 9;
}

.media__image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: var(--image-focus, center);
}

@media (max-width: 640px) {
  .media {
    aspect-ratio: 4 / 3;
  }

  .media__image {
    object-position: var(--image-focus-mobile, center 30%);
  }
}

Premium visual result

Responsive crop with focal control
premium
Premium media block

The crop changes safely on mobile without losing the important subject.

Premium image CSS does not avoid cropping forever. It makes the crop intentional.

Fast practical rule

If an image is cropped wrong on mobile, do not remove object-fit:cover immediately. First adjust the container ratio and object-position. If the desktop composition still fails, use a mobile-specific crop with <picture>.

Debug checklist

  • Inspect the image container height, width, and aspect ratio on mobile.
  • Check whether the image uses object-fit:cover or a background with background-size:cover.
  • Adjust object-position to protect the image’s focal point.
  • For background images, adjust background-position at mobile breakpoints.
  • Replace tiny fixed heights with aspect-ratio where possible.
  • Use object-fit:contain only when showing the full image matters more than filling the box.
  • Use <picture> when mobile needs a different crop or composition.
  • Test real images, not only placeholder rectangles, because focal points change the result.
Best first move Change object-position in DevTools and see whether the important subject comes back.
Most common cause A desktop image is being forced into a narrow mobile container with cover.
Most sneaky cause A background image has the right size but the wrong mobile background position.
Better mindset Cropping is not always a bug. Uncontrolled cropping is the bug.

Final takeaway

An image cropped wrong on mobile is usually a responsive composition problem. The image is being forced into a mobile shape, and the browser has to decide what to cut.

Control the crop intentionally. Use a safer aspect ratio, choose the right object-position or background-position, and use a mobile-specific crop when the desktop image cannot work on a narrow screen.

Want more fixes like this?

Browse more image, mobile, and responsive CSS debugging guides in the FrontFixer library.

Why Is My Image Leaving Empty Space Around It?

Image leaving empty space around it problems usually happen because the image is inline, the container has padding, the image aspect ratio does not match the box, or default spacing is coming from margins and line-height.

CSS Image Layout Fix

Why is my image leaving empty space around it?

An image can look like it has mysterious blank space around it: a small gap under the image, extra padding inside a card, empty space at the top, or unused space because the image does not fill its container. The real cause is usually not the image itself. It is how the image is displayed, how the container is sized, or how the aspect ratio is being handled.

  • Image gap
  • display block
  • object-fit
  • aspect-ratio

What the bug looks like

There is a blank strip below the image, empty bands around it, or a card image that refuses to fill the visible frame.

Why it happens

The image display mode, wrapper spacing, aspect ratio, or fitting rule does not match the visual layout you expect.

What usually fixes it

Use display:block, remove unwanted wrapper spacing, set a clear aspect ratio, and use object-fit intentionally.

Error 1

The image is inline and leaves a baseline gap

Images are inline elements by default. Inline elements align with the text baseline, leaving room for letters that drop below the line. That is why a small gap can appear under an image.

Broken code

Inline image
.card img {
  width: 100%;
}

Broken visual result

Baseline gap
baseline gap
The image behaves like inline text, so the browser leaves space under it.

Correct code

Block image
.card img {
  display: block;
  width: 100%;
  height: auto;
}

Fixed visual result

Gap removed
A block image no longer reserves baseline space below itself.
Error 2

The container padding is creating the empty space

Sometimes the image is not the problem. The parent card has padding, so the image cannot touch the edge of the card even when it is set to 100% width.

Broken code

Padded wrapper
.card {
  padding: 24px;
}

.card img {
  width: 100%;
}

Broken visual result

Space comes from parent
padding space
The image is respecting the padded content area, not the outer card edge.

Correct code

Separate media and content
.card-media img {
  display: block;
  width: 100%;
  height: auto;
}

.card-content {
  padding: 24px;
}

Fixed visual result

Media fills the card top

The image and content now have separate spacing rules.

Keep image spacing and text spacing separate when the image should touch the card edge.
Error 3

The image aspect ratio does not match the container

If a container has a fixed height but the image has a different shape, the image may leave empty space or look visually disconnected unless you choose how it should fit.

Broken code

Unclear fitting
.image-box {
  height: 190px;
}

.image-box img {
  width: 100%;
  height: auto;
}

Broken visual result

Unused container space
empty area
The image sizing rule does not explain how to fill a fixed-height frame.

Correct code

Object-fit cover
.image-box {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.image-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Fixed visual result

Frame is filled
The image now has a clear frame and a clear fitting strategy.
Error 4

Margins on the image or figure create extra spacing

Images often live inside figure, card, or editor-generated wrappers. A margin on the image, figure, or caption can look like mysterious empty space unless you inspect the wrapper.

Broken code

Default margin
figure {
  margin: 1em 40px;
}

figure img {
  width: 100%;
}

Broken visual result

Wrapper creates space
The empty space is coming from wrapper spacing, not from the image file.

Correct code

Reset wrapper spacing
.card figure {
  margin: 0;
}

.card figure img {
  display: block;
  width: 100%;
  height: auto;
}

Fixed visual result

Wrapper no longer adds space
Reset wrapper margins only where the design needs the image to sit flush.
Premium pattern

A production-minded image card pattern

A stronger image setup separates the media area from the content area, controls aspect ratio, removes baseline gaps, and uses object-fit intentionally.

Premium code

Controlled media frame
.card {
  overflow: hidden;
  border-radius: 24px;
}

.card-media {
  aspect-ratio: 16 / 9;
  overflow: hidden;
}

.card-media img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.card-content {
  padding: 24px;
}

Premium visual result

No mystery spacing

The image has a dedicated media frame, and the text has its own content spacing.

Premium image layouts do not guess. They define display mode, aspect ratio, fitting, and content spacing.

Fast practical rule

If an image leaves empty space, inspect the image and the wrapper separately. Check display, margins, padding, aspect ratio, fixed heights, and object-fit. The blank space usually belongs to one of those, not to the image itself.

Debug checklist

  • Set important card images to display:block.
  • Check whether the parent card or figure has padding or margins.
  • Inspect whether the empty space is inside the image box or outside it.
  • Use aspect-ratio when the image frame should keep a consistent shape.
  • Use object-fit:cover when the frame should be filled.
  • Use object-fit:contain only when showing the full image matters more than filling the frame.
  • Separate image/media spacing from card content spacing.
  • Do not hide spacing with random negative margins until you know where it comes from.
Best first move Inspect the image and parent wrapper to see whether the space comes from display, padding, margin, or height.
Most common cause The image is inline and leaves a baseline gap underneath.
Most visual cause The image aspect ratio does not match the container frame.
Better mindset Image layout needs a media frame, not just width:100%.

Final takeaway

Image leaving empty space around it is usually not mysterious. The image may be inline, the wrapper may have padding or margin, the aspect ratio may not match, or the fitting rule may preserve space instead of filling the frame.

Start by separating the image from its container in DevTools. Once you know whether the gap belongs to the image, wrapper, or frame, the fix becomes simple and much cleaner.

Want more fixes like this?

Browse more CSS image and layout debugging guides in the FrontFixer library.