【问题标题】:loading static files inside a css file in Django在 Django 中的 css 文件中加载静态文件
【发布时间】:2015-09-11 01:51:54
【问题描述】:

我有一个 css 文件,我想在其中导入另一个 css 文件。 我如何在 Django 中做到这一点?

这是我的 style.css 文件,我想在其中导入 owl.carousel.css。

@import url("owl.carousel.css");

body {
    margin: 0;
    padding: 0;
    color: #34495E;
    font-family: 'Source Sans Pro', sans-serif;
    font-size: 14px;
    line-height: 21px;
    position: relative;
    background: #fff;
}

我在我的模板上使用 {% load static %} 来链接 style.css 但如何在 css 中导入 css ?

<link href="{% static "assets/css/style.css" %}" rel="stylesheet"> 

【问题讨论】:

    标签: html css django django-templates


    【解决方案1】:

    您还可以使用 HTML 样式和脚本标签在静态文件中加载内容。

    <style>
        @import url("{% static 'path/to/your/file.css')")
    </style>
    

    【讨论】:

      【解决方案2】:

      如果您想在.css 中导入.css 文件,您需要像往常一样使用路径。

      @import url('/path/to/your/file.css')
      

      如果它们在静态文件夹中:

      static/styles/file1.css
      static/styles/file2.css
      
      /* in file2.css */
      @import url('file1.css')
      

      不能在.css 文件中使用 django 模板语言。

      【讨论】:

        猜你喜欢
        • 2020-04-28
        • 2022-01-20
        • 1970-01-01
        • 2020-08-31
        • 2017-10-08
        • 2017-05-22
        • 2015-04-25
        • 2011-11-26
        • 1970-01-01
        相关资源
        最近更新 更多