Html backgrounds and codes

CSS Backgrounds

The CSS background properties are used to add background effects for elements.

In these chapters, you will learn about the following CSS background properties:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position
  • background (shorthand property)

CSS background-color

The background-color property specifies the background color of an element.

Example

The background color of a page is set like this:

With CSS, a color is most often specified by:

  • a valid color name — like «red»
  • a HEX value — like «#ff0000»
  • an RGB value — like «rgb(255,0,0)»

Look at CSS Color Values for a complete list of possible color values.

Other Elements

You can set the background color for any HTML elements:

Example

Here, the ,

, and elements will have different background colors:

div background-color: lightblue;
>

Opacity / Transparency

The opacity property specifies the opacity/transparency of an element. It can take a value from 0.0 — 1.0. The lower value, the more transparent:

Example

Note: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read.

Transparency using RGBA

If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:

You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) — which specifies the opacity for a color.

An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.

Example

The CSS Background Color Property

Источник

HTML Background Images

A background image can be specified for almost any HTML element.

Background Image on a HTML element

To add a background image on an HTML element, use the HTML style attribute and the CSS background-image property:

Example

Add a background image on a HTML element:

You can also specify the background image in the element, in the section:

Example

Specify the background image in the element:

Background Image on a Page

If you want the entire page to have a background image, you must specify the background image on the element:

Example

Add a background image for the entire page:

Background Repeat

If the background image is smaller than the element, the image will repeat itself, horizontally and vertically, until it reaches the end of the element:

Example

To avoid the background image from repeating itself, set the background-repeat property to no-repeat .

Example

Background Cover

If you want the background image to cover the entire element, you can set the background-size property to cover.

Also, to make sure the entire element is always covered, set the background-attachment property to fixed:

This way, the background image will cover the entire element, with no stretching (the image will keep its original proportions):

Example

Background Stretch

If you want the background image to stretch to fit the entire element, you can set the background-size property to 100% 100% :

Try resizing the browser window, and you will see that the image will stretch, but always cover the entire element.

Example

Learn More CSS

From the examples above you have learned that background images can be styled by using the CSS background properties.

To learn more about CSS background properties, study our CSS Background Tutorial.

Источник

HTML Background Code

This article provides code for setting the background properties of an HTML element.

These examples use Cascading Style Sheets (CSS). Using CSS, you can set the background properties of any HTML element. Plus you can do things like, specify an image’s position, whether it should repeat, how it should repeat etc.

Background Color

You can set the background color of any HTML element using the background-color property.

Background Image

You can set a background image using the background-image property.

Using the code below, you need to change /pix/samples/bg1.gif to the location of your background image. If you need a background image, check out these free background patterns.

Fixed Background Image

You can fix the position of a background image so that its position is fixed even if its containing block scrolls. You do this with the background-attachment property. Once again, you need to change /pix/samples/bg1.gif to the location of your background image.

Shorthand Background Code

You can use the CSS background property to set all the background properties at once. Therefore, you can set background-attachment , background-color , background-image , background-position , background-repeat all in one place using the background property.

Therefore, using the previous code example, we could rewrite it to this:

Backgrounds for the Whole Page

To set the background properties for the whole page, simply apply the property/properties to the body element.

Backgrounds for the Whole Site

The examples on this page use inline styles, which means that the CSS is attached to each HTML element within the HTML code. It’s a good idea to transfer these to an external style sheet if you need to use the same styles throughout your website.

Doing this will save you from having to copy/paste the code on to multiple pages. External style sheets enable you to write the code once, then apply it to your whole site (regardless of how many pages you have).

Background Gradients

CSS3 enables you to create that «background gradient» effect. This is where you can make different colors blend together nicely across an HTML element.

Before CSS3, in order to create a gradient, you had to create an image, upload it to your server, then use the method described above (under the «Background Image» heading).

With the introduction of gradients to CSS3, gradients are a breeze to create.

To create a background gradient, check out these pages:

Full List of Background Properties

Here’s a list of CSS background properties you can use on your HTML elements:

The following properties were introduced in CSS level 3.

Источник

background

The background shorthand CSS property sets all background style properties at once, such as color, image, origin and size, or repeat method. Component properties not set in the background shorthand property value declaration are set to their default values.

Try it

Constituent properties

This property is a shorthand for the following CSS properties:

Syntax

/* Using a */ background: green; /* Using a and */ background: url("test.jpg") repeat-y; /* Using a and */ background: border-box red; /* A single image, centered and scaled */ background: no-repeat center/80% url("../img/image.png"); /* Global values */ background: inherit; background: initial; background: revert; background: revert-layer; background: unset; 

The background property is specified as one or more background layers, separated by commas.

The syntax of each layer is as follows:

Оцените статью