Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flex containers not positioned correctly when their margin-left/right is set to auto #2054

Closed
alexandergitter opened this issue Feb 1, 2024 · 1 comment · Fixed by #2233
Labels
bug Existing features not working as expected
Milestone

Comments

@alexandergitter
Copy link
Contributor

alexandergitter commented Feb 1, 2024

In the following example, a flex container (the .inner div) has its margin-left and margin-right properties set to auto, which should cause it to be centered in its parent div. Instead, it is positioned at the left edge of the .outer div.
WeasyPrint does this correctly when the inner div gets changed to display: block.

(This may or may not be related to #1171, although that issues talks about nested flex containers, whereas in my case the outer container is a normal block element.)

<style>
  .outer {
    background: red;
  }
  
  .inner {
    margin: auto;
    display: flex;
    width: 10rem;
    height: 10rem;
    background: black;
  }
</style>

<div class="outer">
  <div class="inner"></div>
</div>

image

@liZe liZe added the bug Existing features not working as expected label Feb 3, 2024
dhdaines added a commit to dhdaines/WeasyPrint that referenced this issue Aug 19, 2024
@dhdaines
Copy link
Contributor

Yes, this is because of this: https://github.com/Kozea/WeasyPrint/blob/main/weasyprint/layout/flex.py#L92

    # TODO: removing auto margins is OK for this step, but margins should be
    # calculated later.
    if parent_box.margin_top == 'auto':
        box.margin_top = parent_box.margin_top = 0
    if parent_box.margin_bottom == 'auto':
        box.margin_bottom = parent_box.margin_bottom = 0
    if parent_box.margin_left == 'auto':
        box.margin_left = parent_box.margin_left = 0
    if parent_box.margin_right == 'auto':
        box.margin_right = parent_box.margin_right = 0

It can, in fact, be solved by simply not setting box.margin_* to 0 - here parent_box is a temporary copy of the flex box which appears to be used (I don't totally understand the intent of the code) only for determining the minimum size of the flex items. Fixed in #2233

@liZe liZe closed this as completed in bb95cd0 Aug 19, 2024
@liZe liZe added this to the 63.0 milestone Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants