Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting background image with Jekyll-Assets

Tags:

css

sass

jekyll

I've searched documentation, stack overflow, Google and attempted every CSS variation I could think of and can't determine a way of setting an image as a background to a div or element tag such as body using CSS. Should be simple, right?

Attempts included:

#element { background-image: url(<% asset_path "image.jpg" %>); }
#element { background: url(<% asset_path "image.jpg" %>); }
#element { background-image: url({% asset-path "image.jpg" %}); }
#element { background-image: {% asset-path "image.jpg" %}; }
#element { background-image: url("image.jpg"); }

And many more. Basically, I've tried every possible variation I could think of including many I didn't expect to work ahem, and my efforts to find an answer have been exhausted.

Can somebody with knowledge of Jekyll and Jekyll-Assets clarify for myself and future Jekyll initiates how to accomplish this task?

like image 612
James Targett Avatar asked Jan 16 '14 06:01

James Targett


People also ask

How can we set the image as background property?

The background-image property in CSS is used to set an image as the background of an element. Using this CSS property, we can set one or more than one background image for an element. By default, the image is positioned at the top-left corner of an element and repeated both horizontally as well as vertically.

How do I add a background image to a container?

Steps to set the background image:Step 1: Add the Container widget. Step 2: Add the decoration parameter (inside Container) and assign the BoxDecoration class. Step 3: Add the image parameter (inside BoxDecoration) and assign the DecorationImage class.


2 Answers

For me, only combination of changing css file extension to css.scss, together with #element { background: url(asset_path("uri/to/file.jpg")); }, necessarily with double quotation marks, worked.

Moreover, sometimes it won't copy the assets, and then I need to jekyll clean and rm -rf .asset-cache.

assets:
    cache: false

in _config.yml may help as well.

like image 38
Antek Avatar answered Sep 19 '22 16:09

Antek


Try #element { background: url(asset_path("image.jpg")); }. It works for me.

like image 107
jupiteror Avatar answered Sep 22 '22 16:09

jupiteror