Add background image with css

How to Add Background Image with CSS

Let us demonstrate how you can add and position a background image in an HTML document with CSS styles.

How to position a background image.

  • background-image: defines one or more background images for the element.
  • background-repeat: specifies if/how a background image is repeated.
  • background-attachment: defines whether a background image scrolls with the rest of a page or is fixed.
  • background-position: defines the starting position of a background image.

Example of adding a background image:

html> html> head> title>Title of the document title> style> body < background-image: url('https://www.w3docs.com/uploads/media/default/0001/01/477719675fecaac0362957c214fb9aa56fca99b5.jpeg'); background-repeat: no-repeat; background-attachment: fixed; background-position: center; > style> head> body> body> html>

Result

In the example above, the background image is positioned in the center (you can also use other values such as left top; left center; left bottom; right top; right center; right bottom; etc.).

Читайте также:  Multiple php fpm nginx

Here are some ways of positioning a background image:

To repeat a background image, you can use the following values:

  • repeat, which repeats the background image vertically and horizontally,
  • repeat-x, which repeats the background image only horizontally,
  • repeat-y, which repeats the background image only vertically.

Is it possible to add a background image without a URL From the Computer?

Unfortunately, it is not possible to add a background image to a webpage without specifying the image’s URL. This is because web browsers need to know where to locate the image file in order to display it on the page.

Unfortunately, it is not possible to add a background image to a webpage without specifying the image’s URL. This is because web browsers need to know where to locate the image file in order to display it on the page.

Example of adding a background image repeated vertically:

html> html> head> title>Title of the document title> style> body < background-image: url("https://www.w3docs.com/uploads/media/default/0001/01/477719675fecaac0362957c214fb9aa56fca99b5.jpeg"); background-repeat: repeat-y; > style> head> body> body> html>

In the example above, the background image is repeated only vertically.

Use the «no-repeat» value so as the background image won’t be repeated (the image will only be shown once).

Example of adding a non-repeated background image:

html> html> head> title>Title of the document title> style> body < background-image: url("https://www.w3docs.com/uploads/media/default/0001/01/477719675fecaac0362957c214fb9aa56fca99b5.jpeg"); background-repeat: no-repeat; > style> head> body> body> html>

CSS3 introduced the background-size property, which helps us to control the background-image size as displayed in its parent element. In the following example, as a background-size value, we use “cover”, which scales the background image as much as possible so that the background image entirely covers the area.

To create a full-page background image, also add a background image to the container with the height set to 100%.

Example of adding a full-page background image:

html> html> head> title>Title of the document title> meta name="viewport" content="width=device-width, initial-scale=1"> style> body, html < height: 100%; margin: 0; > .bg < background-image: url("https://www.w3docs.com/uploads/media/default/0001/01/477719675fecaac0362957c214fb9aa56fca99b5.jpeg"); height: 100%; background-position: center; background-repeat: no-repeat; background-size: cover; > style> head> body> div class="bg"> div> body> html>

To add a transparent background image, you need the opacity property, which specifies the transparency of an element. Take a value from 0.0-1.0. to decrease the transparency (e.g. 0.2 is hazy, 0.5 makes half transparent).

Example of adding a background image with a specified opacity:

html> html> head> title>Title of the document title> style> img < opacity: 0.5; > style> head> body> img src="https://www.w3docs.com/uploads/media/default/0001/01/477719675fecaac0362957c214fb9aa56fca99b5.jpeg" width="300" height="150" alt="w3docs"> body> html>

In the example above, the image has 50% opacity.

Источник

background-image

The background-image CSS property sets one or more background images on an element.

Try it

The background images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.

The borders of the element are then drawn on top of them, and the background-color is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the background-clip and background-origin CSS properties.

If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none value.

Note: Even if the images are opaque and the color won’t be displayed in normal circumstances, web developers should always specify a background-color . If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.

Syntax

Each background image is specified either as the keyword none or as an value.

To specify multiple background images, supply multiple values, separated by a comma:

background-image: linear-gradient( to bottom, rgba(255, 255, 0, 0.5), rgba(0, 0, 255, 0.5) ), url("catfront.png"); /* Global values */ background-image: inherit; background-image: initial; background-image: revert; background-image: revert-layer; background-image: unset; 

Values

Is a keyword denoting the absence of images.

Accessibility concerns

Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page’s overall purpose, it is better to describe it semantically in the document.

Formal definition

