lampshade
lampshade

Reputation: 2796

Internet Explorer adds height- and width-attributes to a newly appended image automatically

I append a newly created image after it has been loaded to the DOM:

var i = $('<img/>');
i[0].src = 'http://placehold.it/700x300';
i.attr('alt', '');
i.on('load', function() {
    $('body').append(i);            
});

I have set a fixed height for the images in CSS:

img {
    height: 150px;
}

Unfortunately the Internet Explorer adds the width- and height-attributes to the image so it gets heavily distorted. How can I prevent this? Do I have to manually remove the attributes after I append the element?

jsFiddle link

Upvotes: 12

Views: 2549

Answers (2)

dezman
dezman

Reputation: 19358

You can either add !important to your css, or remove the width and height attrs.

img {
    height: 150px !important;
}

or

i.height('').width('');

Upvotes: 0

doitlikejustin
doitlikejustin

Reputation: 6353

Try this:

img {
    height: 150px;
    width: auto;
}

Upvotes: 13

Related Questions