Heading
Heading is used to render semantic HTML heading elements.
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> ); }
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> ); }