Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make broken image respect CSS dimensions

Tags:

html

css

I am clamping image sizes with CSS:

#somewhere img {
   width: 160px;
   height: 90px;
}

When all images load properly, my page layout looks good. On the other hand, if any particular image does not load, my page layout gets screwed up because the broken image will occupy 0px by 0px. Elements around that broken image will scrunch upwards. How do I make broken images still respect the CSS dimensions so my layout doesn't collapse?

broken image

like image 501
JoJo Avatar asked Jul 14 '11 23:07

JoJo


1 Answers

Adding display: block to img should be good enough:

#somewhere img {
   width: 160px;
   height: 90px;
   display: block;
}

If that somehow doesn't work, then wrapping the img in another element will work.

<span><img class="channelLogoImg" width="160" height="90" src="" /></span>

#somewhere span, #somewhere img {
   width: 160px;
   height: 90px;
   display: block;
}

I chose span because that's the usual choice for frivolous wrappers.

like image 108
thirtydot Avatar answered Oct 08 '22 15:10

thirtydot