简体   繁体   English

HTML和CSS文件中的URL路径

[英]URL Paths in HTML and CSS files

Maybe title of this question is a little bit confusing to you but i will explain in detail. 也许这个问题的标题会让您有些困惑,但是我会详细解释。

Imagine you are developing a site (PHP SCRIPT) which has images, js files and background image urls in css. 假设您正在开发一个站点(PHP SCRIPT),该站点在CSS中包含图像,js文件和背景图像URL。 You need to make it work if user who purchased it put it in subfolder like: 如果购买它的用户将其放在子文件夹中,则需要使其工作:

www.example.com/subfolder

or when he put it in www.example.com 或当他将其放在www.example.com

If I use path for background image in my CSS like this: 如果我像这样在CSS中使用背景图片的路径:

background: url(/assets/img/thumbs-up.png) no-repeat;

This will work if user place script in www.example.com but if he places in www.example.com/subfolder , browser will be requesting www.example.com/assets/img/thumbs-up.png instead of www.example.com/subfolder/assets/img/thumbs-up.png 这将如果用户的地方脚本工作, www.example.com ,但如果他在的地方www.example.com/subfolder ,浏览器会请求www.example.com/assets/img/thumbs-up.png代替www.example.com/subfolder/assets/img/thumbs-up.png

This is usually solved using this 通常使用此方法解决

background: url(assets/img/thumbs-up.png) no-repeat;

But no, in my case the browser is requesting this url instead 但是不,在我的情况下,浏览器正在请求此网址

http://www.example.com/subfolder/assets/css/assets/img/thumbs-up.png

This part, assets/css is where CSS file is located. 这部分, assets/css是CSS文件所在的位置。

I fixed this by using 我通过使用解决了这个问题

background: url(../../assets/img/thumbs-up.png) no-repeat;

This works in root www.example.com and www.example.com/subfolder But is it a good practice? 这适用于根目录www.example.comwww.example.com/subfolder但这是一个好习惯吗?

For JS files, I include script like this 对于JS文件,我包括这样的脚本

<script src="assets/js/single_load.js"></script>

This works because file having this code is located in root of the script. 这行得通,因为具有此代码的文件位于脚本的根目录中。 ( http://www.example.com/index.php or http://www.example.com/subfolder/index.php is calling it, if a file in subfolder called JS i think I would have to add ../ many times depending in how many subfolders file is located) http://www.example.com/index.phphttp://www.example.com/subfolder/index.php正在调用它,如果子文件夹中的文件名为JS,我想我必须添加../多次,具体取决于子文件夹文件的位置)

Any thoughts, guides on this? 有什么想法,指导吗?

Use one path for your CSS file in all PHP scripts. 在所有PHP脚本中,为您的CSS文件使用一个路径。

The path to image will be located from your CSS path, not PHP 图像的路径将位于您的CSS路径中,而不是PHP

For example: 例如:

- index.php
- subfolder
    --index.php
-assets
    --css
        ---style.css
    --img

style.css style.css文件

background: url(../assets/img/thumbs-up.png) no-repeat;

config.php config.php文件

<?php
$root_path = "www.example.com";
//OR $_SERVER["REQUEST_URI"]; if you have one index.php

root index.php 根index.php

<?php include "config.php"; ?>
<link href="<?php echo $root_path;?>/assets/css/style.css">

subfolder/index.php 子文件夹/ index.php文件

<?php include "config.php"; ?>
<link href="<?php echo $root_path;?>/assets/css/style.css">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM