i

Learn HTML and CSS in 10 Days

Adding a Background Image and Background Repeat

Adding a Background Image

Apart from providing a background color to an element, background images can also be added up. The background images work in the same fashion as the background colors, but they provide a couple of extra properties to artfulness the pictures. Here also we can use the ‘background’ property that accepts the shorthand value, or we can utilize the background-image property. But each of them will be using the url() function. It will contain the location of the image file within the quotation.

div {

  background-image: url("alert.png");

}

When you use the 'url' value, then you will be getting some undesired results like the image will be repeated. To get away from this, we have background-position as well as the background-repeat property.

Background Repeat

By default, the image in the background gets repeated several times, both horizontally as well as vertically, until otherwise specified. We use the 'background-repeat' property to change the direction of repeating the background image, if any.

div {

  background-image: url("alert.png");

  background-repeat: no-repeat;

}

Values that the 'background-repeat' property takes in are repeat-x, repeat-y, no-repeat, repeat. The repeat-y value will make the background to repeat vertically while the repeat-x value will make the value to repeat horizontally. The no-repeat one will tell the browser to show the image in the background only once. The default value of this property is 'repeat,' which repeats the background image both horizontally as well as vertically.