Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

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

Akshay Agrawal1a9d55ffea25
diff --git a/frontend/src/components/editor/output/ImageOutput.tsx b/frontend/src/components/editor/output/ImageOutput.tsx
index 896af23c83..93c560cff0 100644
--- a/frontend/src/components/editor/output/ImageOutput.tsx
+++ b/frontend/src/components/editor/output/ImageOutput.tsx
@@ -17,18 +17,32 @@ export const ImageOutput = ({
height,
className,
}: Props): JSX.Element => {
- // Convert numeric values to pixel strings, pass string values (like "100%") as-is
+ // 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
+ // over `height: auto` and distort the aspect ratio.
+ //
+ // String values like "100%" are only valid in CSS, so they go in the
+ // style attribute.
const style: React.CSSProperties = {};
- if (width !== undefined) {
- style.width = typeof width === "number" ? `${width}px` : width;
+ if (typeof width === "string") {
+ style.width = width;
}
- if (height !== undefined) {
- style.height = typeof height === "number" ? `${height}px` : height;
+ if (typeof height === "string") {
+ style.height = height;
}
return (
<span className={className}>
- <img src={src} alt={alt} style={style} />
+ <img
+ src={src}
+ alt={alt}
+ width={typeof width === "number" ? width : undefined}
+ height={typeof height === "number" ? height : undefined}
+ style={style}
+ />
</span>
);
};
diff --git a/frontend/src/components/editor/output/__tests__/ImageOutput.test.tsx b/frontend/src/components/editor/output/__tests__/ImageOutput.test.tsx
new file mode 100644
index 0000000000..c25fe9e618
--- /dev/null
+++ b/frontend/src/components/editor/output/__tests__/ImageOutput.test.tsx
@@ -0,0 +1,53 @@
+/* Copyright 2026 Marimo. All rights reserved. */
+
+import { render } from "@testing-library/react";
+import { describe, expect, it } from "vitest";
+import { ImageOutput } from "../ImageOutput";
+
+const src = "data:image/png;base64,iVBORw0KGgo=";
+
+describe("ImageOutput", () => {
+ it("renders numeric dimensions as HTML attributes", () => {
+ // Attributes (not inline styles) let stylesheet rules like
+ // `max-width: 100%; height: auto` shrink the image proportionally,
+ // preserving its aspect ratio in width-constrained containers.
+ const { container } = render(
+ <ImageOutput src={src} width={640} height={480} />,
+ );
+ const img = container.querySelector("img");
+ expect(img).toHaveAttribute("width", "640");
+ expect(img).toHaveAttribute("height", "480");
+ expect(img?.style.width).toBe("");
+ expect(img?.style.height).toBe("");
+ });
+
+ it("renders string dimensions as inline styles", () => {
+ const { container } = render(
+ <ImageOutput src={src} width="50%" height="100%" />,
+ );
+ const img = container.querySelector("img");
+ expect(img).not.toHaveAttribute("width");
+ expect(img).not.toHaveAttribute("height");
+ expect(img?.style.width).toBe("50%");
+ expect(img?.style.height).toBe("100%");
+ });
+
+ it("supports mixed numeric and string dimensions", () => {
+ const { container } = render(
+ <ImageOutput src={src} width={640} height="100%" />,
+ );
+ const img = container.querySelector("img");
+ expect(img).toHaveAttribute("width", "640");
+ expect(img).not.toHaveAttribute("height");
+ expect(img?.style.height).toBe("100%");
+ });
+
+ it("renders without dimensions", () => {
+ const { container } = render(<ImageOutput src={src} />);
+ const img = container.querySelector("img");
+ expect(img).not.toBeNull();
+ expect(img).not.toHaveAttribute("width");
+ expect(img).not.toHaveAttribute("height");
+ expect(img).toHaveAttribute("src", src);
+ });
+});