Remove width and height from inserted images

Remove width and height from inserted images

Online by JSC0d3
August 10, 2016 | | 163 recognitions

By default, WordPress adds width and height attributes (in HTML) to each image that you insert into a blog post or page.

Under most circumstances this is desirable, as it ensures that the appropriate room is made for the image within your design, even if the image itself is still loading. However, there are times when these ‘hardcoded’ dimensions will directly contradict something you are trying to achieve with CSS.

If you ever find yourself in that quandary, just use the following code to remove the width and height attributes from your images in WordPress:

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );

function remove_width_attribute( $html ) {
   $html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
   return $html;


JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | April 1, 2019

Images are vital components of every website Before you start questioning the importance of images, just try to imagine your favorite blog or website...

Posted by | March 5, 2019

I’ve been loosely following the noise and #wpdrama surrounding Gutenberg for as long as it has been around and honestly for the most part I’ve...

Posted by | February 24, 2019

To ensure that your site ranks highly in Search Engine Result Pages (SERPs), you’ll need to make it easy for search engine ‘bots’ to explore...

Previous PostBackNext Post

Leave here an impression