0

i am trying to get access to my static images directory (/static/myapp/images/) from inside of a css file.

i initially tried this:

{% load staticfiles %}

#header {
    position:relative;
    width: 680px;
    height:99px;
    margin-left:29px;
    margin-right:21px;
    background: url({% static 'myapp/images/header.png' %}) no-repeat;
}

but this did not work

this however works fine:

#header {
    position:relative;
    width: 680px;
    height:99px;
    margin-left:29px;
    margin-right:21px;
    background: url(../images/header.png) no-repeat;
}

does Django template language work inside of css files?

or is it just my syntax, and lack of understanding off css! this css comes from a simple template i downloaded, i no nothing about css, is there another way to link an image here that the template language will work with?

thanks!

2 Answers 2

1

css unless directly on the template does not run through django's template renderer. You need to use the url,

STATIC_URL = '/static/'

so mysite.com/static/css/mycss.css

or image depending on how your myproject/static looks like (STATICFILES_DIRS / STATIC_ROOT)

mysite.com/static/images/myimage.png

Sign up to request clarification or add additional context in comments.

Comments

1

CSS files are not processed by the django template system. So your second snippet with relative path is the right way to link to images/fonts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.