background-image object fit code example

Example 1: css background image fit

body {
    background-image:    url(images/background.svg);
    background-size:     cover;                      /* <------ */
    background-repeat:   no-repeat;
    background-position: center center;              /* optional, center the image */
}

Example 2: css image fit in div with aspect ratio

img {
    width: 100%;
    height: 100%; 
    object-fit: contain;
}

Example 3: is there an img cover

body {
  margin: 0;
}
img {
  display: block;
  width: 100vw;
  height: 100vh;
  object-fit: cover;
}

Example 4: 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 5: how to contain image size

overflow: 'hidden',
backgroundSize : 'contain',

Example 6: make background image fit jumbotron

.jumbotron {background-size: 100% 100%; }

Tags:

Html Example