Summary
In this lesson, we made the shelter's page react to its visitors and found out that:
- A variant is a prefix in front of a class, followed by a colon. A class with a variant applies only in its state:
hover:bg-orange-600only while the mouse pointer is over the element. The element keeps its usual classes too. - Every class that should apply only in a state needs its own variant, such as
focus-visible:outline-2 focus-visible:outline-orange-500. - Variants can be chained:
enabled:hover:bg-orange-600applies only when both conditions hold. transition-colors,transition-shadowortransition, together withduration-*, make changes smooth. They go on the element without a variant, so that the change is smooth both ways.cursor-pointershows the hand over a button,cursor-not-alloweda crossed-out circle.- Preflight removes the border and the background of form fields, so you style them yourself. The
placeholderattribute shows an example in an empty field, and the variantplaceholder:sets its color. - An element with focus needs a visible outline for people using the keyboard, so never remove it without a replacement:
focus:for fields,focus-visible:for buttons, withoutline-2,outline-offset-2and anoutline-*color. - The
disabledattribute switches a button off, and the variantdisabled:gives it a look that shows it. groupon an ancestor andgroup-hover:on an element inside it style the element when the pointer is over the ancestor.first:,last:,odd:andeven:style an element by its position among its siblings. They go on every one of the siblings.
| Variant | CSS | The class applies |
|---|---|---|
hover: | :hover | while the mouse pointer is over the element (only on devices that can hover) |
active: | :active | while the element is being pressed |
focus: | :focus | while the element has focus |
focus-visible: | :focus-visible | while the element has focus that should be seen (from the keyboard) |
disabled:, enabled: | :disabled, :enabled | when the element is disabled, or isn't |
placeholder: | ::placeholder | to the placeholder of a field |
group-hover: | .group:hover … | while the pointer is over the ancestor with the class group |
first:, last: | :first-child, :last-child | to the first or the last child |
odd:, even: | :nth-child(odd), :nth-child(even) | to the odd or the even children |
In the next lesson, Responsive Design, you'll meet variants that don't depend on what the visitor does, but on how wide the screen is.
Practice: here's a new piece of the shelter's website, a box asking for donations. Its "Donate" button is styled, but it doesn't react to anything yet. Use what you've learned in this lesson.
Instructions
When the mouse pointer is over the "Donate" button, make its background one shade darker (orange-600), and make the change smooth.
Show the hand over the button, and while the button is pressed, an even darker background (orange-700).
Give the button an orange outline, 2 px wide and 2 px away from the button, shown only when the button gets focus from the keyboard.
Start programming for free
9/9