Note details

Why there's a white space under your image tag

BY u6dwl
August 25, 2025
Public
Private
8974 views

Summary of Video on Image Tag Issues and Fixes

Introduction

  • The video addresses common issue with image tags in webpages such as unexpected spaces, images stretching, and unequal column widths.

Key Issues Explored:

  1. Unexpected Space Under Images:

    • Caused by the default inline display of images, allowing for space for descender characters (like "g", "y") in text.
    • Space size depends on the font size of the parent element.
  2. Images Appear Larger than Intended:

    • In situations like Flexbox and Grid layouts.
    • Images clamped to their natural dimensions are larger than the intended size.
  3. Stretched Images in Flex Layouts:

    • Flexbox stretches children to be the same height, which can distort images with different aspect ratios.

Solutions to Address Issues:

  1. Handling Space Under Images:

    • Set images to display: block to eliminate inline element behavior.
    • Alternatively, use CSS styles like display: grid or display: flex on the parent, or set the parent’s font size to zero.
  2. Controlling Image Size:

    • Explicitly set width and height attributes below the image's natural size to avoid clamping in Flex/Grid layouts.
  3. Compatibility with Responsive Layouts:

    • Use CSS to set image width to 100% and height to auto.
    • Avoid using HTML width and height attributes for responsive adjustments, use CSS for aspect ratio management.
  4. Preventing Image Stretch in Flex Layout:

    • Change align-items in the Flex parent to properties like center, start, or end.
    • Use object-fit: cover to maintain image aspect ratio while fitting the desired height.

Further Advice:

  • The video suggests using global styles for images for consistency and avoidance of common issues.
  • For responsive designs, CSS adjustments provide better control over image presentation across different viewports.

Conclusion:

  • The video provides comprehensive insights into why image tags behave unexpectedly in HTML/CSS layouts and proposes practical solutions to manage these quirks.