How do I stretch a background image to cover the entire HTML element?

后端 未结 13 1124
难免孤独
难免孤独 2020-12-04 09:02

I\'m trying to get a background image of a HTML element (body, div, etc.) to stretch its entire width and height.

Not having much luck. Is it even possible or do I h

13条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-04 09:20

    Simply make a div to be the direct child of body (with the class name bg for example), encompassing all other elements in the body, and add this to the CSS file:

    .bg {
        background-image: url('_images/home.jpg');//Put your appropriate image URL here
        background-size: 100% 100%; //You need to put 100% twice here to stretch width and height
    }
    

    Refer to this link: https://www.w3schools.com/css/css_rwd_images.asp Scroll down to the part that says:

    1. If the background-size property is set to "100% 100%", the background image will stretch to cover the entire content area

    There it shows the 'img_flowers.jpg' stretching to the size of the screen or browser regardless of how you resize it.

提交回复
热议问题