I'm with difficulties to centralize my components in screen using CSS.
See my App.js
:
import { Container } from 'native-base';
import React from 'react';
import { StyleSheet } from 'react-native';
import Screen from './ScreenContainer';
const styles = StyleSheet.create({
container: {
backgroundColor: '#FF6666',
flex: 1
}
});
const App = () => (
<Provider store={store}>
<Container style={styles.container}>
<Screen />
</Container>
</Provider>
);
export default App;
Now, see my ScreenContainer.js
import { Container, Content, Form, Input, Label, Item } from 'native-base';
import React from 'react';
import AppLogo from '../components/AppLogo';
const Screen = () => (
<Container>
<Content>
<AppLogo />
<Form>
<Item floatingLabel last>
<Label>Username</Label>
<Input />
</Item>
</Form>
</Content>
</Container>
);
export default Screen;
This code results in this screen:
But, I want that the screen stay with this format:
When I change the code for:
const styles = StyleSheet.create({
container: {
alignItems: 'center',
backgroundColor: '#FF6666',
flex: 1
}
});
My App returns this screen:
You can either use class="row" of can give inline styles like style={{ display: 'flex, justContent: 'center', alignItems: 'center' this will perfectly align everything in center of the parent div.
To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .
In React Native, Flexbox is the same as it is for basic CSS. The only difference being in default values. flexDirection: The default value for CSS is row whereas the default value in React Native is column. alignContent: The default value for CSS is stretch the default value in React Native is flex-start.
To center our box we use the align-items property to align our item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally.
It's Done:
import { Container, Content, Form, Input, Label, Item } from 'native-base';
import React from 'react';
import { StyleSheet } from 'react-native';
import AppLogo from '../components/AppLogo';
const styles = StyleSheet.create({
container: {},
content: {
alignItems: 'center',
flex: 1,
justifyContent: 'center'
},
form: {
width: '100%'
},
item: {}
});
const Screen = () => (
<Container style={styles.container}>
<Content contentContainerStyle={styles.content}>
<AppLogo />
<Form style={styles.form}>
<Item floatingLabel last>
<Label>Username</Label>
<Input />
</Item>
</Form>
</Content>
</Container>
);
export default Screen;
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