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-600 only 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-600 applies only when both conditions hold.
  • transition-colors, transition-shadow or transition, together with duration-*, make changes smooth. They go on the element without a variant, so that the change is smooth both ways.
  • cursor-pointer shows the hand over a button, cursor-not-allowed a crossed-out circle.
  • Preflight removes the border and the background of form fields, so you style them yourself. The placeholder attribute shows an example in an empty field, and the variant placeholder: 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, with outline-2, outline-offset-2 and an outline-* color.
  • The disabled attribute switches a button off, and the variant disabled: gives it a look that shows it.
  • group on an ancestor and group-hover: on an element inside it style the element when the pointer is over the ancestor.
  • first:, last:, odd: and even: style an element by its position among its siblings. They go on every one of the siblings.
VariantCSSThe class applies
hover::hoverwhile the mouse pointer is over the element (only on devices that can hover)
active::activewhile the element is being pressed
focus::focuswhile the element has focus
focus-visible::focus-visiblewhile the element has focus that should be seen (from the keyboard)
disabled:, enabled::disabled, :enabledwhen the element is disabled, or isn't
placeholder:::placeholderto 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-childto 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

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

9/9