Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Image url in css theme

Solved by Karmalakas View solution

Started by mmm 4 years ago · 13 replies · 1577 views
4 years ago

Hi,
this could be asked many times but didn't find... sorry about that.
I inherited quark theme and in my custom theme want to set background image for a page.
In my theme i have images folder with my xy.jpg image, in my css/custom.css i tried to set:
background-image: url('images/xy.jpg');
background-image: url('/images/xy.jpg');
background-image: url('theme://images/xy.jpg');
background-image: url('{{theme://images/xy.jpg}}');

non of these works. The only one that works is
background-image: url('../images/xy.jpg');

but i prefer something without relative path

is this available?

Many thanks

4 years ago

hi,

{% if page.header.header_image %}
{% set style = ' style="background-image: url(\'' ~ page.media.images[page.header.header_image].cache.url() ~ '\')"' %}
{% elseif theme_config.default_header_image %}
{% set style = ' style="background-image: url(\'' ~ url('theme://img/' ~ theme_config.default_header_image) ~ '\')"' %}
{% else %}
{% set style = '' %}
{% endif %}

it's work, my environtment using windows 10 with laragon.

4 years ago

yes, this work in twig file, but not in css file. I have a css

4 years ago

@backit:
background-image: url(‘/images/xy.jpg’);

This must work if /images is on your web root. Eg. if image is at example.com/some/stuff/images/image.jpg and CSS is at example.com/css/path/styles.css, then url('/some/stuff/images/image.jpg') must work

4 years ago

thank you @Karmalakas , but images is in my theme's path as css too:
grav-admin/user/themes/mytheme/images/xy.jpg
grav-admin/user/themes/mytheme/css/custom.css
i thought in css something like
background-image: url(‘images/xy.jpg’);
should work, but it's not...

4 years ago

So doesn't url('/user/themes/mytheme/images/xy.jpg') work?

4 years ago

did not try, but theme should have some shortcut to find resources inside theme, isn'it?

4 years ago

It does.. On server side (PHP, Twig). CSS is processed on client side and PHP streams or Twig vars won't work there

4 years ago

and which should work?
/images/xy.jpg
or
images/xy.jpg
?

4 years ago

/ means path is from web root. Without - path is from css location

4 years ago

i know, i thought grav or webserver knows how to interpret that url. Twig variables don't work in scss too?

4 years ago

/forum/general/image-url-in-css-theme-t4935?u=karmalakas

4 years ago

@Karmalakas:
/forum/general/image-url-in-css-theme-t4935?u=karmalakas
It does… On server side (PHP, Twig). CSS is processed on client side and PHP streams or Twig vars won’t work there

scss too?

DON'TCARETHISi must type more than 20 chars, so here it is...DONTCARETHIS

last edited 09/30/22 by mmm
4 years ago Solution

I really doubt any SCSS/SASS compiler supports Twig. And I don't really see how it would. Unless you're compiling to CSS on every single page load, so that it could check variables and use them.

👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 16 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 5 days ago
3 43 5 days ago