Home >Web Front-end >CSS Tutorial >How to set CSS background

How to set CSS background

(*-*)浩
(*-*)浩Original
2019-11-27 11:46:182520browse

How to set CSS background

#CSS allows applying a solid color as a background, as well as creating quite complex effects using background images.

CSS’s capabilities in this regard are far superior to HTML. (Recommended learning: CSS Basic Tutorial)

Background color

You can use the background-color attribute to set the background color for an element. This property accepts any legal color value.

This rule sets the background of the element to gray:

p {background-color: gray;}

If you want the background color to extend less outward from the text in the element, just add some Padding:

p {background-color: gray; padding: 20px;}

You can set the background color for all elements, including body to inline elements such as em and a.

background-color cannot be inherited, and its default value is transparent. Transparent means "transparent". That is, if an element does not specify a background color, the background is transparent so that the background of its ancestor elements is visible.

Background image

To put an image into the background, you need to use the background-image attribute. The default value of the background-image property is none, which means no image is placed on the background.

If you need to set a background image, you must set a URL value for this attribute:

body {background-image: url(/i/eg_bg_04.gif);}

Most backgrounds apply to the body element, but they are not limited to this.

The following example applies a background to a paragraph without applying a background to the rest of the document:

p.flower {background-image: url(/i/eg_bg_03.gif);}

You can even set a background image for inline elements, The following example sets a background image for a link:

a.radio {background-image: url(/i/eg_bg_07.gif);}

In theory, it is even possible to apply images to the background of replacement elements such as textareas and select, but not all user agents can Handle the situation well.

In addition, I would like to add that background-image cannot be inherited. In fact, all background properties are not inherited.

The above is the detailed content of How to set CSS background. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn