Stretch an image to fill width of browser window

前端 未结 5 1955
谎友^
谎友^ 2021-02-08 12:11

I have an image, and I want the width to fill up the browser window, no matter the size of the window.

How do I do this in HTML and CSS?

5条回答
  •  温柔的废话
    2021-02-08 12:18

    You add the following element in the head of your HTML document:

    
    

    Then you add

    max-width:100%;
    height:auto;
    

    as a CSS rule for both the image and the container of the image. (It can also work for the image without having a container.)

    And you add

    body {
        margin: 0;
    }
    

    to get rid of the margin around the image and/or container. This is how your image will fill the whole width of the screen, no matter what size the screen is.

提交回复
热议问题