这将居中显示
import { Box, Center } from "@chakra-ui/react"
const Demo = () => {
return (
<Center bg="bg.emphasized" h="100px" maxW="320px">
<Box>This will be centered</Box>
</Center>
)
}
用法
import { AbsoluteCenter, Center, Circle, Square } from "@chakra-ui/react"
<Center bg="tomato" h="100px" color="white">
This is the Center
</Center>
示例
图标
Center 可用于在图标或数字周围创建框架。
1
import { Box, Center, HStack } from "@chakra-ui/react"
import { LuPhone } from "react-icons/lu"
const Demo = () => {
return (
<HStack>
<Center w="40px" h="40px" bg="tomato" color="white">
<LuPhone />
</Center>
<Center w="40px" h="40px" bg="tomato" color="white">
<Box as="span" fontWeight="bold" fontSize="lg">
1
</Box>
</Center>
</HStack>
)
}
带内联的 Center
使用 inline
属性将显示方式更改为 inline-flex
。
import { Box, Center, Link } from "@chakra-ui/react"
import { LuArrowRight } from "react-icons/lu"
const Demo = () => {
return (
<Link href="#">
<Center inline gap="4">
<Box>Visit Chakra UI</Box>
<LuArrowRight />
</Center>
</Link>
)
}
Square
Square
根据给定的 size
(宽度和高度)将其子元素居中。
import { Square } from "@chakra-ui/react"
import { LuPhoneForwarded } from "react-icons/lu"
const Demo = () => {
return (
<Square size="10" bg="purple.700" color="white">
<LuPhoneForwarded />
</Square>
)
}
Circle
Circle
根据给定的 size
将其子元素居中并围绕其创建一个圆形。
import { Circle } from "@chakra-ui/react"
import { LuPhoneForwarded } from "react-icons/lu"
const Demo = () => {
return (
<Circle size="10" bg="blue.700" color="white">
<LuPhoneForwarded />
</Circle>
)
}
AbsoluteCenter
AbsoluteCenter
使用 position: absolute
策略相对于其父元素居中。传递 axis
属性可改变对齐轴。
import { AbsoluteCenter, Box } from "@chakra-ui/react"
import { LuPhone } from "react-icons/lu"
const Demo = () => {
return (
<Box position="relative" h="100px">
<AbsoluteCenter bg="tomato" p="4" color="white" axis="both">
<LuPhone />
</AbsoluteCenter>
</Box>
)
}
属性
AbsoluteCenter
属性 | 默认值 | 类型 |
---|---|---|
axis | 'horizontal' | 'vertical' | 'both' |