# Background Image Inline CSS URL

**URL:** <https://forum.getkirby.com/t/background-image-inline-css-url/10058>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 22, 2018, 10:54am UTC](https://forum.getkirby.com/t/background-image-inline-css-url/10058 "2018-03-22T10:54:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fleix](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fleix/32/8261_2.png) [@Fleix](https://forum.getkirby.com/u/Fleix)\
**Post date:** [March 22, 2018, 10:54am UTC](https://forum.getkirby.com/t/background-image-inline-css-url/10058/1 "2018-03-22T10:54:03Z")

</div>

Hi There!

I got into Kirby recently and I’m in love with it’s simplicity and thorough documentation.  
However, I have a little problem: I wan’t to pull the background image from a field and place it in an inline css style. I did it like that:

`<div class="test" style="background-image: url(<?php echo $page->background()->url()?>) no-repeat">Test</div>`

Unfortunately the background is not shown, and oddly the rendered URL seems to miss some directories, it renders:

`/main/background-image.jpg`

instead of:  
`main/content/home/background-image.jpg`

I know similar questions have been answered but it didn’t work for me.  
What am I doing wrong?

Best, Felix

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 22, 2018, 11:22am UTC](https://forum.getkirby.com/t/background-image-inline-css-url/10058/2 "2018-03-22T11:22:45Z")

</div>

You need to put it through `toFile()` first:

```auto
<div class="test" style="background-image: url(<?= $page->background()->toFile()->url()?>) no-repeat">Test</div>

```

You should also check the image physically exists first, or you will get an error…

```auto
<?php if($bgimage = $page->background()->toFile()): ?>
    <div class="test" style="background-image: url(<?= $bgimage->url()?>) no-repeat">Test</div>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![Fleix](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fleix/32/8261_2.png) [@Fleix](https://forum.getkirby.com/u/Fleix)\
**Post date:** [March 22, 2018, 1:39pm UTC](https://forum.getkirby.com/t/background-image-inline-css-url/10058/3 "2018-03-22T13:39:56Z")

</div>

Cool, this solved my issue! Thanks for the quick reply!

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:28pm UTC](https://forum.getkirby.com/t/background-image-inline-css-url/10058/4 "2025-02-06T17:28:35Z")

</div>

Archived on February 6, 2025, 5:28pm UTC


