Styles API
What is Styles API
Styles API is a set of props and techniques that allows you to customize styles of any element inside Mantine component inline or with theme object. All Mantine components that have styles support Styles API.
Styles API selectors
Every Mantine component that supports Styles API has a set of elements names that can be used to
apply styles to inner elements inside the component. For simplicity, these elements names are called
selectors in Mantine documentation. You can find selectors information under Styles API
tab
in component documentation.
Example of Button component selectors:
You can use these selectors in classNames
and styles
both in component props and in theme.components
:
classNames prop
With classNames
prop you can add classes to inner elements of Mantine components. It accepts
object with keys as element names and values as classes:
classNames in theme.components
You can also define classNames in theme.components to apply them to all components of specific type:
Components CSS variables
Most of Mantine components use CSS variables to define colors, sizes, paddings and other
properties. You can override these values by with a custom CSS variables resolver function
in theme.components or by passing it to vars
prop.
You can find CSS variables information under Styles API
tab in component documentation.
Example of Button component CSS variables:
Example of custom CSS variables resolver function used to add more sizes to Button component:
styles prop
styles
prop works the same way as classNames
, but applies inline styles. Note that inline
styles have higher specificity than classes, so you will not be able to override them with classes
without using !important
. You cannot use pseudo-classes (for example, :hover
, :first-of-type
)
and media queries inside styles
prop.
styles prop usage
Some examples and demos in the documentation use
styles
prop for convenience, but it is not recommended to usestyles
prop as the primary means of styling components, asclassNames
prop is more flexible and has better performance.
Styles API based on component props
You can also pass a callback function to classNames
and styles
. This function will receive
theme as first argument and component props as second. It should return
object of classes (classNames
) or styles (styles
).
You can use this feature to conditionally apply styles based on component props. For example, you can change TextInput label color if the input is required or change the input background color if input has error:
Static classes
Every component that supports Styles API also includes static classes that can be used to style
component without using classNames
or styles
props. By default, static classes have
.mantine-{ComponentName}-{selector}
format. For example, root
selector of Button
component will have .mantine-Button-root
class.
You can use static classes to style component with CSS or any other styling solution:
Static classes prefix can be changed with classNamesPrefix
on MantineProvider.
Components classes
Classes of each component are available in Component.classes
object. For example, you can
find Button classes in Button.classes
:
Key | Class |
---|---|
root | m-77c9d27d |
inner | m-80f1301b |
label | m-811560b9 |
section | m-a74036a |
loader | m-a25b86ee |
group | m-80d6d844 |
You can use these classes to create components with the same styles as Mantine components: