Introduction
Background size controls how a background image fits inside an element. It helps make images look good on different screen sizes.
Jump into concepts and practice - no test required
background-size: <length> | <percentage> | cover | contain | auto;background-size: cover;background-size: contain;background-size: 100px 50px;
background-size: auto;<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Background Size Example</title> <style> .box { width: 20rem; height: 12rem; border: 2px solid #333; margin: 1rem; background-image: url('https://via.placeholder.com/300x150'); background-repeat: no-repeat; background-position: center; display: inline-block; vertical-align: top; } .cover { background-size: cover; } .contain { background-size: contain; } .fixed-size { background-size: 100px 50px; } .auto { background-size: auto; } </style> </head> <body> <h1>Background Size Examples</h1> <section> <article class="box cover" aria-label="Background size cover"> <strong>cover</strong> </article> <article class="box contain" aria-label="Background size contain"> <strong>contain</strong> </article> <article class="box fixed-size" aria-label="Background size 100px by 50px"> <strong>100px 50px</strong> </article> <article class="box auto" aria-label="Background size auto"> <strong>auto</strong> </article> </section> </body> </html>
background-repeat: no-repeat; to avoid repeating the image when using background-size.cover option is great for full-screen backgrounds.contain option is useful when you want the whole image visible without cropping.cover fills the area, possibly cropping the image.contain fits the whole image inside, possibly leaving space.background-size: cover; do to a background image?cover behaviorcover scales the background image to fill the entire element area, even if some parts get cropped.contain fits the whole image inside without cropping, and repeating is controlled by background-repeat.cover = fills and crops [OK]contain scales the image to fit inside the element fully without cropping.cover crops, fill and stretch are invalid values for background-size.div {
width: 200px;
height: 100px;
background-image: url('flower.jpg');
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
What will the background image look like inside the div?background-size: contain; effectbackground-repeat: no-repeat; prevents tiling, and background-position: center; centers the image. So empty space may appear if aspect ratios differ.div {
background-image: url('tree.jpg');
background-size: 100%;
background-repeat: no-repeat;
}
What is the problem?background-size: 100%; meaningbackground-repeat: no-repeat; disables tiling correctly, and background-size accepts percentages.background-size: 100%; only sets width, height is auto, so it may not fill the entire element. -> Option Acontain fits whole image but may leave empty space; cover fills but crops; 100% 100% stretches whole image to exactly fill without cropping or space; auto repeat tiles.background-size: 100% 100%; to stretch image exactly. -> Option D100% 100% fills exactly, no crop [OK]