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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
perzanko
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews