Skip to content

image not respecting alignement with png in .rst #1760

Description

@12rambau

I decided to open a follow-up of this thread as the bug seems to be fixed but fails on some more specific image type.

I try to align the content of my readme.rst file to use as a long description in my pypi release. using the .. raw:: directive is then impossible.

A common way to do it (that works on both sphinx and pypi) is:

.. image:: toto.png
   :align: center

It seems this is not taken into account when generating .png preview as shown in this gist where the .svg is correctly aligned and the .png is not.

Activity

  1. jluebbe commented on Jan 19, 2024

    @jluebbe

    It seems that alignment also doesn't work for URLs with parameters (?foo=1) after the .svg.

  2. 12rambau commented on May 27, 2024

    @12rambau
    Author

    Actually this is not respected by .sgv image anymore either. I think it's related to all the regression issue reported since Jan #1798

  3. github-actions commented on Jul 26, 2024

    @github-actions

    Stale issue message

  4. lafrech commented on Oct 1, 2024

    @lafrech

    This thread might be stale, but the issue is still there.

  5. flying-sheep commented on Apr 23, 2026

    @flying-sheep
    Contributor

    @zkoppert please reopen

  6. zkoppert commented on Apr 24, 2026

    @zkoppert
    Member

    Reopening this issue based on investigation in #1222. The bug is confirmed and reproducible — RST image alignment works for SVGs but not for PNGs (or any non-SVG image type).

    Root cause: Our GitHubHTMLTranslator in rest2html has custom depart_image handling for SVGs that rebuilds the <img> tag and copies the align attribute directly. Non-SVG images fall through to docutils' default HTMLTranslator, which converts :align: to class="align-right" — and the sanitization pipeline strips class attributes. The same issue affects :width: and :height:, which get converted to style attributes that are also stripped.

    Proposed fix: Update GitHubHTMLTranslator to emit align, width, and height as HTML attributes for all images (not just SVGs). These attributes are explicitly in the sanitizer's allowlist and are safe — they can only express layout, not execute code.

  7. added
    keepLabel to avoid being marked as stale
    and removed on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepLabel to avoid being marked as stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions