Course contentsShow
← React Native
Lesson 257 of 1,5554. Flexbox and Native StylingFree lesson

Controlling Aspect Ratio Constraints

Maintain proportional scaling of media assets across varying device display ratios.

In this lesson, you will learn how to maintain a consistent shape for your media assets, like images or video containers, regardless of the screen dimensions of the device they are running on.

The Problem with Fixed Dimensions

When building cross-platform apps, you often encounter a variety of screen sizes—from compact tablets to elongated smartphones. If you set a fixed width and height on an image, it might look perfect on one device but appear stretched or squashed on another. Simply setting one dimension (e.g., width: '100%') often leads to the component collapsing or losing its intended visual form.

Using the aspectRatio Prop

React Native provides a specific style property called aspectRatio. This property tells the layout engine: "Whatever width you calculate for this component, ensure the height remains proportional to that width based on this specific ratio."

The value is a simple number (width divided by height). For example, a standard landscape video (16:9) has an aspect ratio of 1.77, while a square image has a ratio of 1.

import React from 'react';
import { View, Image, StyleSheet } from 'react-native';

const MediaCard = () => {
  return (
    <View style={styles.container}>
      <Image
        source={{ uri: 'https://example.com/photo.jpg' }}
        style={styles.responsiveImage}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    width: '100%',
  },
  responsiveImage: {
    // This forces the height to adjust automatically 
    // to keep the image a perfect square (1:1)
    aspectRatio: 1, 
    width: '100%',
  },
});

Why This Matters

By using aspectRatio, you delegate the complex math of responsive scaling to the layout engine. When the parent container changes size—perhaps due to the device orientation switching from portrait to landscape—the image will automatically resize its height to match the new width, preventing the visual distortion that occurs with hard-coded pixel values. It is the most reliable way to ensure your media assets maintain their intended artistic intent across the entire range of mobile hardware.

Key Takeaway: Use the aspectRatio style property to create self-scaling components that maintain their visual proportions relative to their parent container's width.