Make an image to fit its parent dimensions

后端 未结 6 1405
梦如初夏
梦如初夏 2020-12-14 00:25

I have some photos which have big sizes, I would like to set them to the Parent\'s dimension size. If one time the parent dimension is 1200x800, I would like to set the phot

相关标签:
6条回答
  • 2020-12-14 00:49

    The css property that you want is max-width :

    Css

    img {
        max-width:100%;
    }
    

    You could add a container around your image and set overflow:hidden to prevent images to get bigger than the defined width/height.

    0 讨论(0)
  • 2020-12-14 00:49

    I might be naive, but isn't this as simple as this ?

    img {
        width:100%;
        height:100%;
    }
    
    0 讨论(0)
  • 2020-12-14 00:55

    Try this in the image tag

    <img src="url.jpg" width="100%" height="100%" />
    

    Or set the backgound of the element as the image and do the same.

    0 讨论(0)
  • 2020-12-14 00:56

    Use this bit of css to scale your image:

    img {
        max-width: 100%;
        max-height: 100%;
    }
    
    0 讨论(0)
  • 2020-12-14 01:05

    In the case the image is bigger than the parent container you have to set :

    img {
        max-width: 100%;
    }
    


    If your image is smaller you have to set instead

    img {
        min-width: 100%;
    }
    

    otherwise it will just stay at its original width and height.

    (the Height is set to auto by default)

    0 讨论(0)
  • 2020-12-14 01:07

    try to use the max-width property, this might give a bug in earlier versions IE though

    #custom img {
        max-width: 100%;
        max-height: 100%;
    }
    
    0 讨论(0)
提交回复
热议问题