Skip to content

fix(css): specify position: relative for .katex#4170

Merged
grigoriy-reshetniak merged 2 commits intoKaTeX:mainfrom
egawata:fix-mathml-position
Mar 20, 2026
Merged

fix(css): specify position: relative for .katex#4170
grigoriy-reshetniak merged 2 commits intoKaTeX:mainfrom
egawata:fix-mathml-position

Conversation

@egawata
Copy link
Copy Markdown
Contributor

@egawata egawata commented Mar 19, 2026

What is the previous behavior before this PR?

Elements with the .katex-mathml class are positioned much lower than expected.
As a result, when there are many KaTeX-rendered elements on a page, unexpected whitespace appears at the bottom of the page.

demo: https://gist.github.com/egawata/fe28a58324c470ed100349e352d22471

The cause is that the .katex-mathml class has position: absolute, but its immediate parent .katex does not have position: relative.

This issue appears to be the same as the one reported in #2796.
Also, PR #3859 was intended to fix this, but it seems unable to be merged into the current latest master.

What is the new behavior after this PR?

The .katex-mathml elements are correctly positioned within the .katex elements, eliminating the layout displacement.

Fixes #2796

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 19, 2026

Greptile Summary

This PR fixes a long-standing CSS layout bug (#2796) where .katex-mathml (an accessibility-only, position: absolute element) was incorrectly positioned relative to a distant ancestor rather than its .katex parent, causing unexpected whitespace to accumulate at the bottom of pages with many KaTeX-rendered elements.

Key changes:

  • Adds position: relative to .katex in src/styles/katex.scss, making it a proper containing block for its absolutely-positioned .katex-mathml child.

Impact:

  • The fix is minimal and surgical — a single CSS property on the top-level .katex class.
  • .katex-mathml is a screen-reader-only element (1px × 1px, clipped, overflow: hidden) so there is no visual change to rendered math — only the layout displacement is corrected.
  • No JavaScript or rendering logic is touched.
  • No new stacking context is created (no z-index is set alongside position: relative), so existing layering behaviour is preserved.

Confidence Score: 5/5

  • This PR is safe to merge — it is a one-line, well-understood CSS fix with no side-effects on visual rendering or JavaScript behaviour.
  • The change is a single CSS property (position: relative) on a well-understood selector. The root cause (a missing positioned ancestor for an position: absolute child) is clearly documented in the issue, the fix is the canonical solution, and no other styling or logic is affected.
  • No files require special attention.

Important Files Changed

Filename Overview
src/styles/katex.scss Adds position: relative to .katex so that the absolutely-positioned .katex-mathml child is correctly contained within its parent, preventing layout displacement.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    subgraph Before["Before (Bug)"]
        A1["nearest positioned ancestor\n(position: relative/absolute/fixed)"] -->|".katex-mathml positioned\nrelative to this"| B1[".katex\n(position: static — default)"]
        B1 --> C1[".katex-mathml\n(position: absolute)\n⚠️ escapes .katex container"]
        B1 --> D1[".katex-html\n(visible math)"]
    end

    subgraph After["After (Fix)"]
        A2["nearest positioned ancestor"] --> B2[".katex\n(position: relative) ✅"]
        B2 -->|".katex-mathml now\ncontained here"| C2[".katex-mathml\n(position: absolute)\n✅ correctly clipped inside .katex"]
        B2 --> D2[".katex-html\n(visible math)"]
    end
Loading

Last reviewed commit: "fix(css): specify po..."

@grigoriy-reshetniak
Copy link
Copy Markdown
Collaborator

I checked #3970 (comment) and haven't seen any issues with \sqrt. It's also wrapped in position: relative by immediate parent, so it looks safe.

@grigoriy-reshetniak grigoriy-reshetniak merged commit 020f0d8 into KaTeX:main Mar 20, 2026
8 checks passed
@grigoriy-reshetniak
Copy link
Copy Markdown
Collaborator

Thank you!

@edemaine
Copy link
Copy Markdown
Member

🎉 This PR is included in version 0.16.40 🎉

The release is available on:

Your semantic-release bot 📦🚀

alexander-turner pushed a commit to alexander-turner/KaTeX that referenced this pull request Mar 20, 2026
## [0.16.40](KaTeX/KaTeX@v0.16.39...v0.16.40) (2026-03-20)

### Bug Fixes

* **css:** specify position: relative for .katex ([KaTeX#4170](KaTeX#4170)) ([020f0d8](KaTeX@020f0d8))
dadezzz pushed a commit to dadezzz/ice-notes that referenced this pull request Mar 25, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [katex](https://katex.org) ([source](https://github.com/KaTeX/KaTeX)) | [`0.16.39` → `0.16.40`](https://renovatebot.com/diffs/npm/katex/0.16.39/0.16.40) | ![age](https://developer.mend.io/api/mc/badges/age/npm/katex/0.16.40?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/katex/0.16.39/0.16.40?slim=true) |

---

### Release Notes

<details>
<summary>KaTeX/KaTeX (katex)</summary>

### [`v0.16.40`](https://github.com/KaTeX/KaTeX/blob/HEAD/CHANGELOG.md#01640-2026-03-20)

[Compare Source](KaTeX/KaTeX@v0.16.39...v0.16.40)

##### Bug Fixes

- **css:** specify position: relative for .katex ([#&#8203;4170](KaTeX/KaTeX#4170)) ([020f0d8](KaTeX/KaTeX@020f0d8))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44Ni4wIiwidXBkYXRlZEluVmVyIjoiNDMuODYuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: Renovate Bot <renovate@zarantonello.dev>
Co-committed-by: Renovate Bot <renovate@zarantonello.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MathML span position

3 participants