# Using responsive images with Hugo

**URL:** <https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524>\
**Category:** tips & tricks\
**Tags:** modules\
**Created:** [March 6, 2022, 4:31pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524 "2022-03-06T16:31:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [March 6, 2022, 4:31pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/1 "2022-03-06T16:31:59Z")

</div>

I’ve created a Hugo module for handling responsive images with Hugo. In a gist, you can use this partial (from the module) to generate multiple resized versions of an image and wrap them in a `<picture>` tag with media rules so that the browser loads the image according to the viewport size.

Here’s an example usage:

```nohighlight
{{ with resources.Get "images/example.jpg" }}
  {{ partial "img" (dict "img" . "alt" "Some alt text") }}
{{ end }}

```

This is the Hugo module I wrote:

> **[hugo-modules/responsive-images at main · UtkarshVerma/hugo-modules](https://github.com/UtkarshVerma/hugo-modules/tree/main/responsive-images)**
>
> main/responsive-images

I talk about it more in this blog post:

> **[Using responsive images effortlessly with Hugo](https://bitbanged.com/posts/using-responsive-images-effortlessly-with-hugo/)**
>
> Learn how to use responsive images with Hugo to make your website faster without breaking a sweat.

Looking forward to hearing your thoughts and suggestions on this.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 6, 2022, 7:31pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/2 "2022-03-06T19:31:43Z")

</div>

MediaType is unreliable due to:  
[https://github.com/gohugoio/hugo/issues/8931](https://github.com/gohugoio/hugo/issues/8931)

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [March 6, 2022, 8:10pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/3 "2022-03-06T20:10:08Z")

</div>

It seems a few people (including myself) have image handling / responsive image modules. Do you have a synopsis of what is different / interesting about your module compared to others out there?

Also, for what it’s worth, I’m not a fan of monorepos (why is an off-topic discussion), so even if I didn’t have my own module, I’d be unlikely to use yours, for that reason; I wonder if you have considered having separate repos rather than a monorepo?

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [March 7, 2022, 5:05am UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/4 "2022-03-07T05:05:08Z")

</div>

> [@cshoredaniel](#):
>
> It seems a few people (including myself) have image handling / responsive image modules. Do you have a synopsis of what is different / interesting about your module compared to others out there?

I don’t know about the other modules, so I cannot answer this. I think other modules will behave somewhat similar to my module. I made this module because things like these have to be tailored to one’s needs. That is why I wrote it for myself. I shared it online so that it might be helpful to others who are in the process of writing their own partials for the same task.

> [@cshoredaniel](#):
>
> Also, for what it’s worth, I’m not a fan of monorepos (why is an off-topic discussion), so even if I didn’t have my own module, I’d be unlikely to use yours, for that reason; I wonder if you have considered having separate repos rather than a monorepo?

It’s a matter of preference. I find it easier to have all my modules in a single repo because when I’m working on a website, it is likely that multiple modules are worked upon and updated. It’s way easier for me to commit changes directly.

Also, I don’t see any difference in terms of usage of monorepo modules as compared to a standalone one. You can get my module simply through:

```auto
# config.yaml
module:
  imports:
    - path: github.com/UtkarshVerma/hugo-modules/responsive-images

```

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [March 7, 2022, 5:00pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/5 "2022-03-07T17:00:34Z")

</div>

Fair enough, thank you for answering. I don’t know most of the other image handling repos out there either. Knowing it is mostly a repo for yourself, with hopes it might provide something useful to others as well is helpful.

For my repo, my goal has been to be flexible enough that other people can use it for their own site via configuration options (if the defaults don’t meet their needs), but it remains to be seen if I have succeeded. I will be posting in the forum about my module ‘stack’ release sometime soon and hopefully get some feedback then.

The monorepo thing is a topic for another forum; I had tried using a monorepo for a couple different (non-Hugo) project and discovered warts. There is often not a single right answer to this kind of thing, so to each their own.

---

<div class="post-metadata">

**Author:** ![ju52](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/ju52/32/6749_2.png) [@ju52](https://discourse.gohugo.io/u/ju52)\
**Post date:** [March 7, 2022, 7:36pm UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/6 "2022-03-07T19:36:55Z")

</div>

I tryed to put it all in the [image render hook](https://github.com/gj52/HugoSample/blob/master/themes/photon/layouts/_default/_markup/render-image.html).

Some values can set in config with Params, used with site.Params.\*  
You are free to use and modify it.

```auto
[Params.Images]
    maxSize = 1480
    setSizes = [480, 800, 1200]
    sizes = "sizes=(max-width: 30em) 90vw, (max-width: 60em) 80vw, 1480px"

```

**sizes** matches the values in the used CSS

HTH

---

<div class="post-metadata">

**Author:** ![UtkarshVerma](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/utkarshverma/32/11273_2.png) [@UtkarshVerma](https://discourse.gohugo.io/u/UtkarshVerma)\
**Post date:** [March 8, 2022, 6:25am UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/7 "2022-03-08T06:25:53Z")

</div>

Thank you for sharing your approach. That was how I started out initially as well, and it supports the majority of the use cases.

Now, after having made the partial, I just call the partial in the render-hook.

---

<div class="post-metadata">

**Author:** ![cshoredaniel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cshoredaniel/32/19684_2.png) [@cshoredaniel](https://discourse.gohugo.io/u/cshoredaniel)\
**Post date:** [March 9, 2022, 4:26am UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/8 "2022-03-09T04:26:58Z")

</div>

I went a step further and made the partial so that it can also be called by a shortcode. So there in my repo there is a replacement for the ‘figure’ shortcode that allows a content creator to override basically anything, a partial (for use in layouts), and a the render hook with site params that can be overridden by page params. It’s perhaps overkill and not what everyone will want, but I find it useful.

---

<div class="post-metadata">

**Author:** ![powerfulwebdesign](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/powerfulwebdesign/32/14227_2.png) [@powerfulwebdesign](https://discourse.gohugo.io/u/powerfulwebdesign)\
**Post date:** [November 4, 2023, 12:44am UTC](https://discourse.gohugo.io/t/using-responsive-images-with-hugo/37524/9 "2023-11-04T00:44:56Z")

</div>

I have developed this image module

> **[GitHub - future-wd/hugo-responsive-images](https://github.com/future-wd/hugo-responsive-images)**
>
> Contribute to future-wd/hugo-responsive-images development by creating an account on GitHub.

The idea was that it was as non opinionated as possible. I am open to suggestions if they follow that principle.
