--- title: Conditional Styles description: Learn how to use conditional and responsive styles in Chakra. --- ## Overview Chakra allows you to write styles for pseudo states, media queries, and custom data attributes with the conditional style props. :::note See the list of [built-in conditions](#reference) below. ::: ## Usage For example, here's how to change the background color of a button when it's hovered: ```jsx Hover me ``` ### Nested condition Conditional values can be nested to create complex selector rules. Here's how to change the background color of an element when in focus on hover: ```jsx Hover & Focus me ``` ### At Rules This also works with the supported at-rules (`@media`, `@layer`, `@container`, `@supports`, and `@page`): ```tsx Hello ``` ## Pseudo Classes ### Hover, Active, Focus, and Disabled Here's an example of how to style the hover, active, focus, and disabled states of an element ```jsx Hover me > Hover me ``` ### First, Last, Odd, Even Here's an example of how to style the first, last, odd, and even elements in a list ```jsx {items.map((item) => ( {item} ))} ``` You can also style even and odd elements using the `_even` and `_odd` modifier ```jsx {items.map((item) => ( ))}
{item}
``` ## Pseudo Elements ### Before and After To style the `::before` and `::after` pseudo elements of an element, use the `_before` and `_after` modifiers ```jsx /_before/ Hello ``` ### Placeholder To style the placeholder text of any input or textarea, use the `_placeholder` modifier: ```jsx {3} ``` ### File Inputs To style the file input button, use the `_file` modifier: ```jsx {3} ``` ## Media Queries ### Reduced Motion Use the `_motionReduce` and `_motionSafe` modifiers to style an element based on the user's motion preference: ```jsx Hello ``` ### Color Scheme The `prefers-color-scheme` media feature is used to detect if the user has requested the system to use a light or dark color theme. Use the `_osLight` and `_osDark` modifiers to style an element based on the user's color scheme preference: ```jsx Hello ``` ### Color Contrast The `prefers-contrast` media feature is used to detect if the user has requested the system use a high or low contrast theme. Use the `_highContrast` and `_lessContrast` modifiers to style an element based on the user's color contrast preference: ```jsx Hello ``` ### Orientation The `orientation` media feature is used to detect if the user has a device in portrait or landscape mode. Use the `_portrait` and `_landscape` modifiers to style an element based on the user's device orientation: ```jsx Hello ``` ## Selectors ### Arbitrary selectors For arbitrary, use the `css` prop to write styles for one-off selectors: ```tsx ``` Here's another example that targets the child elements of a parent element: ```tsx *": { margin: "2" }, }} /> ``` ### Group Selectors To style an element based on its parent element's state or attribute, add the `group` class to the parent element, and use any of the `_group*` modifiers on the child element. ```jsx
Hover me
``` This modifier works for every pseudo class modifiers like `_groupHover`, `_groupActive`, `_groupFocus`, and `_groupDisabled`, etc. ### Sibling Selectors To style an element based on its sibling element's state or attribute, add the `peer` class to the sibling element, and use any of the `_peer*` modifiers on the target element. ```jsx /_peerHover={{ bg: "red.500" }}/

Hover me

I'll change by bg
``` > **Note:** This only works for when the element marked with `peer` is a > previous siblings, that is, it comes before the element you want to start. ## Data Attribute ### LTR and RTL To style an element based on the direction of the text, use the `_ltr` and `_rtl` modifiers ```jsx {2}
Hello
``` ### State To style an element based on its `data-{state}` attribute, use the corresponding `_{state}` modifier ```jsx /_loading/ Hello ``` This works for common states like `data-active`, `data-disabled`, `data-focus`, `data-hover`, `data-invalid`, `data-required`, and `data-valid`. ```jsx /_active/ Hello ``` ### Orientation To style an element based on its `data-orientation` attribute, use the `_horizontal` and `_vertical` modifiers ```jsx Hello ``` ## ARIA Attribute To style an element based on its `aria-{state}=true` attribute, use the corresponding `_{state}` prop ```jsx Hello ``` ## Reference Here's a list of all the condition props you can use in Chakra: ## Customization Chakra lets you create your own conditions, so you're not limited to the ones in the default preset. Learn more about customizing conditions [here](/docs/theming/customization/conditions).