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?
Configuring static files Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg .
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.
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.
Use a relative path. Relative to the folder where the css file reside
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With