Make WordPress Core


Ignore:
Timestamp:
10/07/2025 06:22:02 AM (2 months ago)
Author:
westonruter
Message:

Media: Switch to enqueueing contain-intrinsic-size CSS fix for IMG tags having sizes=auto.

This deprecates the wp_print_auto_sizes_contain_css_fix() function running at wp_head priority 1, in favor of a wp_enqueue_img_auto_sizes_contain_css_fix() function which runs just before at wp_head priority 0. The latter function unhooks the former while also enqueueing an inline style to be printed with all other styles but up front to preserve the cascade. This eliminates directly printing the STYLE tag, which was a change done similarly before for the emoji styles. See #58775.

For backwards compatibility, the CSS can still be prevented from being enqueued/printed via:

remove_action( 'wp_head', 'wp_print_auto_sizes_contain_css_fix', 1 );

This change ensures that all styles are printed together using the correct API for emitting styles.

Developed in https://github.com/WordPress/wordpress-develop/pull/8954.

Follow-up to [59435].

Props westonruter, sabernhardt, SirLouen, flixos90, joemcgill, SergeyBiryukov, superpoincare.
See #62413.
Fixes #62731.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r60811 r60910  
    64626462    return $editor_settings;
    64636463}
     6464
     6465/**
     6466 * Prints a CSS rule to fix potential visual issues with images using `sizes=auto`.
     6467 *
     6468 * This rule overrides the similar rule in the default user agent stylesheet, to avoid images that use e.g.
     6469 * `width: auto` or `width: fit-content` to appear smaller.
     6470 *
     6471 * @since 6.7.1
     6472 * @deprecated 6.9.0 Use wp_enqueue_img_auto_sizes_contain_css_fix() instead.
     6473 * @see wp_enqueue_img_auto_sizes_contain_css_fix()
     6474 *
     6475 * @see https://html.spec.whatwg.org/multipage/rendering.html#img-contain-size
     6476 * @see https://core.trac.wordpress.org/ticket/62413
     6477 * @see https://core.trac.wordpress.org/ticket/62731
     6478 */
     6479function wp_print_auto_sizes_contain_css_fix() {
     6480    _deprecated_function( __FUNCTION__, '6.9.0', 'wp_enqueue_img_auto_sizes_contain_css_fix' );
     6481
     6482    /** This filter is documented in wp-includes/media.php */
     6483    $add_auto_sizes = apply_filters( 'wp_img_tag_add_auto_sizes', true );
     6484    if ( ! $add_auto_sizes ) {
     6485        return;
     6486    }
     6487
     6488    ?>
     6489    <style>img:is([sizes="auto" i], [sizes^="auto," i]) { contain-intrinsic-size: 3000px 1500px }</style>
     6490    <?php
     6491}
Note: See TracChangeset for help on using the changeset viewer.