# Horizontally center align block latex

**URL:** <https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848>\
**Category:** Card Design\
**Created:** [August 31, 2024, 5:57am UTC](https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848 "2024-08-31T05:57:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guoliang.oon](https://avatars.discourse-cdn.com/v4/letter/g/df788c/32.png) [@guoliang.oon](https://forums.ankiweb.net/u/guoliang.oon)\
**Post date:** [August 31, 2024, 5:57am UTC](https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848/1 "2024-08-31T05:57:01Z")

</div>

Is it possible to horizontally center align only rendered latex for those within `[$$]` tags but leave those within `[$]` tags inline?

This would make it consistent with how Anki aligns mathjax content for `\(` vs. `\[`.

I know latex content can in general be targeted with the css selector `img[src*="latex"]`. But that doesn’t distinguish between inline and block latex.

For example, in the screenshot below you can see that the block mathjax is center aligned, but the block latex (commutative diagram) is not.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/3X/0/4/047af421b173c4c20f17b92639108c6542778445.png)

---

<div class="post-metadata">

**Author:** ![jcznk](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/jcznk/32/15215_2.png) [@jcznk](https://forums.ankiweb.net/u/jcznk)\
**Post date:** [August 31, 2024, 7:25am UTC](https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848/2 "2024-08-31T07:25:10Z")

</div>

This code seems to work well on my end for applying styling specifically to “block LaTeX”:

```auto
.latex[alt*="displaymath"], .latex[alt*="$$"] {
    display: block; 
    margin-left: auto; 
    margin-right: auto;
}

```

Explanation: I checked the HTML and noticed that the `alt` attribute in the LaTeX images contains certain text like `"displaymath"` or `"$$"` when block LaTeX is used. The CSS uses a partial match selector (`[alt*=""]`) to target these cases.

While this worked in my testing, it might not cover every scenario. If needed, you can use this add-on to inspect the `alt` attribute and tweak the selector: [AnkiWeb - AnkiWebView Inspector Add-on](https://ankiweb.net/shared/info/31746032).

---

<div class="post-metadata">

**Author:** ![guoliang.oon](https://avatars.discourse-cdn.com/v4/letter/g/df788c/32.png) [@guoliang.oon](https://forums.ankiweb.net/u/guoliang.oon)\
**Post date:** [August 31, 2024, 7:47am UTC](https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848/3 "2024-08-31T07:47:46Z")

</div>

@jcznk What a great addon! Exactly what I needed. Thanks for teaching a man how to fish 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [September 30, 2024, 7:47am UTC](https://forums.ankiweb.net/t/horizontally-center-align-block-latex/48848/4 "2024-09-30T07:47:58Z")

</div>

Closed on September 30, 2024, 7:47am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
