rhcarvalho

rhcarvalho

Hi,

In the past when I needed to set the background image on an element I gave up Verified Routes and wrote something like bg-[url(/images/background.svg)] in my template. The Tailwind compiler then generates a CSS class that sets the background to the image.

I found myself in need of doing it again, and since I couldn’t find an existing thread on using TailwindCSS + VerifiedRoutes I decided to ask – is there a way to use both together?

I tried this but it doesn’t work, because I understand the Tailwind compiler sees Elixir code as is and not the compiled string after the ~p sigil is processed:

<div class={"bg-[url(#{~p"/images/background.svg"})]"}>...</div>

Another idea, which I did not try, would be to add some kind of plugin in tailwind.config.js similar to how Heroicons are made available. I thought perhaps someone in the community has an easier way to offer?

Showing Posts 1 to 9

codeanpeace

codeanpeace

Hmm, wonder if the nested double quotes are the problem…

class={"bg-[url(" + ~p"/images/background.svg" + ")]"}

class={"bg-[url(#{~p'/images/background.svg'})]"}

Just to rule that out, I’d give those a shot ¯\_(ツ)_/¯

rhcarvalho

rhcarvalho OP

Tailwind only looks at the text of the files, without any further interpretation: Detecting classes in source files - Core concepts - Tailwind CSS, so quoting differences don’t matter.

rhcarvalho

rhcarvalho OP

There are a few possibilities listed in Detecting classes in source files - Core concepts - Tailwind CSS.

I could:

Considering that Verified Routes for static paths don’t really verify that the full path exists in disk, but only that the path is well-formed, then I think the possibilities above are not worth it – simply don’t use a verified route for cases like in my OP.

I will feel good if other people share that’s what they do :slight_smile:

harmon25

harmon25

My solution to a similar problem was to use css variables and to extend tailwind background image:

{
 extend: {
    backgroundImage: (theme) => ({
      custom: "var(--bg-image)"
    })
  }
}

this requires setting the --bg-image css variable to url(/some/image.png) which can be done with an inline style, kinda like this:

<div style={"--bg-image: #{url}"} class="bg-custom">
 something here
</div>
rhcarvalho

rhcarvalho OP

A bit of going on a tangent, but something I learned recently is that [...] can also be used to set CSS variables:

This can be useful for things like CSS variables as well, especially when they need to change under different conditions:

<div class="[--scroll-offset:56px] lg:[--scroll-offset:44px]">
 <!-- ... -->
</div>

So that could be used to set --bg-image conditionally, but if the value would be url(...) then again the URL needs to appear as-is within the parentheses or back to Tailwind not picking up the value to generate CSS classes.

arcanemachine

arcanemachine

Just spitballing here, but how about just adding it to the safelist since it’s just the one URL?

LostKobrakai

LostKobrakai

<div class="bg-[url:var(--bg-image)]" style="--bg-image: url(…)">

That one works. The style can be dynamically set in that case.

More details: Adding custom styles - Core concepts - Tailwind CSS

rhcarvalho

rhcarvalho OP

:clap:

I like how you put those ideas together :heart_decoration:

Did you actually reach out for that in existing projects? I’m still finding verified routes may not be worth the trouble in that case.

LostKobrakai

LostKobrakai

I did use that approach before, though with colors not urls.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews