concept padding in category react native

This is an excerpt from Manning's book React Native in Action.
Listing 3.18 Creating TodoButton.js
import React from 'react' import { Text, TouchableHighlight, StyleSheet } from 'react-native' const TodoButton = ({ onPress, complete, name }) => ( #1 <TouchableHighlight onPress={onPress} underlayColor='#efefef' style={styles.button}> <Text style={[ styles.text, complete ? styles.complete : null, #2 name === 'Delete' ? styles.deleteButton : null ]} #3 > {name} </Text> </TouchableHighlight> ) const styles = StyleSheet.create({ button: { alignSelf: 'flex-end', padding: 7, borderColor: '#ededed', borderWidth: 1, borderRadius: 4, marginRight: 5 }, text: { color: '#666666' }, complete: { color: 'green', fontWeight: 'bold' }, deleteButton: { color: 'rgba(175, 47, 47, 1)' } }) export default TodoButtton #1 Takes onPress, complete, and name as props #2 Checks whether complete is true, and applies a style #3 Checks whether the name property equals “Delete” and, if so, applies a style
Pay particular attention to the style that centers the text. You got lucky by using
margin: 10. If you usedpadding: 10, the background of the text component would occlude the underlying border stroke of theViewcomponent (see figure 4.7).Figure 4.7 This is what figure 4.6 would look like if the centeredText style used padding: 10 instead of margin: 10 to position the text. 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. When padding is specified, it allows the content of the component to not be flush against the border. In figure 4.9, the
backgroundColorproperty bleeds through the component’s edges up to the border, which is the space defined bypadding. The available properties available forpaddingarepadding,paddingLeft,paddingRight,paddingTop, andpaddingBottom. If only the mainpaddingproperty is set without another, more-specific value such aspaddingLeftorpaddingTop, then that value is passed to all sides of the component (top, right, bottom, and left). If bothpaddingand a more-specificpaddingproperty are specified, such aspaddingLeft, then the more-specificpaddingproperty takes precedence. This behavior is exactly like borders and margins.Rather than create a new example to show how padding is different than margins, let’s reuse the code from listing 4.11 and make a few tweaks. Change the
marginstyles on the example components topaddingstyles, and add a border around theTextcomponents and change their background color. Figure 4.11 shows what you’ll end up with.Listing 4.12 Modifying listing 4.11 to replace margins with padding
import React, { Component } from 'react'; ... <View style={styles.container}> <View style={styles.exampleContainer}> <Example style={{}}> #1 <CenteredText>A</CenteredText> </Example> </View> <View style={styles.exampleContainer}> <Example style={{paddingTop: 50}}> #2 <CenteredText>B</CenteredText> </Example> </View> <View style={styles.exampleContainer}> <Example style={{paddingTop: 50, paddingLeft: 10}}> #3 <CenteredText>C</CenteredText> </Example> </View> <View style={styles.exampleContainer}> <Example style={{paddingLeft: -10, paddingTop: -10}}> #4 <CenteredText>D</CenteredText> </Example> </View> </View> ... }, centeredText: { textAlign: 'center', margin: 10, borderWidth: 1, #5 backgroundColor: 'lightgrey' } }); #1 Example A: unchanged, with no margins or padding #2 Example B: marginTop changed to paddingTop #3 Example C: marginTop and marginLeft changed to paddingTop and paddingLeft, respectively #4 Example D: marginLeft and marginTop changed to paddingLeft and marginTop, respectively. The negative values remain. #5 Adds a border and background color to the Text component !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":11,\"ch\":29},{\"line\":11,\"ch\":39}],[{\"line\":16,\"ch\":29},{\"line\":16,\"ch\":39}],[{\"line\":21,\"ch\":47},{\"line\":21,\"ch\":57}],[{\"line\":39,\"ch\":36},{\"line\":39,\"ch\":46}],[{\"line\":40,\"ch\":51},{\"line\":40,\"ch\":61}],[{\"line\":11,\"ch\":29},{\"line\":11,\"ch\":39}],[{\"line\":16,\"ch\":29},{\"line\":16,\"ch\":39}],[{\"line\":21,\"ch\":47},{\"line\":21,\"ch\":57}],[{\"line\":39,\"ch\":36},{\"line\":39,\"ch\":46}],[{\"line\":40,\"ch\":51},{\"line\":40,\"ch\":61}],[{\"line\":16,\"ch\":45},{\"line\":16,\"ch\":56}],[{\"line\":21,\"ch\":29},{\"line\":21,\"ch\":40}],[{\"line\":40,\"ch\":66},{\"line\":40,\"ch\":77}],[{\"line\":41,\"ch\":51},{\"line\":41,\"ch\":62}],[{\"line\":16,\"ch\":45},{\"line\":16,\"ch\":56}],[{\"line\":21,\"ch\":29},{\"line\":21,\"ch\":40}],[{\"line\":40,\"ch\":66},{\"line\":40,\"ch\":77}],[{\"line\":41,\"ch\":51},{\"line\":41,\"ch\":62}],[{\"line\":11,\"ch\":29},{\"line\":11,\"ch\":39}],[{\"line\":16,\"ch\":29},{\"line\":16,\"ch\":39}],[{\"line\":21,\"ch\":47},{\"line\":21,\"ch\":57}],[{\"line\":39,\"ch\":36},{\"line\":39,\"ch\":46}],[{\"line\":40,\"ch\":51},{\"line\":40,\"ch\":61}],[{\"line\":33,\"ch\":7},{\"line\":33,\"ch\":22}],[{\"line\":34,\"ch\":7},{\"line\":34,\"ch\":35}]]"} !@%STYLE%@!