Welcome to Genhike Sports

CSS Background Image URL

← Back to the tag reference

Hand-written Includes in-depth notes

CSS Background Image URL css_url Used inside components 20 host files / 20 calls

Format an image URL into a form that is safe to write inside url() (no quotes, percent-encoded special characters).

css_url($url)

Pages Site-wide (system) Home List pages Detail pages Single pages

Used in
Site-wide (system) / Home / List pages / Detail pages / Single pages
Defined in
core/tpl_functions.php:569
Core method
(no X::y() forwarding call detected)
How do I read these repository paths?

All paths are relative to the site root. Open core/tpl_functions.php at line 569 to see the definition of css_url(). The "core method" is what the tag actually forwards to: a template writes content_list(...) and the core runs Tpl::contentList(...).

1. Fill in parameters, get the code Edit any field and the result below updates instantly; press Copy and paste it into a template. It also works without JavaScript: press Generate and the server recomputes it.

click the title to collapse / expand
Image URL; returns url(...) already, and none for empty input
Generated code

Now generating: css_url

Real page previewOpen in new window

This is the real rendering of the page (real header, container, this tag output, real footer). It reloads when you change a parameter.

This is the server-computed default; it updates as you edit any parameter and the preview on the right is re-rendered too. It also works without JavaScript: fill the form and press Generate.

2. Full parameter table 1 parameters, one per row; scroll sideways on narrow screens.

click the title to collapse / expand
Parameter Type Default Description
url string required / as needed Image URL; returns url(...) already, and none for empty input

3. Examples Every example can be copied as-is; the later ones are more advanced.

click the title to collapse / expand

Smallest background image

The smallest form: css_url() builds the url() token and e() escapes the whole attribute value.

<div class="ry-hero" style="background-image:<?= e(css_url(theme_asset('assets/img/bg.jpg'))) ?>"></div>

Gradient over an optional image

The usual pattern for an optional background image: fall back to the gradient alone instead of emitting an empty image layer.

<?php
$img      = theme_asset('assets/img/bg.jpg');
$gradient = 'linear-gradient(180deg, rgba(3,17,44,.62) 0%, rgba(3,17,44,.38) 100%)';
$bg       = $img !== '' ? $gradient . ', ' . css_url($img) : $gradient;
?>
<section class="ry-banner" style="background-image:<?= e($bg) ?>">
    <h2 class="ry-banner__title"><?= e($item['title']) ?></h2>
</section>

Reuse through a custom property

Store the prepared token in a custom property and consume it with background-image:var(--ry-hero-bg), which keeps one computed value for several rules.

<?php $img = theme_asset('assets/img/hero.jpg'); ?>
<div class="ry-hero" style="--ry-hero-bg:<?= e(css_url($img)) ?>">
    <div class="ry-hero__inner"><?= e($item['title']) ?></div>
</div>

4. What it does / when to use it

Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and the image layer stacked on top of a linear-gradient(). It returns a finished url(...) token, unquoted, with the characters that would break CSS (spaces, quotes, brackets, angle brackets) percent-encoded, so escaping the whole property value with e() cannot damage it. Do not use it for HTML attribute URLs such as &lt;img src="..."&gt;, where plain e() output is correct, and do not use it in CSS files or selectors.

5. Which pages it belongs on This is the question clients actually ask: "I am building the home page / a list page / a detail page — which tags do I use?"

click the title to collapse / expand
Page What that page is What this tag does there
Site-wide (system) Header, footer, head, styles and scripts — every page template needs them It is a system tag written once per page template, inside the page shell (head, header, footer, scripts).
You use it to: Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and
Home Home page: hero, category blocks, latest content On the home page it usually sits in the hero, a category block, or a latest/featured section.
You use it to: Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and
List pages Category list pages: listing, filters, pagination On a list page it lives in the list container or right next to the pagination bar.
You use it to: Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and
Detail pages Content detail pages: body, specs, related items On a detail page it goes in the body area or in the related-items sidebar.
You use it to: Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and
Single pages One-off pages such as About or Contact On a single page (About, Contact) it sits inside the content block.
You use it to: Use it wherever an image URL goes into a CSS background-image: banners, hero sections, card covers, section textures and

Clicking a page name jumps to every tag for that page in the reference (the "By page" filter there). This page always shows everything about the tag, whatever that filter is set to.

6. How to use it (step by step)

  1. Get the image URL from wherever it lives, whether that is a theme asset, a component config value or a content field, and keep it in a variable such as $img. Relative and absolute addresses both work, because the function only formats a string.
  2. Hand that variable to css_url(). The result is the whole url(...) token rather than a bare address, so never wrap it in another url().
  3. Escape the complete background-image value with e() when printing it, as in background-image:&lt;?= e(css_url($img)) ?&gt;. The value contains quotes, commas and brackets, and because the encoded token holds no quotes, escaping it stays safe.
  4. Stack a gradient over the image by concatenating the gradient first: $bg = $gradient . ', ' . css_url($img);. The other order puts the image layer on top and hides the gradient.
  5. Handle the optional case explicitly when an image may be missing. css_url('') returns none, not an empty string, so test the address you were given instead of the result.

7. Common pitfalls

  • Writing url('&lt;?= e($img) ?&gt;') and escaping the whole thing. The single quotes become &amp;#039;, the browser receives invalid CSS, and the background silently disappears with no page error and no log entry. Use css_url() and add no quotes of your own.
  • Wrapping the return value in url() again, as in url(&lt;?= e(css_url($img)) ?&gt;). That produces url(url(...)) and the background still never shows. The function already returns the full token.
  • Using it to test whether an image is configured. An empty address comes back as none rather than an empty string. Test the address you pass in, or keep the gradient in a separate fallback branch.
  • Expecting it to check that the file exists. It only formats a string, and a 404 image is a separate problem that works the same with a CDN address. When a background does not show, read the computed CSS in devtools first, then open the image URL.
  • Reversing the order when combining an image with a gradient, as in css_url($img) . ', ' . $gradient. The image layer sits on top and hides the gradient, which looks like the gradient never applied.

Note from the reference table The sheet's example wraps the result in a second url(), which produces invalid CSS. css_url() already returns the complete value (unquoted, percent-encoded), so print it directly after background-image:.

8. Real rendered output This section was rendered once on the server with this site real data; it is not a mock-up.

click the title to collapse / expand

It belongs to the document <head>: rendered on its own it is just a set of references that only make sense inside a <head>. Dropping it into this preview would show you raw <link> / <script> source and nothing usable. Copy it into your page template instead (right after <head>).

9. Where it already appears in your templates Grouped by components / page templates / template packs; the data comes from a script scan.

click the title to collapse / expand

Components 20 files / 20 calls 89% of tag calls live in components

  • templates/_shared/components/banner-3d-rotate.php ×1 lines 330
  • templates/_shared/components/banner-aurora.php ×1 lines 459
  • templates/_shared/components/banner-cards.php ×1 lines 93
  • templates/_shared/components/banner-coverflow.php ×1 lines 320
  • templates/_shared/components/banner-fade-center.php ×1 lines 200
  • templates/_shared/components/banner-fade-side.php ×1 lines 197
  • templates/_shared/components/banner-fullscreen.php ×1 lines 224
  • templates/_shared/components/banner-glass.php ×1 lines 91

Click "See this code + preview": the left shows the real code around that call site (±12 lines, call line highlighted) and the right renders a real page preview with the current parameters.

11. Previous / next In group order; this is tag 55 of 56.

click the title to collapse / expand

At a glance

css_url

Site-wide (system) / Home / List pages / Detail pages / Single pages

← Back to the tag reference

Send an Enquiry