# How to keep an image's natural aspect ratio when setting height

**URL:** <https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059>\
**Category:** New Responsive Engine\
**Created:** [September 27, 2022, 10:00pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059 "2022-09-27T22:00:46Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [September 27, 2022, 10:00pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/1 "2022-09-27T22:00:46Z")

</div>

i want to set an image’s height but let the width be set by the natural aspect ratio.

Instead, the width is getting set to 100% and making all my images look like weird rectangles.

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/3/3/33cec114a539d9d23973721c9facc9e9e2b20dfd.jpeg)

I’ve been able to do this when I specify the width but not the height.

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/5/4/5417bf41dd052059fe6e8ab954fd7032577f970f.jpeg)

I’d like them to have fixed height rather than fixed width. But allow the width to automatically be determined by the height.

---

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [October 31, 2022, 12:56am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/2 "2022-10-31T00:56:19Z")

</div>

Just past the one-month anniversary of posting. Hope y’all don’t mind a bump in case someone knows?

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [October 31, 2022, 5:55am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/3 "2022-10-31T05:55:47Z")

</div>

Interesting that there’s no way to do this  
It’s worth building a plugin around

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [October 31, 2022, 6:47am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/4 "2022-10-31T06:47:55Z")

</div>

> **[Get Image Size Plugin for Bubble, by Blur Apps](https://bubble.io/plugin/get-image-size-1667195998036x526076211612090400)**
>
> Get Image Size Plugin page on Bubble, by Blur Apps. Add Get Image Size in one click to your app. Bubble lets you build web apps without any code.

Just built this out. Hope it helps

* * *

Cheers,  
Ranjit from [Atomic Fusion](https://atomicfusion.io/)

Explore and reuse the community’s Bubble assets & Accelerate your Bubble development with Atomic Fusion

---

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [October 31, 2022, 2:25pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/5 "2022-10-31T14:25:20Z")

</div>

Does that need to download each element to get the dimensions, which I guess would mean you download each image twice, once to get size, then once on the page when you display it?

Definitely solves my problem though. I’ll have to see what the bandwidth effects are.

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [November 1, 2022, 4:53am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/6 "2022-11-01T04:53:58Z")

</div>

The image gets cached on first load, so it shouldn’t be downloading multiple times

---

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [November 11, 2022, 2:41am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/7 "2022-11-11T02:41:15Z")

</div>

You know, I finally had a moment to implement this and I’m realizing that images don’t have any place that allows you to dynamically set width and height. How were you intending to use the values that app that you built exposes?

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [November 11, 2022, 4:28am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/8 "2022-11-11T04:28:44Z")

</div>

Setting Custom CSS via JS at the moment. Running it via the ‘run javascript’ action from the Toolbox plugin on page load:

```auto
$(function() {
    var div = $('#myElement');
   
    div.css('height', <Plugin_height_value>);
    div.css('width', <Plugin_width_value>);
});

```

Can’t wait for dynamic height/width becomes a thing on Bubble.

---

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [November 11, 2022, 4:26pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/9 "2022-11-11T16:26:49Z")

</div>

And of course the next problem with Bubble is the lack of being able to assign a class to items.

I kinda feel bad for the developers, there’s so much opportunity to make Bubble better with fairly small changes, but… they can only do it one at a time.

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [November 11, 2022, 5:11pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/10 "2022-11-11T17:11:28Z")

</div>

Well there is a plugin that lets you do that, but it’s not the most reliable in my experience. I’d prefer just injecting CSS.

This JS that I mentioned above doesn’t need any classes to be assigned since ‘width’ and ‘height’ are default attributes that exist on the element. So simply running a code snippet like that along with the plugin on page load should do the trick for you!

---

<div class="post-metadata">

**Author:** ![brenton.strine](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/brenton.strine/32/124156_2.png) [@brenton.strine](https://forum.bubble.io/u/brenton.strine)\
**Post date:** [November 11, 2022, 5:21pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/11 "2022-11-11T17:21:49Z")

</div>

Right, I would just need to add the ID to each item or use Classify.

Really what I’m realizing is that when you find yourself going against the grain of what’s easy in Bubble… stop.

---

<div class="post-metadata">

**Author:** ![dcardinesiii](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dcardinesiii](https://forum.bubble.io/u/dcardinesiii)\
**Post date:** [December 9, 2022, 5:37am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/12 "2022-12-09T05:37:56Z")

</div>

Hi can I chime in?

I have a few questions about the implementation

1. what’s the workflow you used to activate the “run javascript” action, “on page load?”
2. in the snippet there’s a line that references a div “#myElement” buy you said there’s no need for CSS so I’m a bit confused?
3. can you show the settings of your javascript element it’s the first time i’m using it

Thank you!

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [December 9, 2022, 6:17am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/13 "2022-12-09T06:17:30Z")

</div>

‘run javascript’ comes from the Toolbox plugin

Without CSS would have been ideal, but there isn’t a way around it unfortunately.  
The question is how exactly to implement it - manually via the ‘run javascript’ workflow or by using a CSS injector plugin like Classify.

---

<div class="post-metadata">

**Author:** ![dcardinesiii](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dcardinesiii](https://forum.bubble.io/u/dcardinesiii)\
**Post date:** [December 9, 2022, 6:22am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/14 "2022-12-09T06:22:43Z")

</div>

So basically here are the steps

1. Install ‘imageheight’, ‘classify’, and ‘java…’ plugin
2. Add CSS ID to the image element
3. Add the imageheight element
4. On page load activate ‘run java…’
5. Enter the code snippet to the javascript element

My follow up questions would be:

1. Will I mess with the names of these variables \<Plugin\_height\_value\> \<Plugin\_width\_value\> or no need anymore

Thanks for replying so quickly btw

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [December 9, 2022, 6:28am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/15 "2022-12-09T06:28:10Z")

</div>

It’s an either/or between classify and ‘run javascript’

So the steps would be:

1. Install the Toolbox plugin
2. Add a ‘run javascript’ WF on page load with this script:  
[How to keep an image's natural aspect ratio when setting height - #8 by atomicfusion](http://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/8)
3. Set the ID of the target element as “myElement”

That should do it

---

<div class="post-metadata">

**Author:** ![dcardinesiii](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dcardinesiii](https://forum.bubble.io/u/dcardinesiii)\
**Post date:** [December 9, 2022, 7:02am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/16 "2022-12-09T07:02:45Z")

</div>

I can’t seem to get it to work

Here’s the imageheight element:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/b/a/bad768ddb75791d3d7b8a94d3130441443444621.jpeg)

The Image element ID:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/4/6/461106190c7459be8ee49e0cdfbf794821e4aafe.png)

And the workflow snippet:

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/f/9/f92520050f0c56725f43fc1b548180cfbe1aa4f5.png)

Here’s what’s resulting, it’s getting cut and what it should be is a full length of a page screenshot

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/d/2/d22a31f7bdaf164d3c9eb38cd89e3acbca750f6f.jpeg)

---

<div class="post-metadata">

**Author:** ![atomicfusion](https://sea1.discourse-cdn.com/bubble/user_avatar/forum.bubble.io/atomicfusion/32/138701_2.png) [@atomicfusion](https://forum.bubble.io/u/atomicfusion)\
**Post date:** [December 9, 2022, 4:41pm UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/17 "2022-12-09T16:41:25Z")

</div>

OK

So \<Plugin\_height\_value\> would come from the plugin via dynamic data - so it would look something like `ImageSize A's height_value`

similarly for \<Plugin\_width\_value\>

---

<div class="post-metadata">

**Author:** ![dcardinesiii](https://avatars.discourse-cdn.com/v4/letter/d/a88e4f/32.png) [@dcardinesiii](https://forum.bubble.io/u/dcardinesiii)\
**Post date:** [December 10, 2022, 9:45am UTC](https://forum.bubble.io/t/how-to-keep-an-images-natural-aspect-ratio-when-setting-height/226059/18 "2022-12-10T09:45:51Z")

</div>

Yeah I tried it too, unfortunately it doesn’t work also…

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/5/a/5a9d645e0b585fea21f2eb814f50632285a1306f.png)

here’s the result

 ![image](https://us1.discourse-cdn.com/bubble/original/3X/2/a/2ae2f92c64604903105e821ae1d27f87bc01c9fb.jpeg)
