Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 71 ยท Styling and Layout

Sizing, Margins and Absolute Positioning

Control size and spacing in React Native: width, height, percentages, aspectRatio, padding, margin, position absolute, zIndex and overflow, with live layout boxes.

Flexbox decides how children share a container, but you still need to say how big things are, how much air surrounds them and, occasionally, where something sits on top of its neighbors. This lesson covers the properties that do that: width and height, percentages, aspectRatio, padding and margin, position: 'absolute', zIndex and overflow.

If you know CSS, a good part will be familiar, and the differences are small but they matter. Two examples up front: every View already behaves as if box-sizing: border-box were set, and every View is a positioning context by default (there is no position: 'fixed'). As in the previous lessons, the live demos use HTML as a stand-in for native views.

Width and height

You can give a size in three ways:

TSX
<View style={{ width: 120, height: 80 }} />       {/* density-independent pixels */}
<View style={{ width: '50%' }} />                  {/* percent of the parent's size */}
<View style={{ flex: 1 }} />                       {/* share of the free space */}

Percentages refer to the parent's corresponding dimension, so width: '50%' inside a 300-wide parent is 150. A percentage is meaningless if the parent has no size of its own, which is another reason to keep every ancestor sized.

When you set nothing, a view is as big as its content along the main axis, and (with the default alignItems: 'stretch') fills the parent along the cross axis. In the default column layout that means "full width, as tall as the content", which is what you want for most cards and rows.

The constraint props minWidth, maxWidth, minHeight and maxHeight clamp the result. They are the right tool for "grow with the content, but never beyond 600" on a tablet.

Images need sizes

A local image knows its own size, but a remote one does not until it downloads, so the layout cannot guess. Always give a remote Image a width and height (or width plus aspectRatio), or it renders at zero size and looks missing. You will see this again in Images, Icons and Media.

aspectRatio

aspectRatio is width divided by height. Set one dimension and the other follows:

TSX
<View style={{ width: '100%', aspectRatio: 16 / 9, backgroundColor: '#000' }} />

This is how you build responsive media: a video frame, a square avatar grid cell (aspectRatio: 1), or a card header that keeps its proportions on any screen width.

Padding and margin

padding is space inside a view's border, margin is space outside it. Both accept a number and have the usual per-side and per-axis forms:

Props Meaning
padding, margin all four sides
paddingHorizontal, marginHorizontal left and right
paddingVertical, marginVertical top and bottom
paddingTop, paddingBottom, paddingLeft, paddingRight one side (same for margin)
paddingStart, paddingEnd (and margin) start/end, which flip in right-to-left languages

Specific sides win over the broader shorthand. Two details differ from CSS:

  • Because of border-box sizing, width: 100 with padding: 10 is still 100 wide in total, and the content area shrinks to 80.
  • Vertical margins do not collapse. A 20-high bottom margin followed by a 20-high top margin gives 40, not 20.

Use paddingStart/paddingEnd (or marginStart/marginEnd) instead of left/right if your app will ship in Arabic or Hebrew, so layouts mirror correctly.

Here the box model is drawn with a stand-in. Change the numbers to see how the outer size, the margin and the content area relate:

Stand-in: padding, margin and border-box sizing

With the starting values the view is 200 wide, the content area is 200 - 16*2 - 4 = 164 (the 4 is the two 2-wide borders in this stand-in), and with margins the view occupies 240 units of its parent.

Three ways to size

Fixed width and height in dp.
Percent A string, relative to the parent.
Flex A share of the free space.
1 / 2

Absolute positioning

By default a view takes part in the Flexbox layout of its parent. Setting position: 'absolute' removes it from that flow and places it with top, right, bottom, left relative to the parent's edges (inside its border). Other children behave as if the absolute view were not there.

Since every View is already positioned (the default is position: 'relative'), an absolute child is always placed against its direct parent. You never need to add position: 'relative' to the parent, the way you do on the web.

Useful patterns:

TSX
// A badge in the corner of an icon
<View style={{ width: 40, height: 40 }}>
  <Icon />
  <View style={{ position: 'absolute', top: -4, right: -4, backgroundColor: 'red', borderRadius: 8, minWidth: 16, alignItems: 'center' }}>
    <Text style={{ color: '#fff', fontSize: 11 }}>3</Text>
  </View>
</View>

// A dimming overlay that covers its parent exactly
<View style={StyleSheet.absoluteFill} />

StyleSheet.absoluteFill is a ready-made style equal to position: 'absolute' with all four edges at 0. React Native also has absoluteFillObject for when you need the raw object to spread into your own style.

Because there is no position: 'fixed', things that stay on screen while content scrolls (a floating action button, a bottom bar) are placed in a root view that sits outside the ScrollView, or inside the screen container with absolute positioning.

zIndex and drawing order

Siblings are drawn in the order they appear, so later ones cover earlier ones. zIndex overrides that for siblings: a higher value draws on top. It only compares views that share a parent, so you cannot lift a child above its parent's neighbor by giving it a huge value. If you need that, restructure so both live under the same parent, or use a Modal, covered in Modals, Alerts and Bottom Sheets.

On Android elevation also affects the drawing order, which is a well-known source of surprises when a shadowed card appears above something that has a higher zIndex.

overflow

overflow accepts 'visible' (the default), 'hidden' or 'scroll'. With hidden, anything outside the view's bounds is clipped. That is how you round the corners of an image container (borderRadius plus overflow: 'hidden') or cut off a banner's decorative shapes. Remember the trade-off: a clipped parent also hides an absolutely positioned badge that sticks out, like the one above.

The next demo stacks a card, a corner badge, an overlay and a zIndex toggle:

Stand-in: absolute positioning, zIndex and overflow

Tick overflow: hidden and the badge is cut at the card's edge, exactly as it would be in React Native. Untick the zIndex box and the badge slips under the dark overlay, because the overlay's zIndex: 1 beats the badge's 0.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A View is 200 wide with padding: 20 and a 2-wide border on each side. How wide is its content area?

    Views use border-box sizing: the 200 total includes padding (20 on each side, 40) and the border (2 on each side, 4), leaving 200 - 40 - 4 = 156.
  2. 2. A parent has width 300. A child sets width: '50%'. How wide is the child?

    A percentage refers to the parent's matching dimension, so half of 300 is 150. If the parent had no size of its own, the percentage would have nothing to refer to.
  3. 3. Why does an absolutely positioned child of a plain View not need position: 'relative' on its parent?

    The default position is relative, so each View anchors its absolute children. There is no static-parent lookup like the one on the web.
  4. 4. Two sibling Views overlap. The first has zIndex: 5, the second has none. Which draws on top?

    zIndex overrides the default order for siblings. Remember it only compares views that share a parent.

Summary

  • Sizes come from fixed numbers in dp, percentage strings (relative to the parent), flex, or content; min* and max* clamp them.
  • aspectRatio (width divided by height) lets one dimension follow the other; remote images need explicit sizes.
  • Padding is inside the border, margin outside and never collapses; views use border-box sizing; paddingStart/End support right-to-left languages.
  • position: 'absolute' takes a view out of the flow and anchors it to its parent; StyleSheet.absoluteFill covers the parent. There is no fixed.
  • zIndex orders siblings only; overflow: 'hidden' clips children, including badges that stick out.
  • The boxes in the demos are HTML stand-ins for native views.

Next: Responsive Layouts and Screen Sizes