简体   繁体   English

在CSS中控制相对URL

[英]Controlling relative URLs in CSS

I have my product pages arranged like this: 我的产品页面排列如下:

home_directory/products/product_one/  
home_directory/products/product_two/  

etc 等等

Each directory has its own "index.html," "first_image_small.jpg," "first_image_large.jpg," "second_image....you get the idea. 每个目录都有自己的“ index.html”,“ first_image_small.jpg”,“ first_image_large.jpg”,“ second_image ....”。

I'm using CSS "background-image" and Media Queries so that those images will switch out for screen size both "onLoad" AND when someone flips portrait/landscape or resizes their browser. 我正在使用CSS“背景图像”和媒体查询,以便这些图像将在“ onLoad”和有人翻转纵向/横向或调整浏览器大小时切换为屏幕大小。 (by the way, please check out this question if you have an idea how to do that in JavaScript because the only answer so far is "onLoad"-only). (顺便说一下,如果您有一个如何在JavaScript中执行此操作的想法,请检查此问题 ,因为到目前为止唯一的答案是“仅onLoad”)。

Anyway, according to this question , if I try to link each "index.html" to a single stylesheet it will look for the images relative to the stylesheet's directory instead of each "index.html"s (and yes, I understand why it needs to be that way). 无论如何,根据这个问题 ,如果我尝试将每个“ index.html”链接到一个样式表,它将查找相对于样式表目录的图像,而不是每个“ index.html”的图像(是的,我知道为什么会这样)需要这样)。

The obvious solution is to duplicate the stylesheet in each product directory, but that's kind of a mess for maintenance. 显而易见的解决方案是在每个产品目录中复制样式表,但这对于维护来说是一团糟。 Any more elegant solutions? 还有更好的解决方案吗?

it does not seem to be possible to have relative url's in css use another base path than the css file's path. css中的相对url似乎不可能使用css文件路径以外的其他基本路径。 the specification states: 规范指出:

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document 部分URL是相对于样式表的源而不是相对于文档来解释的

however if you do not want to use any serverside scripting or javascript you can symlink the css files on your server. 但是,如果您不想使用任何服务器端脚本或javascript,则可以在服务器上符号链接css文件。 this will allow you to edit the file at only one place: 这将允许您仅在一个位置编辑文件:

assuming your directory structure and the css at: home_directory/style.css . 假设您的目录结构和CSS位于: home_directory/style.css do the following on the unix command line: 在UNIX命令行上执行以下操作:

cd home_directory
ln -s style.css products/product_one/
ln -s style.css products/product_one/

you will have created symbolic links to the original file. 您将创建到原始文件的符号链接。 the browser will be able to access them from the different paths. 浏览器将能够从不同路径访问它们。

if you are using an apache webserver it might be necessary to allow for symlinks. 如果您使用的是Apache Web服务器,则可能需要允许符号链接。 for this add the following line to the configuration or .htaccess 为此,将以下行添加到配置或.htaccess

Options +FollowSymLinks

alternatively you can setup a server rewrite rule that will serve the css file from any path, without the need for symbolic links. 或者,您可以设置服务器重写规则,该规则将从任何路径提供css文件,而无需符号链接。 documentation for apache can be found here . apache的文档可以在这里找到。

If using Less / Sass is allowed, there is a really straight forward solution: import the css file with Less/Sass. 如果允许使用Less / Sass ,则有一个非常简单的解决方案:使用Less / Sass导入css文件。


Let's see how it would be done using Less. 让我们看看如何使用Less来完成。 Assuming that your folder structure looks like this: 假设您的文件夹结构如下所示:

  • home_directory home_directory
    • css ( your common css file goes here ) CSS( 您常用的CSS文件在此处
    • products 制品
      • product1 产品1
        • css CSS
        • images 图片
      • product2 产品2
        • css CSS
        • images 图片

For your common css file (located in home_directory/css ): 对于您的通用CSS文件(位于home_directory / css中 ):

  • All the styles related to products will have relative paths. 与产品相关的所有样式都有相对路径。 Eg: background:url("../images/pic.jpg"); 例如: background:url("../images/pic.jpg");
  • Change the extension from .css to .less (I don't know if this is necessary, but I had problems if I kept the extension as .css) 将扩展名从.css更改为.less(我不知道这是否有必要,但是如果我将扩展名保留为.css会遇到问题)
  • When you import the .less file use rel="stylesheet/less" 导入.less文件时,请使用rel="stylesheet/less"

For the products css file (located in home_directory/products/productX/css ): 对于产品css文件(位于home_directory / products / productX / css中 ):

  • The Less file will have the same content for all the products. Less文件将对所有产品具有相同的内容。
  • It will have only one line of code (assuming that your common css file is styles.less): 它将只有一行代码(假设您的公共css文件为styles.less):

      @import "../../../css/styles.less"; 

...And basically, that's it. ...基本上就是这样。 You don't need to change the content of the product stylesheets, just the common css file and you are done. 您不需要更改产品样式表的内容,只需更改通用的CSS文件即可。

Note: You'll have to compile the styles with the Less compiler and it will automatically generate the CSS for you. 注意:您必须使用Less编译器来编译样式,它会自动为您生成CSS。 Or if you don't like the compiler, you could use the less.js file (although it's only recommended for testing and not for production) 或者,如果您不喜欢编译器,则可以使用less.js文件(尽管仅建议将其用于测试而非生产)

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

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