Skip to content
Justice Tech School

Background Images

background-image accepts a url(), including data: URIs, or a gradient. background-repeat controls tiling (repeat, no-repeat, repeat-x), background-position places the image, and background-size can be cover, contain or explicit lengths.

cover scales the image to fill the box and may crop it, while contain fits the whole image inside and may leave gaps. Here we use a tiny inline SVG so no network is needed.

Example
<style>
  .tile {
    height: 120px;
    background-color: #ecfccb;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Ccircle cx='12' cy='12' r='5' fill='%2316a34a'/%3E%3C/svg%3E");
    background-repeat: repeat;
  }
  .once {
    margin-top: 10px;
    height: 120px;
    background-color: #ecfccb;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Ccircle cx='12' cy='12' r='5' fill='%2316a34a'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: center;
    background-size: 60px;
  }
</style>
<div class="tile"></div>
<div class="once"></div>

Exercise

Stop the background image from tiling.

.hero {
  background-repeat: ;
}

Quiz Test

Which background-size value fills the box and may crop the image?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.