Search code examples
reactjsgridviewreact-nativeflexbox

Space between components in React Native styling


enter image description here

I have 6 View components (shown in the picture) , I want to have space between all 6 View components.

My code:

<View style={{flexDirection:'column',flex:6}}>
            <View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
                <View style={{backgroundColor:'red',flex:1}}>
                </View>
                <View style={{backgroundColor:'blue',flex:1}}>
                </View>
            </View>


            <View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
                <View style={{backgroundColor:'white',flex:1}}>
                </View>
                <View style={{backgroundColor:'black',flex:1}}>
                </View>

            </View>

            <View style={{flex:2,flexDirection:"row",justifyContent:'space-between'}}>
                <View style={{backgroundColor:'gray',flex:1}}>
                </View>
                <View style={{backgroundColor:'yellow',flex:1}}>
                </View>

            </View>


 </View>

Solution

  • Try to add padding to the element then and another blank view in each row, padding make space between each item

    enter image description here

    <View style={{
          flexDirection:'column',
          flex:1,
        }}>
            <View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
                <View style={{backgroundColor:'red',flex:2,padding:10}}>
                </View>
              <View style={{flex:0.1}}/>
                <View style={{backgroundColor:'blue',flex:2,padding:10}}>
                </View>
            </View>
    
            <View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
                <View style={{backgroundColor:'white',flex:2,padding:10}}>
                </View>
                <View style={{flex:0.1}}/>
                <View style={{backgroundColor:'black',flex:2,padding:10}}>
                </View>
          </View>
          <View style={{flex:2,flexDirection:"row",justifyContent:'space-between',padding:10}}>
                <View style={{backgroundColor:'gray',flex:1,padding:10}}>
                </View>
                <View style={{flex:0.1}}/>
                <View style={{backgroundColor:'yellow',flex:1,padding:10}}>
                </View>
            </View>