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.

Support

Need Help with Implementing Responsive Images in Grav CMS

Solved by pamtbaau View solution

Started by 01K 3 years ago · 8 replies · 609 views
3 years ago

Hello,
I'm looking for assistance with implementing responsive images in my Grav CMS site. I have two versions of images: 'horizontal.jpg' for desktop and 'vertical.jpg' for mobile devices.
Additionally, I'm using the image map where coordinates differ depending on which image to display.
I want to use Grav's built-in functionality to achieve the following:

  1. Display 'horizontal.jpg' for desktop devices with appropriate coordinates.
  2. Display 'vertical.jpg' for mobile devices with appropriate coordinates.

Here is the original code of the image with coordinates which is displayed on all devices:

TWIG
<img loading="lazy" title="image title" usemap="#image-map" alt="my alt" src="{{ theme_url }}/images/001-full.jpg" style="object-fit: contain;width: 100%;height: 100%;display: block;">

<map name="image-map">
    <area target="" href="#" coords="529,511,4,0" shape="rect">
    <area target="" href="#" coords="1064,512,535,3" shape="rect">
    <area target="" href="#" coords="1072,512,1599,1" shape="rect">
</map>   

Or, maybe it's not possible to achieve the desired results with built-in Grav functionality?

3 years ago Solution

@01K, I'm afraid Grav won't help you with this...

  • Grav can help you with presenting images (<img>) with different resolutions, but not with art direction (eg. horizontal vs vertical cropped images).
  • Nor does Grav help you with maps (<map>).
3 years ago

Understood,
probably I need a CSS code, which will replace the horizontal image with vertical on mobile device and JS code to inject map code...

3 years ago

@01K

probably I need a CSS code, which will replace the horizontal image with vertical on mobile device

I'm afraid that won't work. Using css you can only set the background image of an element and <map> will only work together with <img src="..." usemap="...">

You might try adding two maps using Twig and use js to inject/update an <img> with the proper src and usemap attributes depending on the dimensions of the viewport.

3 years ago

@pamtbaau:
I’m afraid that won’t work. Using css you can only set the background image of an element and <map> will only work together with <img src="..." usemap="...">

You might try adding two maps using Twig and use js to inject/update an <img> with the proper src and usemap attributes depending on the dimensions of the viewport.

Thank you!
I did it in that way and it seems, that now I have a working solution!

3 years ago

BTW: why are you using so old methods? I haven't been using maps for decades. I guess using a SVG would resolve lots of your troubles, including these with responsiveness.

3 years ago

Because it was the first solution which I have googled and it was working from the beginning 🙂
Have you seen such examples on stack overflow or somewhere else?

3 years ago

I'm sorry, I wasn't examining such a case close enough as I wasn't in need to build something like that. I've seen an example of interactive Europe map, long time ago, that was doing the same, but without the old <map> tag. It was using CSS and JS. Unfortunately it's already gone, but lots of things can be now made with SVG, just remember you need this SVG to be set inside HTML file to have the full control over it.

3 years ago

[b]blazejs[/b], understood, thanks!

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 9 hours ago
Support · by Anna, 3 days ago
2 59 12 hours ago
Support · by Justin Young, 13 hours ago
1 30 13 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago