Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Flexbox Layout


Flexbox is a powerful layout system in React Native that allows you to design flexible and responsive layouts. It simplifies the process of aligning and distributing space among items in a container, even when the size of items is unknown or dynamic. Here are the key concepts and properties used in Flexbox:


Flex Container (View with flex)

In React Native, a View component can act as a flex container by using the flex property.

Example

import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

const FlexboxExample = () => {
  return (
    <View style={styles.container}>
      <View style={styles.box}>
        <Text>Box 1</Text>
      </View>
      <View style={styles.box}>
        <Text>Box 2</Text>
      </View>
      <View style={styles.box}>
        <Text>Box 3</Text>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1, // Take up all available space
    flexDirection: 'row', // Arrange children horizontally
    justifyContent: 'space-around', // Align items with space around
    alignItems: 'center', // Align items vertically
    backgroundColor: '#f0f0f0',
  },
  box: {
    width: 100,
    height: 100,
    backgroundColor: '#2196F3',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default FlexboxExample;

        

Flex Direction (flexDirection)

The flexDirection property specifies the primary axis of the layout.

Example

            const styles = StyleSheet.create({
                container: {
                  flex: 1,
                  flexDirection: 'column', // Default: arrange items vertically
                  justifyContent: 'center',
                  alignItems: 'center',
                },
              });
              
        

Justify Content (justifyContent)

The justifyContent property defines how items are distributed along the main axis.

Example

            const styles = StyleSheet.create({
                container: {
                  flex: 1,
                  justifyContent: 'center', // Center items vertically
                  alignItems: 'center',
                },
              });
              
        

Align Items (alignItems)

The alignItems property defines how items are aligned along the cross axis

Example

            const styles = StyleSheet.create({
                container: {
                  flex: 1,
                  justifyContent: 'center',
                  alignItems: 'center', // Center items horizontally
                },
              });
              
        

Conclusion

Flexbox in React Native provides a powerful way to create layouts that adapt to different screen sizes and orientations. By understanding and using properties like flex, flexDirection, justifyContent, and alignItems, you can efficiently design responsive and flexible UIs for your mobile applications.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java