March 9, 2012

Allow images in a <div> to be wider than lines of text in the same <div>

Question by Horace Loeb

I want to fit text in a <div> to the width of the <div>, but fit images in the same <div> to the width of the parent <div>

This diagram should make things clear:

(here’s the URL if it’s too small: http://dl.dropbox.com/u/2792776/screenshots/2012-01-22_1838.png)

Answer by Starx

With a little bit of js, you can do this with avoiding all the complication.

$(document).ready(function() {
    //read every images
    $("img").each(function(k,v) {
        //get the width parent's parent
        var width = $(this).parent().parent().innerWidth();
        //use it
        $(this).css('width',width);
    });
});

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!