Skip to content

fix: preserve aspect ratio for images with numeric dimensions#10130

Merged
akshayka merged 1 commit into
mainfrom
aka/fix-image-aspect-ratio
Jul 10, 2026
Merged

fix: preserve aspect ratio for images with numeric dimensions#10130
akshayka merged 1 commit into
mainfrom
aka/fix-image-aspect-ratio

Conversation

@akshayka

Copy link
Copy Markdown
Contributor

This PR changes images to carry their width and heights as HTML attributes instead of as inline styles. This in turn allows our stylesheet to correctly scale images when rendered in (for example) hstacks.

String values like "100%" are only valid in CSS and still go through the style attribute, preserving the percentage support added in #9966.

In particular, this prevents matplotlib figures (which carry pixel dimensions in their mimebundle metadata since #9144) from being squished or distorted.

Before change:

image

With fix:

image

Set numeric width/height as HTML attributes instead of inline styles.
Inline styles override the stylesheet's `height: auto`, so when
`max-width: 100%` clamped an image's width in a constrained container
(e.g. mo.hstack), the height stayed fixed and the image was distorted.
HTML attributes provide the intrinsic size and aspect ratio while
letting stylesheet rules shrink the image proportionally.

String values like "100%" are only valid in CSS and still go through
the style attribute, preserving the percentage support added in #9966.

Fixes matplotlib figures (which carry pixel dimensions in their
mimebundle metadata since #9144) rendering squished inside mo.hstack.
@akshayka akshayka requested review from Copilot and mscolnick July 10, 2026 03:38
@vercel

vercel Bot commented Jul 10, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Jul 10, 2026 3:38am

Request Review

@akshayka akshayka added the bug Something isn't working label Jul 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the ImageOutput frontend rendering so numeric width/height are emitted as HTML attributes (preserving intrinsic aspect ratio), while percentage/string dimensions continue to be applied via inline CSS.

Changes:

  • Render numeric width/height as <img width> / <img height> attributes instead of inline styles.
  • Keep string dimensions (e.g., "100%") on the style attribute to preserve percentage support.
  • Add unit tests covering numeric, string, mixed, and absent dimension cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
frontend/src/components/editor/output/ImageOutput.tsx Switch numeric dimensions to HTML attributes; keep string dimensions as inline styles.
frontend/src/components/editor/output/tests/ImageOutput.test.tsx Add tests to validate attribute-vs-style behavior for dimensions.

Comment on lines +20 to +24
// Numeric dimensions are set as HTML attributes rather than inline styles.
// Attributes give the browser the image's intrinsic size and aspect ratio,
// but can still be overridden by stylesheet rules (`max-width: 100%;
// height: auto` from preflight), so images shrink proportionally in
// width-constrained containers like mo.hstack. Inline styles would win

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

@akshayka akshayka merged commit 1a9d55f into main Jul 10, 2026
33 of 34 checks passed
@akshayka akshayka deleted the aka/fix-image-aspect-ratio branch July 10, 2026 14:34
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.14-dev54

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants