Margin, padding, and border help you control space and edges around your app elements. They make your app look neat and easy to use.
Margin, padding, border in React Native
Introduction
Syntax
React Native
const styles = StyleSheet.create({
box: {
margin: 10, // space outside the box
padding: 15, // space inside the box
borderWidth: 2, // thickness of the border
borderColor: 'blue' // color of the border
}
});Margin adds space outside the element.
Padding adds space inside the element, between content and border.
Border draws a line around the element.
Examples
React Native
margin: 20React Native
paddingHorizontal: 10, paddingVertical: 5
React Native
borderWidth: 1, borderColor: 'red', borderRadius: 5
Sample App
This app shows a white box centered on the screen. The box has space outside it (margin), space inside it around the text (padding), and a blue border around it.
React Native
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; export default function App() { return ( <View style={styles.container}> <View style={styles.box}> <Text style={styles.text}>Hello with margin, padding, and border!</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0' }, box: { margin: 20, padding: 15, borderWidth: 3, borderColor: 'blue', backgroundColor: 'white' }, text: { fontSize: 16 } });
Important Notes
Margin and padding can be set for all sides or individually (top, bottom, left, right).
Border color and width must be set together to see the border.
Use borderRadius to make rounded corners on borders.
Summary
Margin adds space outside an element.
Padding adds space inside an element.
Border draws a line around an element.
Practice
1. What does
padding do in a React Native style?easy
Solution
Step 1: Understand padding concept
Padding adds space inside the element, pushing content away from the edges.Step 2: Compare with margin and border
Margin adds space outside the element; border draws a line around it.Final Answer:
Adds space inside the element, between content and border -> Option AQuick Check:
Padding = inside space [OK]
Hint: Padding is inside space; margin is outside space [OK]
Common Mistakes:
- Confusing padding with margin
- Thinking border adds space
- Mixing padding with background color
2. Which of the following is the correct way to add a 10 pixel margin to all sides of a
View in React Native?easy
Solution
Step 1: Identify margin syntax
To add margin on all sides, usemargin: 10.Step 2: Check other options
Padding adds inner space, borderWidth adds border thickness, and marginLeft only adds left margin.Final Answer:
{ margin: 10 } -> Option DQuick Check:
margin: 10 adds margin all sides [OK]
Hint: Use margin: value for all sides margin [OK]
Common Mistakes:
- Using padding instead of margin
- Using borderWidth to add space
- Only setting marginLeft instead of all sides
3. What will be the visible effect of this style on a
View?
{
padding: 20,
margin: 10,
borderWidth: 5,
borderColor: 'red'
}medium
Solution
Step 1: Analyze padding and margin values
Padding: 20 adds space inside the View; margin: 10 adds space outside.Step 2: Analyze border properties
borderWidth: 5 adds a 5 pixel border; borderColor: 'red' colors it red.Final Answer:
The View has 10 pixels space outside, 20 pixels space inside, and a 5 pixel red border -> Option CQuick Check:
margin = outside, padding = inside, borderWidth + borderColor = border [OK]
Hint: Margin outside, padding inside, borderWidth + borderColor visible [OK]
Common Mistakes:
- Mixing padding and margin values
- Ignoring borderColor effect
- Assuming borderWidth is padding
4. Identify the error in this style code for adding margin and border:
{
margin: 15,
border: '2px solid black'
}medium
Solution
Step 1: Check margin syntax
Margin: 15 is valid as a number for all sides.Step 2: Check border property
React Native does not support CSS shorthand like 'border: 2px solid black'. It requires separate properties: borderWidth and borderColor.Final Answer:
React Native does not support the 'border' shorthand property -> Option BQuick Check:
No 'border' shorthand in React Native [OK]
Hint: Use borderWidth and borderColor separately, no shorthand [OK]
Common Mistakes:
- Using CSS shorthand border property
- Thinking margin needs to be string
- Believing margin requires padding
5. You want a
View with a 15 pixel red border, 10 pixels padding inside, and 20 pixels margin outside. Which style object achieves this correctly?hard
Solution
Step 1: Match borderWidth and borderColor
Border should be 15 pixels thick and red, so borderWidth: 15 and borderColor: 'red' are correct.Step 2: Match padding and margin values
Padding inside should be 10, margin outside 20, so padding: 10 and margin: 20 are correct.Step 3: Check other options for errors
{ borderWidth: 10, borderColor: 'red', padding: 20, margin: 15 } swaps padding and margin values; { border: '15px solid red', padding: 10, margin: 20 } uses unsupported border shorthand; { borderWidth: 15, borderColor: 'red', padding: 20, margin: 10 } swaps padding and margin values.Final Answer:
{ borderWidth: 15, borderColor: 'red', padding: 10, margin: 20 } -> Option AQuick Check:
Correct border, padding, margin values [OK]
Hint: Match each property to its exact value carefully [OK]
Common Mistakes:
- Swapping padding and margin values
- Using CSS border shorthand
- Incorrect borderWidth or borderColor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
