How to make the background image to fit into the whole page without repeating using plain css?

前端 未结 6 468
温柔的废话
温柔的废话 2021-02-01 06:28

I have an JPG image with size 1024 x 724. My page size is not fixed. My requirement is: If I resize the page then the background image should also resize and fit to the page.

6条回答
  •  梦如初夏
    2021-02-01 06:49

    You can either use JavaScript or CSS3.

    JavaScript solution: Use an absolute positioned tag and resize it on the page load and whenever the page resizes. Be careful of possible bugs when trying to get the page/window size.

    CSS3 solution: Use the CSS3 background-size property. You might use either 100% 100% or contain or cover, depending on how you want the image to resize. Of course, this only works on modern browsers.

提交回复
热议问题