In this lesson, you will learn why React Native does not support "margin collapsing" and how to handle layout spacing consistently across your UI components.
The Web vs. Native Reality
If you come from a web development background, you are likely used to margin collapsing, where the vertical margins of two adjacent block-level elements "merge" together into a single, larger margin. In CSS, the result is the larger of the two margins rather than the sum of both.
React Native does not support margin collapsing.
In the mobile layout engine (Yoga), every component's margin is treated as an absolute space that exists independently. If you place two View components next to each other with a marginBottom: 20 on the first and a marginTop: 20 on the second, React Native will render both. The total gap between these components will be 40 pixels, not 20.
Why This Matters
This difference often leads to "double spacing" bugs when developers try to translate web-based design systems directly into React Native. If you don't account for this, your UI will often look significantly "looser" or more spread out than your design mockups intend.
Solving the Spacing Inconsistency
To maintain clean, predictable layouts in React Native, we generally adopt a "single-direction" spacing strategy. Instead of mixing marginTop and marginBottom, stick to one rule.
// A common, predictable pattern
const Card = () => (
<View style={{ marginBottom: 16 }}>
<Text>Content</Text>
</View>
);
// Your main container layout
<View>
<Card />
<Card />
{/* Total gap is 16, not 32, because the last element doesn't
need a margin if handled by a parent or specific logic. */}
</View>
When you need to space out items inside a container, avoid individual margins entirely and use the gap property in your container style. This is the modern, preferred way to handle space in Flexbox:
<View style={{ flexDirection: 'column', gap: 16 }}>
<Component />
<Component />
</View>
Using gap ensures that space is only applied between children, preventing the issues caused by individual margin calculations and making your code much easier to debug.
Key Takeaway: React Native ignores margin collapsing, so use gap in your containers or adopt a single-direction margin strategy to prevent unintended double spacing.