Coolest Guides on the Planet

coolest guides on the planet

Coolest Guides On The Planet

  • Home
  • macOS
  • WebDev
  • All Posts
  • Contact

Responsively Removing Width and Height Attributes on Images and wp-caption on WordPress

May 9, 2013 8 Comments

If you are looking to go responsive for images on your site and want a theme that responds to various devices such as tablets and mobiles there are a couple of issues to sort out with existing hard coded images that have hard coded inline style width and height attributes. In WordPress not only do you need to removed these inline styles of images but also need to address the div ID attachment/wp-caption class that sets an inline width style attribute to the parent div container of the image each time you set a caption.

how does she scale

Drag the window narrower – how does she scale?

Best CSS Mark Up for scaling images

First up is that the best way to scale images responsively is by optimising the image at the larger scale required such as your intended desktop size and then letting it scale down for table and mobile devices. So set the largest size you need and just use the max-width property set to 100%

img {
max-width:100%;}

This way the image will only scale as wide as how many pixels are actually in it, not beyond that width, but will scale down to fit smaller container areas.

Removing Existing Inline Styling for Width and Height Attributes on Images

This can be achieved with some javascript, I couldn’t find a way to do this via a mass mysql method, so here is a great script , put it in the header.php of your theme.

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
 $('img').each(function(){ 
 $(this).removeAttr('width')
 $(this).removeAttr('height');
 });
});
</script>

This can be used to target specific images instead of all images by using a CSS selector, like so:

 $('.myspecificclass img').each(function()

So now you can remove all inline styling attributes and target images with the max-width property. But WordPress can also throw in some additional inline styling of the surrounding div of the image which has a class of wp-caption and has an inline width style, this inline style prevents the image from scaling. This is only added if you have used captions in your pics.

Removing the wp-caption Inline Width Style on Images

I found 2 solutions to this problem – one is a php function that just takes out the wp-caption inline style across the board and the other is a CSS solution.

php script

You need to add this php function to your function.php file in your theme.

add_shortcode( 'wp_caption', 'fixed_img_caption_shortcode' );
add_shortcode( 'caption', 'fixed_img_caption_shortcode' );

function fixed_img_caption_shortcode($attr, $content = null) {
	 if ( ! isset( $attr['caption'] ) ) {
		 if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
		 $content = $matches[1];
		 $attr['caption'] = trim( $matches[2] );
		 }
	 }
	 $output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
		 if ( $output != '' )
		 return $output;
	 extract( shortcode_atts(array(
	 'id'      => '',
	 'align'   => 'alignnone',
	 'width'   => '',
	 'caption' => ''
	 ), $attr));
	 if ( 1 > (int) $width || empty($caption) )
	 return $content;
	 if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
	 return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" >' 
	 . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
}

css method

div.wp-caption{
 max-width: 100%;}

Because there is no ‘height’ attribute, the max-width attribute overrides the inline width pixel dimension when the viewport is smaller and the images scale to fit.

Cats: Theming, WordPress

Tags

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls

Donate a Beer to the Coolest Guides

Get Beaver Builder Now!

Discuss

3gs 10.6 apache backup baseband boot clean urls cpanel css curl custom database drupal el capitan git Google image instadmg ios iphone jailbreak keys lion mac macos mojave macos sierra menu mysql OSX panda php phpmyadmin private public redirect redsn0w remote rsa SEO shell ssh terminal unstoppables upgrade urls
Get DesktopServer

Lynda

Lynda.com Online Training Videos

TreeHouse

smlinks

Learn WordPress
osx-modify-shell-path

How to Add to the Shell Path in macOS Big Sur and Catalina using Terminal

October 19, 2019

virtual-hosts osx 10.10 yosemite

Set up Virtual Hosts on macOS Catalina 10.15 in Apache

October 19, 2019

Installing Homebrew on macOS Catalina 10.15, Package Manager for Linux Apps

October 18, 2019

Where is the bash shell in macos Catalina?

October 12, 2019

Refine your search

  • All
  • Modules
  • Themes
  • Documentation
  • Forums & Issues
  • Groups

RSS ars technica

  • Musi says evidence shows Apple conspired with music industry on App Store ban
  • Trump threatens Apple with 25% tariff to force iPhone manufacturing into US
  • I helped a lost dog’s AirTag ping its owner: An ode to replaceable batteries
  • Apple legend Jony Ive takes control of OpenAI’s design future
  • Epic goes to court to force Fortnite back on US iOS

RSS mac surfer

  • Tot is new text editor for Mac, iPhone, and iPad focused on constraints and ease of use
  • TiPbITS: Google Drive Sorting Can Hide New Documents
  • How to take a screenshot on a MacBook Pro
  • How To Create Simple Animation With Mac Keynote
  • Last Week on My Mac: Virus pandemics

Donate

Copyright © 2025· Neil Gee - All Rights Reserved - Hosted by Runcloud

Copyright © 2025 · gee on Genesis Framework · WordPress · Log in