Styling
Change colours, sizes and spacing with the style prop.
Your React Native code runs inside your browser tab on React Native for Web, shown on a phone-sized screen. Nothing to install.
The style prop
Looks are set with style. Its value is a JavaScript object, so there are two pairs of curly brackets: the outer pair switches to JavaScript, and the inner pair is the object.
Property names are written in camel case, such as fontSize. Numbers have no unit, and colours are strings.
import { Text, View } from "react-native";
export default function App() {
return (
<View>
<Text style={{ color: "crimson", fontSize: 28 }}>Fresh today</Text>
</View>
);
}Your turn: style some text
Two properties in one style object.
Practice task
Show the word Sale in green, with a font size of 30.
Your answer is run and checked in the lesson. Create a free account to try it.
Keeping styles together
Long style objects make the tags hard to read. StyleSheet.create lets you name your styles and keep them below the component.
The component then refers to a style by its name.
import { StyleSheet, Text, View } from "react-native";
export default function App() {
return (
<View>
<Text style={styles.title}>Asha's Bakery</Text>
<Text style={styles.note}>Open from 7 am</Text>
</View>
);
}
const styles = StyleSheet.create({
title: { fontSize: 26, fontWeight: "bold" },
note: { color: "gray" },
});Styling a View
A View can be styled too. backgroundColor fills it, padding adds space inside it, and borderRadius rounds its corners.
Styled Views are how cards, banners and buttons are built.
import { Text, View } from "react-native";
export default function App() {
return (
<View style={{ backgroundColor: "lightyellow", padding: 16, borderRadius: 12 }}>
<Text>Cinnamon rolls, fresh at 9</Text>
</View>
);
}Your turn: a card
Style the View that holds the text.
Practice task
Show the word Menu inside a View that has a yellow background and a padding of 20.
Your answer is run and checked in the lesson. Create a free account to try it.