Skip to content

Incorporate layout constraints from ancestor blocks into the sizes calculations #1511

Description

@joemcgill

As outlined in #760, the initial work for accurately setting the sizes attribute for image and cover blocks has been released through the Enhanced Responsive Images plugin. While the current implementation effectively handles individual image blocks, it does not account for images constrained by other ancestor blocks. This issue aims to address this issue by focusing on multiple block types, including column, group, row, and stack blocks (alignments only). Enhancements for grid blocks are also being considered (though they are of lower priority due to their complexity and recent introduction).

The primary goal is to improve the sizes attribute across these block types to ensure images are displayed with the correct sizes for optimal rendering across different screen sizes.

Activity

  1. moved this to Not Started/Backlog 📆 in WP Performance Ongoingon Aug 29, 2024
  2. changed the title [-]Develop a system to incorporate layout constraints from ancestor blocks (e.g., group, row, columns, etc) into the sizes calculation.[/-] [+]Incorporate layout constraints from ancestor blocks into the sizes calculations[/+] on Aug 29, 2024
  3. moved this from Not Started/Backlog 📆 to Definition ✏️ in WP Performance Ongoingon Aug 29, 2024
  4. self-assigned this
    on Sep 26, 2024
  5. joemcgill commented on Oct 10, 2024

    @joemcgill
    MemberAuthor

    @mukeshpanchal27 and I have been exploring this issue over the last several weeks and I wanted to give a quick progress update.

    Technical discovery

    In order to incorporate layout information from column, group, row, and stack blocks that are ancestors of images, we considered several different approaches.

    Use wp_calculate_image_sizes directly

    Modifying the wp_calculate_image_sizes filter seems like the most straightforward was to improve the calculation of the sizes attribute. However, one of the key challenges is that this filter does not receive any layout information from the block context, which is crucial for accurately determining the appropriate sizes value for images. Instead, that function is only aware of the img markup itself, not even the figure that wraps the img and provides its own layout alignment.

    We created an experimental plugin to demonstrate a proof of concept for calculating the sizes value during block rendering, when more information is available. However, this approach is a bit fragile and will result in a DB query for each image.

    Hooking into the block rendering process

    In this approach, layout constraints would be passed from ancestor blocks to images using block rendering filter render_block_data. Inspired by how the core gallery block handles this process.

    While this approach could work, we quickly found that passing block layout information down to innerBlocks was not performant.

    Passing layout information as block context

    In this approach, layout constraints would be passed from ancestor blocks to images using block context.

    Before each block in a layout is rendered, context can be used to pass information about the current state of WordPress or information from parent blocks to their inner blocks. We can make use of this functionality by hooking into the render_block_context filter to capture information from a parent block—like the parent’s alignment or existence of sibling blocks—and set this as context that can be accessed by the image when the sizes attribute is being calculated.

    Some initial experiments with this approach have been promising and looks to be the best approach to pursue at this time.

    In the process of working on this approach we ran into the same bug reported in Trac#62046 and have been helping find a fix via WordPress/wordpress-develop#7344 and WordPress/wordpress-develop#7522.

    Next steps

    At this point, we're planning on moving forward on an implementation based on using the block context approach described above. This will likely be done in multiple PRs that focus on specific aspects of this problem (e.g., passing layout from columns to a nested image, passing layout from a group block, etc).

    Any new issues related to this effort should be added as sub-issues of this one to track progress. All PRs should target the newly created feature/1511-incorporate-layout-constraints-from-ancestors branch that has been created for this effort.

  6. joemcgill commented on Nov 21, 2024

    @joemcgill
    MemberAuthor

    To help with implementation of this issue, I'm listing some use cases we need to solve for

  7. moved this from To Do 🔧 to In Progress 🚧 in WP Performance Ongoingon Nov 21, 2024
  8. joemcgill commented on Nov 25, 2024

    @joemcgill
    MemberAuthor

    @mukeshpanchal27 before we get to far into this, I think trying to reorganize the file structure of the plugin a bit to separate the auto-sizes feature from the functionality to improve sizes calculation could be heplful. I've taked a pass at doing so in #1699.

  9. mukeshpanchal27 commented on Dec 10, 2024

    @mukeshpanchal27
    Member

    @joemcgill Here is different use cases for column block.

    • Add Columns Block

      • Select a predefined layout option for the columns, such as 100, 50 / 50, 33 / 66, etc.
        • If you select a 2-column layout, no width attribute is added to the column blocks.
        • If you select 33 / 66, specific width values are added to the column block.
    • Add Columns Block and Click 'Skip'

      • Clicking the "Skip" button on the layout option adds a 2-column layout without any width attributes in the column blocks. By default, the px unit is selected.
        • Adding a new column in this scenario creates it without a width, similar to the existing column.
    • Transform Image Block to Columns

      • Add an Image block and click "Transform to." This wraps the image in a single-column layout and sets the column's width to 100%.
        • Adding a new column in this scenario creates additional columns with a 100% width, regardless of the number of columns added.
    • Set Columns with Different Units

      • Add a Columns block and assign different units to the column widths.
        • For example, in a 2-column layout, set the first column to 50% width and the second column to 450px.
        • Consider similar cases for layouts with 3 or 4 columns.
    • Column Settings with Group or Columns as Parent

      • When a Group or Columns block is the parent, the same column settings are available for the child columns.
  10. joemcgill commented on Dec 10, 2024

    @joemcgill
    MemberAuthor

    Thanks @mukeshpanchal27. This is a good list of ways of creating columns in different layouts. For the purposes of defining the requirements for the the "Use block context to pass layout info from columns:" use case listed here, I think we should focus on what the expected sizes values will be in each scenario based on the block content generated in the use cases you described above.

    For example:

    Each column block that doesn't contain a width attribute will take up the same proportional space as it's sibling column blocks (e.g., 2 columns: 50%, 3 columns: 33%, etc.). So, if the block markup looks like this...

    <!-- wp:columns -->
    <div class="wp-block-columns"><!-- wp:column -->
    <div class="wp-block-column"><!-- wp:image -->
    <figure class="wp-block-image"><img alt=""/></figure>
    <!-- /wp:image --></div>
    <!-- /wp:column -->
    
    <!-- wp:column -->
    <div class="wp-block-column"><!-- wp:image -->
    <figure class="wp-block-image"><img alt=""/></figure>
    <!-- /wp:image --></div>
    <!-- /wp:column --></div>
    <!-- /wp:columns -->

    ...the sizes attributes for images should be limited to 50% of the core/columns block, based on its alignment context.

  11. joemcgill commented on Dec 12, 2024

    @joemcgill
    MemberAuthor

    As noted in #1701 (comment), when using layout widths returned by wp_get_global_settings() in sizes calculations, it is likely that those values will be set to something other than a px value, and our logic will need to be updated to handle these cases.

    According to this documentation, these values can be any valid CSS length value, including absolute values (e.g., px), relative values (e.g., rem, em, vw), percentages, CSS functions (e.g. calc(), clamp()), or even CSS variables (e.g., var(--theme-block-wide-max-width).

    For any values that can't be compared directly on the server we'll need to see if we can write sizes values that allow the client to handle the calculations rather than trying to resolve it on the server. For example, in auto_sizes_calculate_better_sizes() where we're comparing the width of an image with the container size to determine which is smaller, rather than doing something like this to resolve a specific px width:

    $alignment    = auto_sizes_get_layout_width( 'default' );
    $layout_width = sprintf( '%1$spx', min( (int) $alignment, $image_width ) );

    We would want to consider something like this:

    $alignment    = auto_sizes_get_layout_width( 'default' );
    $layout_width = "sprintf( 'min(%1$dpx, $alignment)', array( $image_width, $alignment ) );
  12. moved this from In Progress 🚧 to Done 😃 in WP Performance Ongoingon Dec 13, 2024
  13. moved this from Done 😃 to Not Started/Backlog 📆 in WP Performance Ongoingon Dec 13, 2024
  14. joemcgill commented on Dec 13, 2024

    @joemcgill
    MemberAuthor

    The first steps have been merged to trunk in #1738 in advance of the upcoming release. I'm reopening this issue for continued iteration on this list.

  15. moved this from Not Started/Backlog 📆 to In Progress 🚧 in WP Performance Ongoingon Dec 13, 2024
  16. added
    [Type] EpicA high-level project / epic that will encompass several sub-issues
    on Dec 13, 2024
  17. mukeshpanchal27 commented on Jul 7, 2025

    @mukeshpanchal27
    Member

    In the latest release (v1.5.0), the majority of issues have been resolved. However, a few still need to be implemented.

  18. felixarntz commented on Jul 7, 2025

    @felixarntz
    Member

    Thanks @mukeshpanchal27! Can you please open issues for these and link them to #1511 (comment)? Then we can proceed from there.

  19. westonruter commented on Aug 5, 2025

    @westonruter
    Member

    @mukeshpanchal27 What about for the Gallery block? When I have a gallery of three images at the beginning of post content, I see no size calculations are happening.

    HTML Markup
    <figure
      class="wp-block-gallery has-nested-images columns-default wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex"
    >
      <figure class="wp-block-image size-large">
        <img
          fetchpriority="high"
          decoding="async"
          width="1024"
          height="683"
          sizes="(max-width: 645px) 100vw, 645px"
          data-id="39"
          src="https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-1024x683.jpg"
          alt=""
          class="wp-image-39"
          srcset="
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-1024x683.jpg  1024w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-300x200.jpg    300w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-768x513.jpg    768w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-1536x1025.jpg 1536w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/07/Bison_with_its_young-2048x1367.jpg 2048w
          "
        />
      </figure>
    
      <figure class="wp-block-image size-large">
        <img
          decoding="async"
          width="1024"
          height="741"
          sizes="(max-width: 645px) 100vw, 645px"
          data-id="12"
          src="https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-1024x741.jpg"
          alt=""
          class="wp-image-12"
          srcset="
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-1024x741.jpg  1024w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-300x217.jpg    300w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-768x556.jpg    768w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-1536x1112.jpg 1536w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/Bison_bison_Wichita_Mountain_Oklahoma-2048x1483.jpg 2048w
          "
        />
      </figure>
    
      <figure class="wp-block-image size-large">
        <img
          decoding="async"
          width="1024"
          height="668"
          sizes="(max-width: 645px) 100vw, 645px"
          data-id="8"
          src="https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-1024x668.jpg"
          alt=""
          class="wp-image-8"
          srcset="
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-1024x668.jpg  1024w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-300x196.jpg    300w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-768x501.jpg    768w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-1536x1002.jpg 1536w,
            https://wcus-perf-talk-demo.local/wp-content/uploads/2025/06/American_bison_k5680-1-2048x1336.jpg 2048w
          "
        />
      </figure>
    </figure>

    Note that this has different layouts on Desktop vs Mobile:

    Desktop Mobile
    Image Image
  20. mukeshpanchal27 commented on Sep 29, 2025

    @mukeshpanchal27
    Member

    What about for the Gallery block?

    For the Gallery block, it’s a bit more complex. We haven’t yet explored how to calculate better sizes there 😄

  21. westonruter commented on Apr 9, 2026

    @westonruter
    Member

    What about for the Gallery block?

    For the Gallery block, it’s a bit more complex.

    I've filed this in #2449

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Plugin] Enhanced Responsive ImagesFormerly "Auto Sizes" [slug: auto-sizes][Type] EpicA high-level project / epic that will encompass several sub-issues

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions