|
| 1 | +--- |
| 2 | +id: basics-layout |
| 3 | +title: Layout |
| 4 | +layout: docs |
| 5 | +category: The Basics |
| 6 | +permalink: docs/basics-layout.html |
| 7 | +next: basics-network |
| 8 | +--- |
| 9 | + |
| 10 | +A component can specify the layout of its children using the flexbox algorithm. Flexbox is designed to provide a consistent layout on different screen sizes. |
| 11 | + |
| 12 | +You will normally use a combination of `flexDirection`, `alignItems`, and `justifyContent` to achieve the right layout. |
| 13 | + |
| 14 | +> Flexbox works the same way in React Native as it does in CSS on the web, with a few exceptions. The most notable one: the defaults are different, with `flexDirection` defaulting to `column` instead of `row`, and `alignItems` defaulting to `stretch` instead of `flex-start`. |
| 15 | +
|
| 16 | +#### Flex Direction |
| 17 | + |
| 18 | +Adding `flexDirection` to a component's `style` determines the **primary axis** of its layout. Should the children be organized horizontally (`row`) or vertically (`column`)? The default is `column`. |
| 19 | + |
| 20 | +```ReactNativeWebPlayer |
| 21 | +import React from 'react'; |
| 22 | +import { AppRegistry, View } from 'react-native'; |
| 23 | +
|
| 24 | +class AwesomeProject { |
| 25 | + render() { |
| 26 | + return ( |
| 27 | + // Try setting `flexDirection` to `column`. |
| 28 | + <View style={{flex: 1, flexDirection: 'row'}}> |
| 29 | + <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> |
| 30 | + <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} /> |
| 31 | + <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> |
| 32 | + </View> |
| 33 | + ); |
| 34 | + } |
| 35 | +}; |
| 36 | +
|
| 37 | +AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); |
| 38 | +``` |
| 39 | + |
| 40 | +#### Justify Content |
| 41 | + |
| 42 | +Adding `justifyContent` to a component's style determines the **distribution** of children along the **primary axis**. Should children be distributed at the start, the center, the end, or spaced evenly? Available options are `flex-start`, `center`, `flex-end`, `space-around`, and `space-between`. |
| 43 | + |
| 44 | +```ReactNativeWebPlayer |
| 45 | +import React from 'react'; |
| 46 | +import { AppRegistry, View } from 'react-native'; |
| 47 | +
|
| 48 | +class AwesomeProject { |
| 49 | + render() { |
| 50 | + return ( |
| 51 | + // Try setting `justifyContent` to `center`. |
| 52 | + // Try setting `flexDirection` to `row`. |
| 53 | + <View style={{ |
| 54 | + flex: 1, |
| 55 | + flexDirection: 'column', |
| 56 | + justifyContent: 'space-between', |
| 57 | + }}> |
| 58 | + <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> |
| 59 | + <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} /> |
| 60 | + <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> |
| 61 | + </View> |
| 62 | + ); |
| 63 | + } |
| 64 | +}; |
| 65 | +
|
| 66 | +AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); |
| 67 | +``` |
| 68 | + |
| 69 | +#### Align Items |
| 70 | + |
| 71 | +Adding `alignItems` to a component's style determines the **alignment** of children along the **secondary axis** (if the primary axis is `row`, then the secondary is `column`, and vice versa). Should children be aligned at the start, the center, the end, or stretched to fill? Available options are `flex-start`, `center`, `flex-end`, and `stretch`. |
| 72 | + |
| 73 | +> For `stretch` to have an effect, children must not have a fixed dimension along the secondary axis. In the following example, setting `alignItems: stretch` does nothing until the `width: 50` is removed from the children. |
| 74 | +
|
| 75 | +```ReactNativeWebPlayer |
| 76 | +import React from 'react'; |
| 77 | +import { AppRegistry, View } from 'react-native'; |
| 78 | +
|
| 79 | +class AwesomeProject { |
| 80 | + render() { |
| 81 | + return ( |
| 82 | + // Try setting `alignItems` to 'flex-start' |
| 83 | + // Try setting `justifyContent` to `flex-end`. |
| 84 | + // Try setting `flexDirection` to `row`. |
| 85 | + <View style={{ |
| 86 | + flex: 1, |
| 87 | + flexDirection: 'column', |
| 88 | + justifyContent: 'center', |
| 89 | + alignItems: 'center', |
| 90 | + }}> |
| 91 | + <View style={{width: 50, height: 50, backgroundColor: 'powderblue'}} /> |
| 92 | + <View style={{width: 50, height: 50, backgroundColor: 'skyblue'}} /> |
| 93 | + <View style={{width: 50, height: 50, backgroundColor: 'steelblue'}} /> |
| 94 | + </View> |
| 95 | + ); |
| 96 | + } |
| 97 | +}; |
| 98 | +
|
| 99 | +AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject); |
| 100 | +``` |
| 101 | + |
| 102 | +#### API Reference |
| 103 | + |
| 104 | +We've covered the basics, but there are many other styles you may need for layouts. The full list is available [here](./docs/flexbox.html). |
0 commit comments