Build faster with Premium Chakra UI Components 💎
Learn moreOctober 19, 2024
Floating labels conserve space in forms, particularly on mobile devices where screen space is limited.
To add a floating label to an input component in Chakra UI, create a styled
input field using the Field
, Input
, and Box
components.
import { Box, Field, Input, defineStyle } from "@chakra-ui/react"
const Demo = () => {
return (
<Field.Root>
<Box pos="relative" w="full">
<Input className="peer" placeholder="" />
<Field.Label css={floatingStyles}>Email</Field.Label>
</Box>
</Field.Root>
)
}
const floatingStyles = defineStyle({
pos: "absolute",
bg: "bg",
px: "0.5",
top: "-3",
insetStart: "2",
fontWeight: "normal",
pointerEvents: "none",
transition: "position",
_peerPlaceholderShown: {
color: "fg.muted",
top: "2.5",
insetStart: "3",
},
_peerFocusVisible: {
color: "fg",
top: "-3",
insetStart: "2",
},
})