有没有办法为 css 中使用的图像设置默认目录?
我有一个 css 文件,出于组织原因,该文件位于图像所在目录的另一个目录中。问题是我总是最终像这样设置所有背景图像:background-image: url(../img/Untitled.jpg)
。 有类似 HTML 的
标记的东西可以在 css 中使用,所以我不需要将“../img/
”放入每个 <代码>背景图像?
I have a css file which, for organizational sake, is in another directory from the one where the images are. The thing is I always end up setting all the background-images like this: background-image: url(../img/Untitled.jpg)
.
There's anything like the HTML's <base>
tag that can be used in css so I don't need to put that "../img/
" to every single background-image
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一个可能的选项是创建您的 使用 php 或其他服务器的 css 样式副语言。然后,您可以使用目录变量来输出每个项目的完整路径。
styles.php
Another possible option would be to create your css styles using php or another server side language. You can then use a directory variable to spit out the full path for each item.
styles.php
不,恐怕您会被这种语法所困扰。
编辑:您可能想查看 http://lesscss.org/。使用变量,您至少可以在顶部定义所有网址。
No, I'm afraid you are stuck with that syntax.
EDIT: You might want to check out http://lesscss.org/. With variables you could at least define all of the urls at the top.
URL 重写是你的朋友。
CSS 图像引用始终与 CSS 文件的 URL 相关。假设我们的 CSS 文件位于
/css
内,并且我们希望图像资源位于/img
内,并且我们厌倦了必须将../img/
在每个图像引用前面。所有相对 CSS 图像引用都是相对于包含该引用的 css 文件的 URL 而言的。设置 URL 重写规则以重写类似
/css/foobar.png
/css/blue-theme/header-background.png
的 URL ,以便它们相对于所需位置(
/img
):/img/foobar.png
/img/blue-theme/header-background.png
瞧< /em>!问题解决了。
您可以使用 Apache 的 mod_rewrite 来完成此类操作。应该与将以下内容添加到相应的
.htaccess
一样简单。在 IIS 上更困难,但概念保持不变。
URL rewriting is your friend here.
CSS image references are always relative to the URL of the CSS file. Let's assume that our CSS files live inside the
/css
and that we want our image resources to live inside/img
and that we're tired of having to put../img/
in front of every image reference.All relative CSS image references are relative with respect to the URL of the css file containing the reference. Set up URL rewrite rules to rewrite urls like these
/css/foobar.png
/css/blue-theme/header-background.png
so they are relative to the desired location (
/img
):/img/foobar.png
/img/blue-theme/header-background.png
Et voila! Problem solved.
You can do this sort of this with Apache's mod_rewrite. Should be about as simple as adding the following to the appropriate
.htaccess
.More difficult on IIS, but the notion remains the same.
除非 css 与图像位于同一目录中,否则没有真正的解决方案。
但正如您在问题中所说,情况并非如此,因此显示图像位置的唯一方法是给出每个
background-image
或静态图像的相对位置(使用http://...
)There is not really a solution to that unless the css is in the same directory as the images.
but as you say in the question that is not the case, so the only way to show where the images are is give the relative location for each
background-image
or the static image (with thehttp://...
)您可以做的一件事是,您始终可以从域根目录下来,而不是使用相对路径向上移动。这个 URL 看起来更像
url(/img/Untitled.jpg)
并且在所有 CSS 文件中都是相同的,无论它们在域下嵌套的深度如何。One thing you can do is rather than going up and over with relative paths, you can always come down from the domain root. This URL would look more like
url(/img/Untitled.jpg)
and it would be the same in all CSS files regardless of how deeply nested they are underneath the domain.您可以在页面加载后替换“路径”...
超时是人为的,只是为了在操作中显示它
https://jsfiddle.net/z52v76e9/2/
在样式表中替换时,您可以使用 head 而不是 body。
You could replace "paths" after the page loads ...
The timeout is artificial just to show it in action
https://jsfiddle.net/z52v76e9/2/
When replacing in stylesheets you can use head instead of body.