290

I have already read several flexbox tutorial, but I still cannot make this simple task to work.

How can I make the red box to 100% width?

enter image description here

Code:

  <View style={styles.container}>
    <Text style={styles.welcome}>
      Welcome to React Natives
    </Text>
    <Text style={styles.line1}>
      line1
    </Text>
    <Text style={styles.instructions}>
      Press Cmd+R to reload,{'\n'}
      Cmd+D or shake for dev menu
    </Text>
  </View>

style:

container: {
  flex: 1,
  justifyContent: 'center',
  alignItems: 'center',
  backgroundColor: '#F5FCFF',
  borderWidth: 1,
  flexDirection: 'column',
},
welcome: {
  fontSize: 20,
  textAlign: 'center',
  margin: 10,
  borderWidth: 1,
},
line1: {
    backgroundColor: '#FDD7E4',
},
instructions: {
  textAlign: 'center',
  color: '#333333',
  marginBottom: 5,
  borderWidth: 1,
},

Thank you!

Update 1: Suggestion by Nishanth Shankar, adding flex:1 for the wrapper, flexDirection: 'row'

Output:

enter image description here

Code:

  <View style={styles.container}>
    <View style={{flex:1}}>
      <Text style={styles.welcome}>
        Welcome to React Natives
      </Text>
    </View>
    <View style={{flex:1}}>
      <Text style={styles.line1}>
        line1
      </Text>
    </View>
    <View style={{flex:1}}>
      <Text style={styles.instructions}>
        Press Cmd+R to reload,{'\n'}
        Cmd+D or shake for dev menu
      </Text>
    </View>
  </View>

  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    borderWidth: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    borderWidth: 1,
  },
  line1: {
      backgroundColor: '#FDD7E4',
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
    borderWidth: 1,
  },

12 Answers 12

534

Simply add alignSelf: "stretch" to your item's stylesheet.

line1: {
    backgroundColor: '#FDD7E4',
    alignSelf: 'stretch',
    textAlign: 'center',
},
7
  • 2
    Using alignSelf stretch, I had an issue with Android - I had an Image with 'absolute' position and in this case, even using 'stretch', such box gets padded to the edge of the elements contained within. Dimensions.get('window').width worked both on iOS and Android in my case.
    – st_bk
    Commented Dec 24, 2017 at 15:40
  • This one way solved all of my 'full width' problems, check it out: snack.expo.io/S1PWQqkcZ
    – webdevinci
    Commented May 19, 2018 at 20:59
  • 3
    It seems like width: "100%" should also work but it doesn't. I think I don't understand when alignSelf: "stretch" works vs width: "100%". @Corentin any ideas? Thx!
    – Joel
    Commented Sep 26, 2018 at 4:06
  • 6
    @st_bk was a life saver there! However, I found a better solution for the event that you're absolutely positioning and need to stretch: position: absolute, top: 0, bottom: 0, left: 0, right: 0 Commented Nov 7, 2018 at 19:34
  • to @lance.dolan point - you really only need the left and right params if you are looking for width 100%. Also i am not 100% on this, but for the alignStretch rule to work you have to have set flexDirection: 'row', because it is set as column by default. Commented May 8, 2019 at 9:57
148

You should use Dimensions

First, define Dimensions.

import { Dimensions } from "react-native";

var width = Dimensions.get('window').width; //full width
var height = Dimensions.get('window').height; //full height

then, change line1 style like below:

line1: {
    backgroundColor: '#FDD7E4',
    width: width,
},
9
  • 4
    thx Melih Mucuk! alignSelf: "stretch" suggested by Corentin S. is the easiest and simple so I accepted that answer. However, having Dimensions leads me to a new way to play around the layout. In some cases, we could adjust the items programmatically, thank you!
    – franfran
    Commented Oct 26, 2015 at 3:47
  • 1
    @Melih I find the width returned is wrong on my Android device. The right width should be 720px, but 360px returns. Commented Jan 23, 2016 at 11:39
  • 4
    @peacepassion, you got 360, because your device dpi is scaled 2x. Try Dimensions.get('window').scale - this should return 2 in your case.
    – matusalem
    Commented Mar 7, 2016 at 16:28
  • Thanks for your answer, I am dealing with a container with a background and your suggestion fits perfectly.
    – clarenswd
    Commented Mar 22, 2016 at 11:59
  • 14
    This will not work when the device is rotated. It's better to use adaptive layout. Commented Dec 21, 2016 at 18:14
32

Editted:

In order to flex only the center text, a different approach can be taken - Unflex the other views.

  • Let flexDirection remain at 'column'
  • remove the alignItems : 'center' from container
  • add alignSelf:'center' to the textviews that you don't want to flex

You can wrap the Text component in a View component and give the View a flex of 1.

The flex will give :

100% width if the flexDirection:'row' in styles.container

100% height if the flexDirection:'column' in styles.container

