Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - why padding does not work?

Tags:

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: enter image description here

Any ideas why? Did I miss something?

like image 646
Run Avatar asked Apr 18 '16 09:04

Run


People also ask

How does padding work in React Native?

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.

How do you add padding in React?

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.

What is the difference between padding and margin in React Native?

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.

How do I give an image a padding in React Native?

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' } });


2 Answers

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> 
like image 196
gilbert-v Avatar answered Sep 24 '22 02:09

gilbert-v


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.

like image 38
Erik Melton Avatar answered Sep 25 '22 02:09

Erik Melton