banner



How To Set An Image As A Background In Css

Set Background Image in CSS

  1. Use the background:url() to Set the Background Image in CSS
  2. Use the Relative Path /image/motion-picture show to Fix the Background Prototype in CSS
  3. Utilize the Relative Path ../image/picture to Set the Groundwork in CSS

This article introduces methods to set background images in CSS. We will discuss how to fix the path of the image so that the groundwork epitome correctly shows upwards. Furthermore, we will discuss the unlike relative paths.

Utilise the background:url() to Set up the Background Image in CSS

We can use the background holding to fix the background image in CSS. The URL() role volition hold the image'south file path equally the background image. The background property is the shorthand of several other properties associated with the groundwork. Those properties in order are groundwork-color, groundwork-image, background-repeat, background-attachement and groundwork-position. Using the background property, we can assign the values for all these backdrop in a brusque fashion.

The criticl indicate where nosotros tin get incorrect is while writing the path of the paradigm in the URL() function. We should be enlightened of the location of the image, and the path that is written in the URL() function should be relative to the CSS file. If we write the CSS in the HTML file itself, the path should exist relative to the HTML file.

In the example below, the paradigm named pic is inside the image binder. The image folder and the following CSS file style.css is in the same folder. Therefore, the following code volition set the groundwork epitome.

The folder structure is shown beneath:

              web ├── manner.css ├── HTML │ └── test.html └── image  └── moving-picture show                          

Instance Code:

              body {     background: url(image/picture); }                          

Use the Relative Path /paradigm/picture to Set the Background Epitome in CSS

Nosotros can use the /image/moving picture file path format to ready the background paradigm in CSS. In such a format, the image is located inside the image folder. The paradigm folder is located at the root of the electric current web.

In the example beneath, we have set the picture image as the background epitome. The location of the CSS file does not matter in this format. Nosotros practise not need to write the file path relative to the CSS file. The file path is written relative to the root of the current spider web. It means that the image binder lies in the root directory.

Example Code:

              body {     groundwork: url(/image/picture); }                          

Use the Relative Path ../epitome/picture to Set the Groundwork in CSS

We can move back to 1 level in a directory construction using the ../ annotation. The ../ note tin be used to write the relative file path in the URL() part while setting the groundwork in CSS.

For instance, nosotros have an HTML file examination.html in the html binder. The html folder lies inside the spider web binder. The image picture lies inside the prototype folder inside the web folder. In such a directory structure, we can use the following code to ready the background.

The folder construction is shown beneath:

              web ├── CSS ├── HTML │ └── exam.html └── epitome  └── motion picture                          

In the example beneath, nosotros have written the CSS in the HTML file itself. The ../ notation takes u.s.a. back to the web binder from the html folder. So it finds the image folder and then the motion-picture show file inside the image binder. In this mode, we can set the background prototype in CSS using the relative path.

Example Code:

              body {     groundwork: url(../image/picture); }                          

Write for united states of america

DelftStack manufactures are written by software geeks like y'all. If you besides would like to contribute to DelftStack by writing paid articles, y'all can check the write for u.s. page.

Related Article - CSS Groundwork

  • Transparent Background Color in CSS
  • Center Groundwork Images in CSS
  • Darken Groundwork Image in CSS
  • Add Background Image in CSS
  • Ezoic

    How To Set An Image As A Background In Css,

    Source: https://www.delftstack.com/howto/css/background-image-not-showing-css/

    Posted by: christensenevisold.blogspot.com

    0 Response to "How To Set An Image As A Background In Css"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel