Why padding never works in React Native? I have 10px padding in the image and the text box below:
const styles = StyleSheet.create({ container: { marginTop: 75, alignItems: 'center' }, image: { width: 107, height: 165, padding: 10, backgroundColor:'blue' }, description: { padding: 20, margin: 10, fontSize: 15, color: '#656565', backgroundColor:'red' } });
Result:
Any ideas why? Did I miss something?
The small circles highlight the points at which the bounding box of the Text component overlaps the border of the View component. You can think of margins as the distance between elements, but padding represents the space between the content of the element and the border of the same element.
To add padding and margin to all React Material-UI components, we can use the Box component. We use Material UI's Box component to add a container with margin and padding. We set the margin on all sides with the m prop and we set the top padding with the pt prop.
The main difference between margin and padding is that margin helps to create space around the element outside the border, while padding helps to create space around the element inside the border.
create({ imageSize: { //newWidth is the width of the device divided by 4. //This is so that four images will display in each row. width: newWidth, height: newWidth, padding: 2 }, list: { flexDirection: 'row', flexWrap: 'wrap' } });
Use this for padding:
function padding(a, b, c, d) { return { paddingTop: a, paddingRight: b ? b : a, paddingBottom: c ? c : a, paddingLeft: d ? d : (b ? b : a) } }
In practice
<Text style={{...padding(10, 20, 10, 5), color: "black"}}>Some text</Text>
Android with React Native tends to not like padding, unless it has a border. A temporary fix would be to change all of your "paddingXXX" to "marginXXX" to get the approximate styling you want.
const styles = StyleSheet.create({ container: { marginTop: 75, alignItems: 'center' }, image: { width: 107, height: 165, margin: 10, backgroundColor:'blue' }, description: { margin: 30, fontSize: 15, color: '#656565', backgroundColor:'red' } });
It's a really bad workaround, but I have yet to see a concise fix to it. As far as I know, there is an issue on the Git repo but hasn't been fixed yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With