How to CSS Background ?

css background

The background of your WebPages are one of the fundamentals that you simply need to know. It is one of the core part of CSS3. In this lesson, we will cover the basics of using CSS backgrounds and how to apply these CSS background properties in your web pages.

CSS Background color

The background-color property in CSS fills the background with a solid color. You can apply background color in the following ways:

How to CSS Images ?

The CSS Image represents a 2D image. In this chapter, we will look at how to use Background Images in CSS to create wonderful WebPages.

CSS Background Images

Background Images play an important role in the WebPages. As you saw in the previous chapter, the Background Color property is to add a color to the page or element to which it is applied. The Background Image property allows us to specify an image to be loaded and displayed as a background. The syntax declaration of Background Images is similar to that of background-color.

How to Add a Background Image with CSS


css background

The background-image property allows you to select an image by a URL reference. The above statement set an image background for the whole page, because we used it on the body tag. You can use background-image property on most CSS elements.

CSS Background Repeat

By default, as we saw in the above picture, a background-image is tiled vertically and horizontally. We can control a background image through its background-repeat property. The default background-image property tiles the images in x-axis and y-axis, so we use used to control it by axis base like repeat-x and repeat-y.


background repeat x-axis

The above CSS statement tiles the background-image only x-axis. Likewise you can set background-repeat property as repeat-y then the images tiles only on y-axis.

No repeat

To turns off tiling background from repeating, you can use no-repeat in background-repeat property. This will prevent tiling altogether and image will be displayed only once in the webpage.

css background no repeat

Positioning Background Images

By default, if it is not repeating, the background image will appear at the top left corner of the viewport. You can position the background images by setting the background-position property of an image. This property control where an element`s background image is placed in the webpage.

css background position

In the above code, the image is not repeating and positioning at the top right corner of the container element. You can positioned background images in various combinations as in the following table.

css background positions

Fix a Background Image in place

The background image, by default, scrolls with the content of the webpage. Scroll is the default placing property and another one is fixed. We can use the background-attachment property is to control both these values.

The above code fixed the background image in viewport, that means it is not scrolling with the content. If you want to scroll your content set as scroll.

CSS Background size

The CSS property background-size allows you to adjust the size of background images, instead of the default behavior of tiling the image at its full size. You can specify the size of the background in CSS.

The above CSS code display an image of size 100 100.

How Do you Stretch a Background Image in a Web Page?

In CSS you can stretch the background image on full screen by background-size property.

How to make a full screen background ?

Also you can set Full Page Background Image using the following code:

CSS Div as background

How to make a Div as full screen

You can set CSS Div as your background of your web page. The following code will make a full page background using CSS Div element.

CSS background color opacity

If you want to make your screen background color transparent, you can use opacity.

You can use the above HTML code for using this CSS code. When you run the code, you will get a transparent red color background.

CSS Transparent background image

You can make your div image as transparent and set as your background screen.

You can use the above HTML code for using this CSS code. When you run the code, you will get a transparent image background as your screen background.

Multiple background images

In CSS3, you can apply multiple backgrounds to your web pages by using a simple comma-separated list. It's like z-index but this isn't z-index, These images are layered atop one another with the first background you provide on top and the last background listed in the back. The current versions of all the major browsers now support this feature.