react-native-css-vh-vw
A ditto of CSS vw
and vh
units - returns, in pixels, a percentage of the device's
viewport width or height.
Install
npm install react-native-css-vh-vw
Dependencies
react-native-css-vh-vw
itself has no dependencies, but it must be used within a
React Native project. The only React Native API that it relies on is Dimensions
, which has support all the way back to React Native's first release.
Usage
vh()
or vw()
to style React Native components.
Simply pass a value between 1 and 1000 to vh(50)
.
50% of your device's viewport height: vw(33.3)
.
33.3% of your device's viewport width: Examples
- Using
vh()
andvw()
to set dimensions of<View>
: Component:
const VhVwDemo = { return <View style= height: width: backgroundColor: 'blue' justifyContent: 'center' alignItems: 'center' > <Text style= color: 'white' >height: </Text> <Text style= color: 'white' >width: </Text> </View> ;} ;
Output:
- Using
vh()
andvw()
to set dimensions of<Text>
:
const VhVwDemo = { return <View style= justifyContent: 'center' alignItems: 'center' > <Text style= fontSize: >fontSize: </Text> <Text style= fontSize: >fontSize: </Text> </View> ;} ;
Output: