Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Dark mode with JS script and → images / pictures

Solved by Medi Spiegelberg View solution

Started by Medi Spiegelberg 4 years ago · 12 replies · 1410 views
4 years ago

Hello Gravpals,

Currently I'm watching some tutorials on how to place dynamic html <picture> tags with the ability to change the src attribute according to some conditions (CSS attributes in my case). My goal is to change several .png logos on a page depending on the background (i.e. light/dark mode), so they alternate between their light or dark version...

If you were to implement that in Grav, how would you do it ?
I guess I can always use html inside the content field, the classic way.
But I was maybe hoping to get some help from experienced people !

Thanks and cheers !
(here's a link to the page... I'm about to add 2 more logos on it : https://1800trucs.ch/partenaires)

EDIT: Also I just wanted to add that I'm working on a credit page to thank GRAV and Hypertext theme (parent of my theme called ALOT - a library of things) + all the people involved. It's on its way !

last edited 07/07/22 by Medi Spiegelberg
4 years ago

@medi, If you have a js script that runs when switching between dark/light theme, why not have js also search for <picture> and change its src attribute?

4 years ago

Good call. Thanks !

But in fact GRAV doesn't generate a <picture> tag when calling ![file.jpg](file.jpg) in content.
Instead it's the classic “img” tag.

last edited 07/08/22 by Medi Spiegelberg
4 years ago

@medi, Maybe I'm a bit confused by your question...

Do you want to know (loosely rephrased)
"How to change the src attribute using JS" or "How to generate a <picture> tag in Markdown", or do you maybe want to know both?

👍 1
4 years ago

Thanks a lot @pamtbaau for the rephrasing.
It's very helpful and so I would like to know how to generate a <picture> in Markdown, by using the common ![image.png](image.png) and finally making it interact with JS in order to add -dark in the file name.

Thank you for your time !
Take care

4 years ago

@medi, Do you need Art Direction that requires the use of element <picture>?

4 years ago

I'm discovering this "Art Direction" help to you right now.
It isn't necessary for now. And could be useful in the future !

In any case, I'll check that and implement stuff that can only add up to GRAV as an "ecosystem".

4 years ago

@pamtbaau:
@medi, If you have a js script that runs when switching between dark/light theme, why not have js also search for <picture> and change its src attribute?

The script is active on the whole website... but I only want to "negate" the pngs on the wall of logos. This is why I was thinking that <picture> tag is best because it interacts with CSS and has a fallback source if the [filename]-dark.png isn't found in the page media.

Is it a plausible explanation ?

4 years ago

@medi:
<picture> tag is best because it interacts with CSS

Maybe my knowledge is outdated, but doesn't it interact with CSS exactly the same way any other element does - via style and/or class attributes?

4 years ago

@Karmalakas:
it interact with CSS exactly the same way any other element does - via style and/or class attributes

Apparently you can change the scr attribute according to CSS conditions.
In my case I would like GRAV to load the dark src depending on : prefers-color-scheme and parent is .dark blabla ← I'm planning to look up this part later.

Thanks for your question

source: https://reactgo.com/dark-mode-change-image/

4 years ago

Here's what I would come up in the final output :

TXT
<picture>
  <source srcset="dark-image.jpg" media="(prefers-color-scheme: dark)">
  <img src="light-image.jpg" alt="logo of blabla">
</picture>

But I don't find how to do it in GRAV.

4 years ago

@medi:
Apparently you can change the scr attribute according to CSS conditions.

But CSS (which you have to write yourself) has nothing to do with it and <picture> doesn't have src attribute at all. Although it may contain <img> tag, which supports src attribute, but this doesn't change depending on the viewport - it only serves as a fallback. The only magic thing here is a srcset attribute, which resolves one of provided values depending on the viewport, but again - it has absolutely nothing to do with CSS

4 years ago Solution

In the end I'm gonna use CSS

TXT
-webkit-filter : invert(.5)
filter : invert(.5) 

That's much less work

Thank you all for your time !

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 224 2 months ago
Themes & Styling · by Ian, 2 months ago
3 118 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 481 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 72 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 152 3 months ago