Discussion on image path issues in css (same package/different package)

Discussion on image path issues in css (same package/different package)
In CSS files, sometimes you need to use background, that is, add a background image, which is generally used when making button styles.

Adding background images in CSS can be divided into the following types according to the relative position of the image and the CSS file:
1. In the same package: background: url(aaa.gif);
2. Different packages:
In this case, there are two ways to set it. One is to use an absolute path, that is, upload/2022/web/www.iteye.com, but it is generally not recommended and is not conducive to project migration; the other is to use a relative path. First, you need to find a root directory common to the image file and the css file, and then add the subdirectory of the image, for example:
CSS file location: WebRoot/test/css/a.css
Image file location: WebRoot/platform/resource/images/icons/a.gif
If you want to find the common root directory, you need to use "../". This path means the parent directory. If there are two parent directories, it is "../../". Then, according to this writing method, the background image css in a.css should be written like this:

Copy code
The code is as follows:

background:url(../../platform/resource/images/icons/a.gif)

Why?

First, we observe the location of these two files and find that the common root directory is WebRoot.
We first find the WebRoot at the location of a.css, so we have "../../" and then the path of the subdirectory of the spliced ​​image is "platform/resource/images/icons/a.gif". Putting them together is the result above.

<<:  Detailed explanation of HTML style tags and related CSS references

>>:  Detailed explanation of common template commands in docker-compose.yml files

Recommend

Implementation of Vue single file component

I recently read about vue. I found a single-file ...

Summary of 50+ Utility Functions in JavaScript

JavaScript can do a lot of great things. This art...

An Incomplete Guide to JavaScript Toolchain

Table of contents Overview Static type checking C...

Vue implements a complete process record of a single file component

Table of contents Preface Single file components ...

Two box models in web pages (W3C box model, IE box model)

There are two types of web page box models: 1: Sta...

How to install and deploy MySQL 8.0 under CentOS8

MySQL 8 official version 8.0.11 has been released...

Detailed explanation of HTML form elements (Part 1)

HTML forms are used to collect different types of...

Full process record of Nginx reverse proxy configuration

1. Preparation Install Tomcat on Linux system, us...

Summary of Linux nc command

NC's full name is Netcat (Network Knife), and...

A brief discussion on the VUE uni-app development environment

Table of contents 1. Through HBuilderX visual int...