How to make full screen background in a web page

前端 未结 10 1483
予麋鹿
予麋鹿 2020-12-15 09:46

How to make an image as background for web page, regardless of the screen size displaying this web page? I want to display it properly. How?

相关标签:
10条回答
  • 2020-12-15 10:14

    Make a div 100% wide and 100% high. Then set a background image.

    0 讨论(0)
  • 2020-12-15 10:15

    I found the reason why there is always a white boder of the background image, if I put the image in a 'div' element inside 'body'. But the image can be full screen, if I put it as background image of 'body'.

    Because the default 'margin' of 'body' is not zero. After add this css, the background image can be full screen even I put it in 'div'.

    body {
        margin: 0px;
    }
    
    0 讨论(0)
  • 2020-12-15 10:19

    A quick search for keywords background generator shows this CSS3 produced background pattern that's dynamically created.

    By keeping the image small and repeatable, you won't have problems with it loading on mobile devices and the small image file-size takes care of memory concerns.

    Here's the markup for the head section:

    <style type="text/css">
    
        body {
          background-image:url('path/to/your/image/background.png');
          width: 100%;
          height: 100%;
        }
    
    </style>
    

    If your going to use an image of something that should preserve aspect ratio, such as people or objects, then you don't want 100% for width and height since that will stretch the image out of proportion. Instead check out this quick tutorial that shows different methods for applying background images using CSS.

    0 讨论(0)
  • 2020-12-15 10:26

    I have followed this tutorial: https://css-tricks.com/perfect-full-page-background-image/

    Specifically, the first Demo was the one that helped me out a lot!

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

    this might help!

    0 讨论(0)
提交回复
热议问题