Skip to content

[fix] Remove heading padding in horizontal containers for proper alignment#14419

Merged
sfc-gh-lwilby merged 2 commits intodevelopfrom
fix/gh-12434
Mar 23, 2026
Merged

[fix] Remove heading padding in horizontal containers for proper alignment#14419
sfc-gh-lwilby merged 2 commits intodevelopfrom
fix/gh-12434

Conversation

@sfc-gh-lwilby
Copy link
Copy Markdown
Collaborator

@sfc-gh-lwilby sfc-gh-lwilby commented Mar 19, 2026

Describe your changes

Fixes #12434

Headings (st.title, st.header, st.subheader) have vertical padding that causes misalignment when placed in horizontal containers. The padding inflates the heading's box height, making it taller than sibling elements like buttons, so vertical_alignment="center" doesn't visually align the text.

  • Removes heading padding (h1–h6) when rendered inside a horizontal layout via FlexContext
  • Removes the compensating negative marginBottom on StyledStreamlitMarkdown in horizontal layouts

Screenshot or video (only for visual changes)

fixed fixed-mobile

GitHub Issue Link (if applicable)

Fixes #12434

Testing Plan

  • Unit Tests (JS): Added 2 tests to Heading.test.tsx verifying padding is removed in horizontal layout and preserved in vertical layout
  • E2E Tests: N/A
  • Manual testing: Verified with verify.py app showing title, header, and subheader alignment in horizontal containers with center/top/bottom alignment

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

Fixes #12434 — headings (h1–h6) had vertical padding that caused
misalignment with sibling elements in horizontal containers. Zero out
the padding and compensating negative marginBottom when FlexContext
indicates a horizontal layout.

Made-with: Cursor

Co-authored-by: lawilby <[email protected]>
@sfc-gh-lwilby sfc-gh-lwilby added change:bugfix PR contains bug fix implementation impact:users PR changes affect end users labels Mar 19, 2026
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Mar 19, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 19, 2026

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-14419/streamlit-1.55.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-14419.streamlit.app (☁️ Deploy here if not accessible)

@sfc-gh-lwilby sfc-gh-lwilby marked this pull request as ready for review March 20, 2026 16:01
Copilot AI review requested due to automatic review settings March 20, 2026 16:01
@sfc-gh-lwilby sfc-gh-lwilby added the ai-review If applied to PR or issue will run AI review workflow label Mar 20, 2026
@github-actions github-actions bot removed the ai-review If applied to PR or issue will run AI review workflow label Mar 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ AI Review Failed

The AI review job failed to complete. Please check the workflow run for details.

You can retry by adding the 'ai-review' label again or manually triggering the workflow.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 fixes heading (st.title, st.header, st.subheader) vertical misalignment inside horizontal containers by removing the extra vertical padding that inflates heading height and by disabling the related negative margin compensation in that layout context.

Changes:

  • Add a horizontal-layout flag to StyledStreamlitMarkdown to remove h1–h6 padding when used in horizontal layouts.
  • Remove StyledStreamlitMarkdown’s compensating negative marginBottom in horizontal layouts.
  • Add unit tests to verify heading padding behavior differs between horizontal vs vertical layouts.

Reviewed changes

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

File Description
frontend/lib/src/components/shared/StreamlitMarkdown/styled-components.ts Adds horizontal-layout conditional heading padding + disables negative margin compensation in that mode.
frontend/lib/src/components/shared/StreamlitMarkdown/Heading.tsx Reads FlexContext and passes horizontal-layout state to StyledStreamlitMarkdown.
frontend/lib/src/components/shared/StreamlitMarkdown/Heading.test.tsx Adds tests asserting padding is removed in horizontal layout and preserved in vertical layout.

Comment on lines +212 to +232
it("removes heading padding in horizontal layout", () => {
const props = getHeadingProps({ body: "hello", tag: "h1" })
const horizontalContext: IFlexContext = {
direction: Direction.HORIZONTAL,
isInHorizontalLayout: true,
isInRoot: false,
isInContentWidthContainer: false,
}

render(
<FlexContext.Provider value={horizontalContext}>
<Heading {...props} />
</FlexContext.Provider>
)

const markdownContainer = screen.getByTestId("stMarkdownContainer")
expect(markdownContainer).toHaveStyle({ "margin-bottom": "" })

const heading = screen.getByRole("heading")
expect(heading).toHaveStyle({ padding: "0" })
})
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

The new style assertions are likely brittle/incorrect for jest-dom: toHaveStyle expects concrete CSS values and typically camelCase keys. {"margin-bottom": ""} is not a valid CSS value (computed style is usually 0px or the property is omitted), and padding: "0" may not match the computed value (0px). Consider asserting with camelCase (marginBottom) and checking for 0px (or asserting the negative margin is not applied), and for padding prefer asserting paddingTop/paddingBottom are 0px in horizontal layout and non-zero in vertical layout.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for the review. The assertions here are intentional and match how Emotion generates CSS:

  • padding: "0" — The styled component sets padding: 0 (the number) when isInHorizontalLayout is true. toHaveStyle({ padding: "0" }) correctly matches this since jest-dom normalizes it. CI confirms the test passes.
  • marginBottom: "" — The styled component explicitly sets marginBottom: "" (empty string) to effectively unset the negative margin compensation. This is the actual value emitted by Emotion, so asserting against it is correct.

Also fixed the import ordering lint violation (IsSidebarContext before Layout/FlexContext) and updated the E2E snapshots for the horizontal-tabs layout in the follow-up commit.

Fix eslint import/order violation in Heading.test.tsx (IsSidebarContext
must sort before Layout/FlexContext) and update horizontal-tabs E2E
snapshots to reflect the heading padding removal.

Made-with: Cursor

Co-authored-by: lawilby <[email protected]>
@sfc-gh-lwilby sfc-gh-lwilby merged commit a1f3ec6 into develop Mar 23, 2026
43 checks passed
@sfc-gh-lwilby sfc-gh-lwilby deleted the fix/gh-12434 branch March 23, 2026 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:bugfix PR contains bug fix implementation impact:users PR changes affect end users

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make titles/headers align better in horizontal container

3 participants