Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to static files in my css files?

I have a reference inside my CSS file that refers to a static image:

#logo {   background: url('/static/logo.png') } 

This works just fine on my dev machine but not on my production environment since the url should be static.mydomain.com/logo.png. How do I dynamically change the css file according to the STATIC_URL in my settings file?

like image 712
the_drow Avatar asked May 05 '11 13:05

the_drow


People also ask

Where do I put static files?

Configuring static files Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg .

Is CSS a static file?

Some CSS can be added to add style to the HTML layout you constructed. The style won't change, so it's a static file rather than a template. Besides CSS, other types of static files might be files with JavaScript functions, or a logo image.

How do I use a static file?

Serve static files Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root. The preceding code was created with the web app template.


2 Answers

Use a relative path. Relative to the folder where the css file reside

like image 82
Riccardo Galli Avatar answered Oct 18 '22 00:10

Riccardo Galli


You can move any CSS that contains static file paths to inline CSS, contained in the template.

i.e.

<div style="background: url('{% static 'logo.png' %}')"></div> 

The catch here is that it won't work for @media queries, you'd need to put those in a block, e.g.

<style>     @media (min-width: 1200px){        background: url('{% static 'logo.png' %}');     } </style> 
like image 35
Nexus Avatar answered Oct 17 '22 23:10

Nexus