question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Image] Maintaining Aspect Ratio of <Image>

See original GitHub issue

[EDIT] Use aspectRatio style prop since RN 0.40+. Docs: aspectRatio

Starting this discussion thread around how to maintain aspect ratio of an image like it does in a browser. When we use <img src='example.com/image.jpg'> in a browser, it renders a 0x0 block, then it fetches the image, looks at its width and height, then updates the layout with this new width/height bound by width/height set on container. So if the image is 400x300. It effectively gets a style of width: 400 and height: 300. But if we configure styling to be width: 500, then it fills in the other dimension height: 300 and is able to display the image.

Now in React Native, this behaviour does not exist for the right reasons. But we also lose this ability to somehow convey the size of the image. In React Native we are limited to giving width and height as part of the style. But in scenarios where the width actually comes from the Flexbox behaviour - with something like alignSelf: stretch, the height needs to be configured absolutely. So it ends up with the width taking the entire width from parent, but height being fixed. Using cover on the image, we are able to fill this space with the image, but the image gets cropped.

@vjeux mentioned in #494 that he prototyped an implementation where you would pass in something like an aspectRatio for the image, and then React Native would maintain that within the bounds of the configured styling - but he backed off because it is not part of the CSS standard. I have a suggestion on how we can do this (we use this method in our internal screen layout system that also uses css-layout).

In React Native, an image is configured like this: <Image source={{uri: "example.com/image.jpeg"}} />, what we are missing here is information about the size of the image. What if we gave that info here in source, and not as part of CSS style info? So it would look something like this:

<Image source={{
    uri: "example.com/image.jpeg",
    height: 400,
    width: 300
  }} style={{
    alignSelf: 'stretch'
  }} />

In this example, the width of the Image would come from the parent, and it would pick the height based on the ratio inferred from the meta info in source. The logic to this would remain the same as what @vjeux prototyped, but now we have a way to configure it outside CSS as there is nothing of this sort in the CSS spec itself. The source spec is a React Native spec, and hence we can support two new properties width and height inside it to not have to fetch an image, but instead know the height and width of the image beforehand.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

5reactions
HaskellFuncommented, Jan 19, 2018

I use this library to get the job done react-native-scalable-image.

4reactions
mikelambertcommented, Dec 3, 2016

I believe aspectRatio was only added in 0.40: https://github.com/facebook/react-native/commit/5850165795c54b8d5de7bef9f69f6fe6b1b4763d

(though thanks for the heads-up!)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preserve an Image's Aspect Ratio When Resized | kirupa.com
By setting the width property to 100 %, you are telling the image to take up all the horizontal space that is available....
Read more >
CSS force image resize and keep aspect ratio - Stack Overflow
Just replace the height attribute by the aspect-ratio attribute. · Using padding-top to set the height from width. · Using position: absolute to...
Read more >
Resize Images Proportionally While Keeping The Aspect Ratio
Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the...
Read more >
3 Ways to Keep Image Aspect Ratio In HTML CSS - Code Boxx
To maintain the aspect ratio of images in CSS, the easiest way is to manually set the dimension of the width, then the...
Read more >
Image Resizer - Crop & Resize Image Online - RedKetchup
How to resize the image to a specific aspect ratio?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found