douqian5920 2014-03-06 15:56
浏览 99
已采纳

如何防止img拉伸其父级

I'm using markdown language to add posts to the site. So, in order to add image to my post I write

![Alt text](/path/to/img.jpg) 

and img appears on the page. There's one problem though - images stretch its parent div if original width of the image exceeds container's width. I use fixed layout so width of container element is hardcoded in css. The Question is: is it even possible to prevent images from stretch my div but instead resize automatically to fit nicely into it?

  • 写回答

1条回答 默认 最新

  • dongyi2889 2014-03-06 15:59
    关注

    is it even possible to prevent images from stretch my div but instead resize automatically to fit nicely into it?

    Yes! Try setting:

    img { max-width: 100%; }
    

    As long as the <img>'s container has an explicit width, the image shouldn't exceed that width.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?