Initial value none
Applies to all elements. It also applies to ::first-letter and ::first-line .
Inherited no
Computed value as specified, but with url() values made absolute
Animation type discrete

Formal syntax

Examples

Layering background images

Note that the star image is partially transparent and is layered over the cat image.

HTML

div> p class="catsandstars">This paragraph is full of catsbr />and stars.p> p>This paragraph is not.p> p class="catsandstars">Here are more cats for you.br />Look at them!p> p>And no more.p> div> 

CSS

p  font-size: 1.5em; color: #fe7f88; background-image: none; background-color: transparent; > div  background-image: url("mdn_logo_only_color.png"); > .catsandstars  background-image: url("startransparent.gif"), url("catfront.png"); background-color: transparent; > 

Result

Specifications

Browser compatibility

BCD tables only load in the browser

See also

  • Implementing image sprites in CSS
  • Image-related data types: ,
  • Image-related functions:
    • cross-fade()
    • element()
    • image()
    • image-set()
    • linear-gradient()
    • radial-gradient()
    • conic-gradient()
    • repeating-linear-gradient()
    • repeating-radial-gradient()
    • repeating-conic-gradient()
    • paint()
    • url()

    Found a content problem with this page?

    This page was last modified on Jul 18, 2023 by MDN contributors.

    Your blueprint for a better internet.

    MDN

    Support

    Our communities

    Developers

    Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
    Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

    Источник

    CSS background-image Property

    The background-image property sets one or more background images for an element.

    By default, a background-image is placed at the top-left corner of an element, and repeated both vertically and horizontally.

    Tip: The background of an element is the total size of the element, including padding and border (but not the margin).

    Tip: Always set a background-color to be used if the image is unavailable.

    Default value: none
    Inherited: no
    Animatable: no. Read about animatable
    Version: CSS1 + new values in CSS3
    JavaScript syntax: object.style.backgroundImage=»url(img_tree.gif)» Try it

    Browser Support

    The numbers in the table specify the first browser version that fully supports the property.

    CSS Syntax

    Property Values

    Value Description Demo
    url(‘URL‘) The URL to the image. To specify more than one image, separate the URLs with a comma Demo ❯
    none No background image will be displayed. This is default
    conic-gradient() Sets a conic gradient as the background image. Define at least two colors Demo ❯
    linear-gradient() Sets a linear gradient as the background image. Define at least two colors (top to bottom) Demo ❯
    radial-gradient() Sets a radial gradient as the background image. Define at least two colors (center to edges) Demo ❯
    repeating-conic-gradient() Repeats a conic gradient Demo ❯
    repeating-linear-gradient() Repeats a linear gradient Demo ❯
    repeating-radial-gradient() Repeats a radial gradient Demo ❯
    initial Sets this property to its default value. Read about initial
    inherit Inherits this property from its parent element. Read about inherit

    More Examples

    Example

    Sets two background images for the element. Let the first image appear only once (with no-repeat), and let the second image be repeated:

    body <
    background-image: url(«img_tree.gif»), url(«paper.gif»);
    background-repeat: no-repeat, repeat;
    background-color: #cccccc;
    >

    Example

    Use different background properties to create a «hero» image:

    .hero-image <
    background-image: url(«photographer.jpg»); /* The image used */
    background-color: #cccccc; /* Used if the image is unavailable */
    height: 500px; /* You must set a specified height */
    background-position: center; /* Center the image */
    background-repeat: no-repeat; /* Do not repeat the image */
    background-size: cover; /* Resize the background image to cover the entire container */
    >

    Example

    Sets a linear-gradient (two colors) as a background image for a element:

    Example

    Sets a linear-gradient (three colors) as a background image for a element:

    #grad1 <
    height: 200px;
    background-color: #cccccc;
    background-image: linear-gradient(red, yellow, green);
    >

    Example

    The repeating-linear-gradient() function is used to repeat linear gradients:

    #grad1 <
    height: 200px;
    background-color: #cccccc;
    background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
    >

    Example

    Sets a radial-gradient (two colors) as a background image for a element:

    Example

    Sets a radial-gradient (three colors) as a background image for a element:

    #grad1 <
    height: 200px;
    background-color: #cccccc;
    background-image: radial-gradient(red, yellow, green);
    >

    Example

    The repeating-radial-gradient() function is used to repeat radial gradients:

    #grad1 <
    height: 200px;
    background-color: #cccccc;
    background-image: repeating-radial-gradient(red, yellow 10%, green 20%);
    >

    Источник

    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.

    Источник

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