Remove WordPress core CSS for auto width images

In WordPress 6.7, a new set of functions were added to WordPress core which were designed to improve things relating to lazy loaded images with auto sizing.

However, I have noticed across a number of websites that the functions and related CSS has visual breaking changes.

I have seen images take over the entire width of the screen, even when their intrinsic size is very small. I have seen grid layouts become completely broken due to the images taking up too much visual space. It can destroy the look and feel of a website, and it doesn’t necessarily have anything to do with theme code.

Thankfully, there is a filter to remove this CSS and the related HTML change on images where it adds a sizes=”auto”.

Helper function that removes WordPress core CSS for auto width images

Add this helper function to your themes functions file to remove the WordPress default CSS and HTML sizes auto that applies to auto width images.

add_filter('wp_img_tag_add_auto_sizes', '__return_false');

I hope this helps people fix what can feel like random and major breaking visual changes on their websites.