Nuance and Webbiness#
Our HTML/CSS efforts up to this point have been relatively broad, to start with the basics. Here we want to sand down some of the rough edges, and introduce you to some specific, advanced techniques you can use to refine and enliven your work—still with just CSS, no JavaScript (yet)!
A good pattern to follow in web (and all) development is to use each technology only for what it does best—using HTML for semantic meaning, CSS to handle how we form a page, and, later, JavaScript to introduce more interaction. But even before we get to JS, we can start to layer in some more nuance, interest, and liveliness with our CSS.
Let’s look at some examples.
It is vain to do with more what can be done with less.
Keyboard Navigation/Focus#
With a lens of accessibility, we want to ensure folks using a keyboard to navigate the web have a good experience on our sites. The first way we do this is via well-structured HTML—that follows a logical, reading order for our page/content—especially as we work responsively.
-
pseudo-class:focus-visible
Let’s restore these indicators! -
A guide to designing accessible focus indicators
Deep-dive from Sara Soueidan.
But as we know, our reset clears all the browser visual defaults—including the stock focus indicator outlines that show when a link is selected, by keyboard. We can restore (and customize) these in our CSS, using the :focus-visibleoutline
Click in the example, and then use
Advanced Typography#
Precise, Optical text-box #
text-boxYou have probably noticed that HTML renders a lot of extra space around text elements, called the line box (or, in design software parlance, the bounding box).
-
Font Metrics, Line-Height and Vertical-Align
A deep-dive on type positioning. -
Vertical Spacing and Line-Height in Design Systems – Google Fonts
It’s all very complicated. -
- MDNtext-box
Now, control the bounding box! -
- web.devtext-box-trim
So… much… easier!
It is based on the font-family ,font-size ,line-height ,marginpadding )
But we can avoid it, now! After a decade of workarounds and discussion, browsers recently added support for the super-easy text-box
You you might consider doing this on everything with the shorthand * { text-box: trim-both cap alphabetic; } !inline
Hanging Punctuation (Kinda)#
Ideally we could set our punctuation outside of our text blocks, for nice visual alignment based solely on the letters—a traditional design technique called hanging punctuation.
7 Practical Uses for ::before::after
Another great CSS Tricks article.
There is actually a hanging-punctuation
Note that the (pasted-in) curly quotation marks are not in the HTML here in the last example! Pseudo-elements again, ::before::after .
Text Ragging (Sorta)#
We’ve gone on-and-on about how you can’t treat the web like print—always perfectly ragging your text for nice, smooth blocks. In modern (responsive) web design we don’t always know what our text will be, nor where it will wrap!
But we can do a handful of things to make for better ragging/wraps, given the unknowns—judiciously using hyphens /­ ,<wbr> ,white-space ,<nobr> ,text-wrap
hyphens / ­ #
hyphens­The hyphens­
hyphens
This depends on the browser and lang­ .
Note <html lang="en">auto
<wbr> #
<wbr>Somewhat related/similar to ­ ,<wbr><br> !
wbr
These are often needed with slashes/URLs.
white-space: nowrap; / <nobr> / #
white-space: nowrap;<nobr> But much more often, you’ll want to keep certain words together—to avoid a widow or orphan, or to keep important/related text together—like in dates, October 22, or with names like van Zanten.
-
– MDNwhite-space
Prevents text from wrapping. -
– MDNnobr
This is technically deprecated. But one of your instructors is a “stan.”
You can keep multiple words (or whole phrases) together with white-space: nowrap; .<nobr><em><strong>
You can also use a manual
On a Mac, you can insert an encoded
text-wrap: balance; #
text-wrap: balance;After many, many years of patient, typographic waiting (and some JS shenanigans) we now have widespread browser support for “balancing” uneven line lengths with text-wrap: balance;
text-wrap
Don’t overuse/abuse this in running text, though!
This is particularly noticeable (and helpful) for centered text! There is also pretty
When in doubt, The Elements of Typographic Style explains many of these conventions.
But also, as Bringhurst says, “read the text before designing it.” Always put yourself in the mind of your reader!
Overflows and Scrolling#
overflow #
overflowAn overflowblock-sizeinline-size .
– MDNoverflow- Overflowing Content – MDN
What to do when things don’t fit.
But we can use this behavior intentionally to crop our content, or create internally-scrolling areas:
overflow / sticky #
overflowstickyImportantly, overflowpositionposition: sticky; )overflow
Stacking Context – MDN
Always confusing; think of these as frames or groups in Figma.
text-overflow / -webkit-line-clamp #
text-overflow-webkit-line-clampYou can also excerpt text (perhaps on a landing page) with the text-overflow-webkit-line-clamp
-
– MDNtext-overflow
For single lines… -
– MDN-webkit-line-clamp
And multiple lines!
This -webkit
Visual and Compositing Effects#
filter #
filterCSS can apply visual and compositing effects on elements—adjusting their graphical display after they are laid out and rendered in the page—with the filter
filter
Applied after everything else.
Note that multiple filters are applied in sequence—changing the order changes the end/rendered result.
backdrop-filter / mix-blend-mode #
backdrop-filtermix-blend-modeThese have corresponding backdrop-filteropacitycolor )mix-blend-mode
– MDNbackdrop-filter – MDNmix-blend-mode
Often used together.
These are “hot rn.” Always mind your legibility/readability, though!
transform #
transformTransform function() #
function()Beyond our standard sizing and layout afforded by CSS, you can also visually manipulate elements using CSS transforms—scaling, skewing, translating, or rotating elements after they are laid out in the DOM. It’s like grabbing the “corner handles” in Adobe/Figma!
-
scale()/scaleX()/scaleY()/scaleZ()/scale3d() -
Change the displayed size of the element—as if it is an image.
-
skew()/skewX()/skewY() -
Tilt an element to the left or right, like turning a rectangle into a parallelogram.
-
translate()/translateX()/translateY()/translate3d() -
Move an element left/right and up/down, and also in three-dimensional space.
-
rotate()/rotate3d() -
perspective() -
Doesn’t affect the element itself, but sets the distance between the user and the
three-dimensional plane.
The units for these are all a bit different; MDN is your friend here, as usual!
-
– MDNtransform
These are fun—but should not be used for actual layout! -
type – MDN<transform-function>
The different types.
Multiple Transforms#
You can also have multiple transforms on a single element, which are written space-separated (no comma!) and then are applied one after the other:
.rotated {
transform: rotate(-5deg);
}
.rotated-and-scaled {
transform: rotate(-5deg) scale(120%);
}
Individual Properties#
The most commonly used transformtranslaterotatescale
You just pull the values out of the function, without the () .transform
.transformed {
transform: translate(1rem 1rem) rotate(-5deg) scale(120%) skew(5deg);
}
.transformed-individually {
translate: 1rem 1rem;
rotate: -5deg;
scale: 120%;
transform: skew(10deg);
}
Okay, What Do They Do?#
Keep in mind that these transformations are also applied after the rest of the CSS is parsed (like filteroverflowtransform
Note how the elements don’t take up more space in the document flow/layout—but they do cause an overflow!
And Motion!#
“Icing on the Cake”#
Motion can add a lot to our work—making it feel less static, more interactive, and more alive! But should always be considered a progressive enhancement on top of work that functions without it.
Inspect and modify CSS animation effects – web.dev
Use the Animations tab in your DevTools to diagnose motion.
Honor Reader Preferences#
Relatedly, some folks might also want a motion-free experience, whether for accessibility or just preference—so we’ll always “scope” our transitions and animations to those who don’t mind, using @media
Sometimes less movement is more - web.dev
Don’t force anything on your visitor!
Avoid Causing Reflows#
Many CSS properties can be transitioned/animated—but keep in mind that changes that cause a reflow (re-triggering layout, sometimes called paint) are slow and can make your page feel glitchy—especially when you start having many of them, or they’re easy to trigger (like :hover )
Why are some animations slow? - web.dev
Avoid expensive/slow reflows!
Each and every in-between state causes the browser to re-render your entire document! So always stick to changes of color ,opacity ,transform
transition #
transitionCSS transitions allow us to move nicely between changing CSS property values—triggered by interaction.
-
Using CSS Transitions – MDN
Every state change is better with some easing. -
An Interactive Guide to CSS Transitions
Nice run-through from Josh Comeau. -
Easing Functions
Custom curves for acceleration control.cubic-bezier()
Instead of having a property take effect immediately when a change is applied, we can tell a CSS property to transition from one value to another over a given amount of time (a duration), and with a specific acceleration (a timing-function), or after some time (a delay). Motion can quickly get very complex!
You Need Two States to Transition!#
Frequently, CSS transitions will be used with JavaScript (later, for us) when adding/removing classes—to make a state change (before vs. after) less abrupt.
For now, we’ll use pseudo-classes to demonstrate different states, the most common being :hover :
:hover
Shorthand?#
You’ll often see a transition
.some-cool-transition {
transition: all 2s 1s linear;
}
.some-cool-transition {
transition-delay: 1s;
transition-duration: 2s;
transition-property: all;
transition-timing-function: linear;
}
Multiple Transitions?#
You can also control how different properties of an element transition independently, with a comma-separated list:
.some-cool-transition {
transition: background-color 2s linear, transform 1s ease-in-out;
}
.some-cool-transition {
transition-duration: 2s, 1s;
transition-property: background-color, transform;
transition-timing-function: linear, ease-in-out;
}
The shorthand here is sometimes easier than discrete properties, where you have to maintain the same order across all of them. It’s all the same to the computer!
animation #
animationSometimes, transitioning a property from one value to another isn’t enough—you may need more complicated (or repeating) motion behavior. CSS animation
Using CSS Animations – MDN
Make things feel more alive.
Keyframes#
To create a keyframe animation, we define an element’s initial (or motion-free) state normally in CSS—then animation-animation-durationanimation-iteration-count ,animation-name
@keyframes
Akin to other timeline-based animations, the percentage complete.
We then define the actual keyframes of an animation in a separate at-rule. Each @keyframes
Don’t go overboard! A little animation goes a long way.
Scroll-Driven Animations#
CSS animations have historically been time-based—just playing out as it passes. But recently Chrome and Safari added support for scroll-driven animations—which instead iterate using the document (or container) scroll position to control the timeline.
-
Scroll-driven animations – MDN
One of the most-common types, no-JS! -
Scroll-Driven Animations
Josh again, with a nice explainer.
Previously, this required some JavaScript—but is such a common scenario that it’s been added to CSS. (Designers love this sort of thing!) It adds the animation-rangeanimation-timeline
But: if everything is animated, is anything animated?
Shorthand, Again?#
Again, you’ll very commonly see animation
section {
animation: pulse-slide 5s infinite ease-in-out;
}
section {
animation-duration: 5s;
animation-iteration-count: infinite;
animation-name: pulse-slide;
animation-timing-function: ease-in-out;
}
With great power there must also come great responsibility.