3
  • yes, I tried to set flexDirection:'row' and flex: 1 so that the width go to 100%. However, the components are all inline and cannot go to the next line. even I used flexWrap: 'wrap'
    – franfran
    Commented Oct 23, 2015 at 9:02
  • any luck with the new method franfran? Commented Oct 24, 2015 at 18:45
  • the alignSelf: 'stretch' suggested by Corentin S. works.
    – franfran
    Commented Oct 26, 2015 at 3:45
18

Here you go:

Just change the line1 style as per below:

line1: {
    backgroundColor: '#FDD7E4',
    width:'100%',
    alignSelf:'center'
}
1
  • 4
    I am very happy this worked! I don't know why, I assumed percentage values weren't working in RN....
    – p-syche
    Commented Nov 24, 2020 at 11:09
6

First add Dimension component:

import { AppRegistry, Text, View,Dimensions } from 'react-native';

Second define Variables:

var height = Dimensions.get('window').height;
var width = Dimensions.get('window').width;

Third put it in your stylesheet:

textOutputView: {
    flexDirection:'row',
    paddingTop:20,
    borderWidth:1,
    borderColor:'red',
    height:height*0.25,
    backgroundColor:'darkgrey',
    justifyContent:'flex-end'
}

Actually in this example I wanted to make responsive view and wanted to view only 0.25 of the screen view so I multiplied it with 0.25, if you wanted 100% of the screen don't multiply it with any thing like this:

textOutputView: {
    flexDirection:'row',
    paddingTop:20,
    borderWidth:1,
    borderColor:'red',
    height:height,
    backgroundColor:'darkgrey',
    justifyContent:'flex-end'
}
6

Use javascript to get the width and height and add them in View's style. To get full width and height, use Dimensions.get('window').width https://facebook.github.io/react-native/docs/dimensions.html

getSize() {
    return {
        width: Dimensions.get('window').width, 
        height: Dimensions.get('window').height
    }
}

and then,

<View style={[styles.overlay, this.getSize()]}>
1
  • Ooh, I like this. Possibly not the best solution but it did solve my problem. Not to mention that it's a pretty neat function in general. Commented Jun 17, 2017 at 21:26
4

width: '100%' and alignSelf: 'stretch' didn't work for me. Dimensions didn't suite my task cause I needed to operate on a deeply nested view. Here's what worked for me, if I rewrite your code. I just added some more Views and used flex properties to achieve the needed layout:

  {/* a column */}
  <View style={styles.container}>
    {/* some rows here */}
    <Text style={styles.welcome}>
      Welcome to React Natives
    </Text>
    {/* this row should take all available width */}
    <View style={{ flexDirection: 'row' }}>
      {/* flex 1 makes the view take all available width */}
      <View style={{ flex: 1 }}>
        <Text style={styles.line1}>
          line1
        </Text>
      </View>
      {/* I also had a button here, to the right of the text */}
    </View>
    {/* the rest of the rows */}
    <Text style={styles.instructions}>
      Press Cmd+R to reload,{'\n'}
      Cmd+D or shake for dev menu
    </Text>
  </View>
3

just remove the alignItems: 'center' in the container styles and add textAlign: "center" to the line1 style like given below.

It will work well

container: {
  flex: 1,
  justifyContent: 'center',
  backgroundColor: '#F5FCFF',
  borderWidth: 1,
}

line1: {
    backgroundColor: '#FDD7E4',
    textAlign:'center'
},
3
Style ={{width : "100%"}}

try this:

StyleSheet generated: {
  "width": "80%",
  "textAlign": "center",
  "marginTop": 21.8625,
  "fontWeight": "bold",
  "fontSize": 16,
  "color": "rgb(24, 24, 24)",
  "fontFamily": "Trajan Pro",
  "textShadowColor": "rgba(255, 255, 255, 0.2)",
  "textShadowOffset": {
    "width": 0,
    "height": 0.5
  }
}
1
  • Please provide a proper answer to the question. Do not provide a link without first explaining here what it is about.
    – Akaisteph7
    Commented Jul 7, 2019 at 0:50
2

Noted: Try to fully understanding about flex concept.

       <View style={{
          flex: 2,
          justifyContent: 'center',
          alignItems: 'center'
        }}>
          <View style ={{
              flex: 1,
              alignItems: 'center, 
              height: 50, 
              borderWidth: 1, 
              borderColor: '#000' 
          }}>
               <Text>Welcome to React Nativ</Text>
           </View>
           <View style={{
              flex: 1,
              alignItems: 'center,
              borderWidth: 1, 
              borderColor: 'red ', 
              height: 50
            }}
            >
              <Text> line 1 </Text>
            </View>
          <View style={{
            flex: 1,
            alignItems: 'center, 
            height: 50, 
            borderWidth: 1,                     
            borderColor: '#000'
          }}>
             <Text>
              Press Cmd+R to reload,{'\n'}
              Cmd+D or shake for dev menu
             </Text>
           </View>
       </View>
1

Simply use width:'100%'

line1: {
      backgroundColor: '#FDD7E4',
width: '100%'
  },
0

My one was not working with width 100%. I added flex: 1 to the child component. It worked immediately. You can give it a try, it might work for your case too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.