Can't resize image using CSS

后端 未结 5 1396
萌比男神i
萌比男神i 2021-01-01 03:36

I\'ve tried every answer I could find on all the sites I could find, but still haven\'t been able to properly resize an image using CSS. I\'ve got it inside a div, and tried

相关标签:
5条回答
  • 2021-01-01 04:01

    The CSS property background:cover will help you!

    html { 
     background: url(images/bg.jpg) no-repeat center center fixed; 
     background-size: cover;
    }
    

    Cover will extend your background to full screen.

    0 讨论(0)
  • 2021-01-01 04:10

    CSS-Tricks has the best solution for this that I could find

    html { 
      background: url(images/bg.jpg) no-repeat center center fixed; 
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }

    Link to CSS-Tricks for the source and original code: https://css-tricks.com/perfect-full-page-background-image/

    0 讨论(0)
  • 2021-01-01 04:10

    Did you remember to link to your stylesheet? I was just having this issue, then I realized that I forgot to link to the CSS and face-palm.

    0 讨论(0)
  • 2021-01-01 04:19

    You have to give the id banner a specific width that is less than 100%. You don't need a height, it already is automatic. You have to target the image inside the banner and not the class added to the image. So it should look like this:

    <div id="banner"><img src="resources/img/banner.png" class="myImage"></div>
    
    #banner{
        width: 60%;
        left: 0px;
        right: 0px;
    }
    
    #banner img{
        width: 100%;
        left: 0px;
        right: 0px;
    }
    
    0 讨论(0)
  • 2021-01-01 04:20

    If your image is smaller than the screen, it will use the image width. If it is bigger, it uses max-width. So assuming your image is smaller than the display, you want to change your "max-width" to "width" to increase the image size.

    <div id="banner"><img src="resources/img/banner.png" id="myImage"></div>
    
    #banner{
        max-width: 100%;
        height: auto;
        left: 0px;
        right: 0px;
    }
    
    #myImage{
        width: 100%;
        height: auto;
        left: 0px;
        right: 0px;
    }
    
    0 讨论(0)
提交回复
热议问题