The visual color appearance of the component
Heading
Heading is used to render semantic HTML heading elements.
Import#
import { Heading } from '@chakra-ui/react'
Usage#
Heading
composes Box
so you can use all the style props and add responsive
styles as well. It renders an <h2>
tag by default.
<Heading>I'm a Heading</Heading>
Changing visual size#
To increase the visual size of the heading, use the size
props. This property
ensures that the heading size automatically adjusts for smaller screens.
<Stack spacing={6}><Heading as='h1' size='4xl' noOfLines={1}>(4xl) In love with React & Next</Heading><Heading as='h2' size='3xl' noOfLines={1}>(3xl) In love with React & Next</Heading><Heading as='h2' size='2xl'>(2xl) In love with React & Next</Heading><Heading as='h2' size='xl'>(xl) In love with React & Next</Heading><Heading as='h3' size='lg'>(lg) In love with React & Next</Heading><Heading as='h4' size='md'>(md) In love with React & Next</Heading><Heading as='h5' size='sm'>(sm) In love with React & Next</Heading><Heading as='h6' size='xs'>(xs) In love with React & Next</Heading></Stack>
Truncate heading#
If you'd like to truncate the heading after a specific number of lines, pass the
noOfLines
prop. This will render an ellipsis when the heading exceeds the
width of the viewport or maxWidth
prop.
<Heading noOfLines={1}>Basic text writing, including headings, body text, lists, and more.</Heading>
Override style#
You can override the size of the Heading component by passing the fontSize
prop. No need to write css
or styled()
.
<Heading size='lg' fontSize='50px'>I'm overriding this heading</Heading>
Composition#
<Box maxW='32rem'><Heading mb={4}>Modern online and offline payments for Africa</Heading><Text fontSize='xl'>Paystack helps businesses in Africa get paid by anyone, anywhere in theworld</Text><Button size='lg' colorScheme='green' mt='24px'>Create a free account</Button></Box>
Props#
The Heading
composes the Box component, so you
can pass all Box
props.
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
size
size
The size of the Heading
"4xl" | "3xl" | "2xl" | "xl" | "lg" | "md" | "sm" | "xs"
xl
variant
variant
The variant of the Heading
string
The Heading
component is a single part component. All of the styling is
applied directly to the h2
element.
To learn more about styling single part components, visit the Component Style page
Theming properties#
The properties that affect the theming of the Heading
component are:
size
: The size of the divider. Defaults toxl
.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Customizing the default theme#
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const custom = defineStyle({color: "yellow.500",fontFamily: "mono",fontWeight: "semibold"// let's also provide dark mode alternatives_dark: {color: 'yellow.300',}})
After customizing the heading theme, we can import it in our theme file and add it in the components property:
import { extendTheme } from '@chakra-ui/react'import { headingTheme } from './components/heading'export const theme = extendTheme({components: { Heading: headingTheme },})
This is a crucial step to make sure that any changes that we make to the heading theme are applied.
Adding a custom variant#
Let's assume we want to include a custom branded variant. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const underline = defineStyle({color: "orange.500",borderBottom: "2px",borderRadius: "10",fontFamily: "serif"// let's also provide dark mode alternatives_dark: {color: 'orange.400',},_hover: {borderColor: "red.200",_dark: {borderColor: "red.300"}}})// Now we can use the new `underline` variant<Heading variant="underline">...</Heading>
Every time you're adding anything new to the theme, you'd need to run the cli command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Changing the default properties#
import { defineStyleConfig } from '@chakra-ui/react'export const headingTheme = defineStyleConfig({defaultProps: {size: 'xl',variant: 'custom',colorScheme: 'brand',},})// This saves you time, instead of manually setting the size,// variant every time you use a heading:<Heading size="xl" variant="underline">...</Heading>
Showcase#
import { Box, SimpleGrid, IconButton, Heading, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <Heading>Default heading</Heading> <Heading variant="custom">Themed heading</Heading> <Heading variant="brand">Branded heading</Heading> <Heading variant="underline">Underlined heading</Heading> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }