{"data":{"slug":"github-thedaviddias-front-end-checklist-skills-dimensions-885321e6cdc9","title":"Prevent Cumulative Layout Shift by Setting Image Dimensions","summary":"Set explicit width and height on images to prevent Cumulative Layout Shift (CLS). This ensures that images reserve space in the layout before they load, reducing layout shifts and improving user experience. The deliverable includes before and after HTML for each fixed image.","url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-dimensions-885321e6cdc9","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-thedaviddias-front-end-checklist-skills-dimensions-885321e6cdc9","markdown_url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-dimensions-885321e6cdc9.md","download_url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-dimensions-885321e6cdc9\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before applying the fix, verify the image's intrinsic pixel dimensions. For dynamically sized images, calculate the aspect ratio and use the CSS aspect-ratio property as a fallback. Skip purely decorative micro assets when the snippet already shows stable sizing and there is no evidence of layout shift risk.","instructions":"---\nname: dimensions\ndescription: \"Use when reviewing image assets, markup, and CDN or build transforms related to Set explicit width and height on images. Check encoded size, rendered size, loading strategy, and above-the-fold impact together. Separate real CLS risks from tiny decorative SVGs or ornament-only assets that are unlikely to move surrounding content.\"\nmetadata:\n  category: images\n  priority: high\n  difficulty: beginner\n  estimatedTime: \"10\"\n  source: frontendchecklist.io\n  url: https:\/\/frontendchecklist.io\/rules\/images\/dimensions\n---\n\n# Set explicit width and height on images\n\nImages without explicit dimensions are the most common cause of Cumulative Layout Shift (CLS), a Core Web Vitals metric. When images load and cause layout jumps, users accidentally click the wrong element\u2014a frustrating experience that Google penalises in search rankings. A CLS score above 0.1 is considered 'needs improvement'; above 0.25 is 'poor'.\n\n## Quick Reference\n\n- Reserve findings for meaningful content images; decorative micro-SVGs or ornament-only assets are low-priority exceptions when CSS already reserves stable space\n- Example low-risk exception: `<img src=\"\/divider.svg\" alt=\"\" aria-hidden=\"true\">` should not be flagged on its own without evidence of visible layout shift\n- Always set `width` and `height` attributes on `<img>` matching the image's intrinsic dimensions\n- Pair with `height: auto` in CSS to keep the image fluid while preserving the aspect ratio\n- Missing dimensions are the leading cause of Cumulative Layout Shift (CLS) from images\n- Browsers derive `aspect-ratio` from these attributes\u2014do not set arbitrary placeholder values\n\n## Check\n\nScan all <img> elements in the codebase. Identify any that are missing either the width or height attribute. Also check <source> elements inside <picture>\u2014dimensions should be on the <img> fallback. Report each image missing dimensions with its file path and line number. Note: SVG icons under 32px, tiny decorative SVG ornaments with stable CSS sizing, and images loaded as CSS backgrounds are exempt.\n\n## Fix\n\nFor each <img> missing width\/height: 1) Add the intrinsic pixel dimensions as attributes (e.g., width=\"800\" height=\"450\"). 2) Use CSS max-width: 100% and height: auto on the element to keep it responsive. 3) The attribute values must match the image's intrinsic aspect ratio\u2014do not set arbitrary numbers. 4) For dynamically sized images, calculate the aspect ratio and use the CSS aspect-ratio property as a fallback. 5) Skip purely decorative micro assets when the snippet already shows stable sizing and there is no evidence of layout shift risk. Show the before and after HTML for each fixed image.\n\n## Explain\n\nExplain why width and height attributes on <img> prevent Cumulative Layout Shift (CLS). When a browser parses HTML, it does not yet know an image's dimensions\u2014without explicit attributes, the browser initially renders a 0-height placeholder, then jumps the layout when the image loads. With width and height set, the browser calculates the aspect ratio from the attributes and reserves space immediately, eliminating the shift. This became especially important as modern browsers use the aspect-ratio CSS property derived from these HTML attributes.\n\n## Code Review\n\nReview image assets, markup, and delivery configuration related to Set explicit width and height on images. Flag exact files or components where format choice, sizing, or loading behavior violates the rule, and describe how to confirm the fix in DevTools.\n\n---\n\nFor full implementation details, code examples, and framework-specific guidance,\nsee `references\/rule.md`.\n\nRule page: https:\/\/frontendchecklist.io\/rules\/images\/dimensions\n","use_cases":["Fixing layout shifts caused by missing image dimensions in HTML","Ensuring responsive images maintain aspect ratio while scaling","Identifying and resolving CLS issues in web performance audits"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/thedaviddias\/Front-End-Checklist\/blob\/e8d14d049010e2ce25afcd5e8609dfcbbe63e47e\/skills\/dimensions\/SKILL.md","source_commit":"e8d14d049010e2ce25afcd5e8609dfcbbe63e47e","license":null,"version":null,"listing_approved_at":"2026-11-23","created_at":"2026-11-22T15:52:22.000000Z","updated_at":"2026-11-23T04:54:56.000000Z","seo_title":"Prevent CLS by Setting Image Dimensions","seo_description":"Set explicit width and height on images to prevent Cumulative Layout Shift (CLS). This improves layout stability and user experience.","seo_keywords":["prevent CLS","image dimensions","Cumulative Layout Shift","responsive images","HTML image attributes"]}}