make background image fit screen code example

Example 1: set background image full screen css

#mainBackground{
    width: 100%;
    height: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0;
    background-image: url("https://images.unsplash.com/photo-1487058792275-0ad4aaf24ca7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80");
    background-size:100% 100%;
    background-repeat: no-repeat;

}
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

Example 2: css background image size to fit screen

html {
    height: 100%
}
body {
    background-image:url("../images/myImage.jpg");
    background-repeat: no-repeat;
    background-size: 100% 100%;
}

Example 3: css background image full screen responsive

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;
}

Example 4: html background image fit to screen

body {
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
}

Example 5: how to set a full size background image in html

<head>
<style>
#example1{
  border: 2px solid black;
  padding: 100px;
  background: url(mountain.jpg);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
</style>
</head>
<body>
<div id="example1">
</div>
</body>

Example 6: css full cover background image

body { 
  background: url(path/to/bg-image.jpg) no-repeat center center fixed;
  background-size: cover;
}

Tags: