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.

William of Ockham, 1324

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.

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-visible pseudo-class, usually adding an outline:

  • #
  • <>
  • ↗
  • Click in the example, and then use Tab to move between links. Ensure the focus works with your site design!

    Advanced Typography#

    Precise, Optical text-box #

    You 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).

    It is based on the font-family , the font-size , and the line-height , which basically means it is different all the time⁠—and crucially, often different from Adobe/​Figma to HTML. This makes it difficult to position type visually and precisely⁠—especially at large, expressive sizes like your headings! It’s always annoying, and you’ll often be adding/​subtracting your spacing ( margin or padding ) to account for it, if you want to line everything up just right, optically.

    But we can avoid it, now! After a decade of workarounds and discussion, browsers recently added support for the super-easy text-box properties to give us control:

  • #
  • <>
  • ↗
  • You you might consider doing this on everything with the shorthand * { text-box: trim-both cap alphabetic; } ! Note though that Chrome doesn’t apply it on inline elements, for some reason.

    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 and ::after – CSS Tricks
    Another great CSS Tricks article.

    There is actually a hanging-punctuation property for this⁠—but only Safari supports it! But we can still approximate the behavior in a few ways, at least for quotes:

  • #
  • <>
  • ↗
  • Note that the (pasted-in) curly quotation marks are not in the HTML here in the last example! Pseudo-elements again, ::before and ::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 / &shy; , <wbr> , white-space , <nobr> and &nbsp; , and text-wrap to somewhat control our line breaks, for better readability.

    hyphens /​ &shy; #

    The hyphens property allows long, multi-syllable words to be hyphenated when they wrap across multiple lines. This can be done automatically by the browser, or by manually inserting &shy; (for soft hyphen) as an HTML character/​entity references:

    hyphens - MDN
    This depends on the browser and lang, unless you manually add &shy; .

  • #
  • <>
  • ↗
  • Note <html lang="en"> is needed⁠—as the auto property works from each browser’s different, internal (and usually, only English) dictionary⁠—so this all has somewhat limited utility/​reliability/​consistency.

    <wbr> #

    Somewhat related/​similar to &shy; , the <wbr> is a (void/​empty) HTML element that denotes a word break opportunity⁠—a bit like an optional <br> ! You can use these to manually control where single long word will wrap, without a hyphen:

    wbr – MDN
    These are often needed with slashes/​URL⁠s.

  • #
  • <>
  • ↗
  • white-space: nowrap; /​ <nobr> /​ &nbsp; #

    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.

    You can keep multiple words (or whole phrases) together with white-space: nowrap; . Historically, the <nobr> tag applied this⁠—keeping in mind that like <em> or <strong>the default behavior is cleared by most resets (ours included)⁠—so likewise you have to restore the property in your own CSS.

    You can also use a manual &nbsp; character/​entity reference between words:

  • #
  • <>
  • ↗
  • On a Mac, you can insert an encoded &nbsp; with ⌥+Space . (It’s apparently much harder on Windows!) This works in many programs, not just your IDE! It’s harder to see, but easier to read.

    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 – MDN
    Don’t overuse/​abuse this in running text, though!

  • #
  • <>
  • ↗
  • This is particularly noticeable (and helpful) for centered text! There is also pretty, but Safari just started to support it and Firefox still doesn’t.

    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 #

    An overflow in CSS happens when there is too much content to fit in a container⁠—usually because you have manually constrained its block-size or inline-size . (By default, the browser will try to show you everything!)

    But we can use this behavior intentionally to crop our content, or create internally-scrolling areas:

  • #
  • <>
  • ↗
  • overflow /​ sticky #

    Importantly, overflow creates a new stacking context⁠—which means things with position (namely, position: sticky; )⁠—and some other properties⁠—will now use the overflow container as their reference/​origin:

    Stacking Context – MDN
    Always confusing; think of these as frames or groups in Figma.

  • #
  • <>
  • ↗
  • text-overflow /​ -webkit-line-clamp #

    You can also excerpt text (perhaps on a landing page) with the text-overflow (for a single line) or -webkit-line-clamp (for multiple lines) properties⁠—which will add an ellipsis … where the text overflows. Only do this when the full text is available on a subsequent page, though:

  • #
  • <>
  • ↗
  • This -webkit stuff is hokey, but still how it is done! Browsers are weird.

    Visual and Compositing Effects#

    filter #

    CSS 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 property:

    filter – MDN
    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 #

    These have corresponding backdrop-filter values⁠—which apply the effect to the page behind an element! You’ll often use these in conjunction with an opacity (or semi-transparent color ) or a mix-blend-mode for interesting Photoshop/​Figma-like layer-blending effects:

  • #
  • <>
  • ↗
  • These are “hot rn.” Always mind your legibility/​readability, though!

    transform #

    Transform 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()

    Spin the element.

    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!

    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 transform types now also have their own separate, individual properties: translate, rotate, and scale⁠—which are applied in that order!

    You just pull the values out of the function, without the () . This can make complicated transform combinations much easier to read/​understand:

    .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 filter), and thus treat your element a bit like an image. And like overflow above, transform also creates a new stacking context for its children:

  • #
  • <>
  • ↗
  • 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 queries.

    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 , and transform for the smoothest performance.

    transition #

    CSS transitions allow us to move nicely between changing CSS property values⁠—triggered by interaction.

    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 changes can often use a bit of coyote time to feel less flickery/​glitchy!

    Shorthand?#

    You’ll often see a transition in shorthand, but again it can be clearer with the discrete properties:

    .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 #

    Sometimes, transitioning a property from one value to another isn’t enough⁠—you may need more complicated (or repeating) motion behavior. CSS animation allows precise state sequencing with keyframes, a timeline-based concept from animation (and other software).

    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- properties, which include animation-duration and animation-iteration-count , as well as an animation-name (something that you make up).

    @keyframes at-rule – MDN
    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 is specified with a percentage of the animation’s duration, and can specify multiple properties⁠—a bit like selectors, but for time:

  • #
  • <>
  • ↗
  • 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.

    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-range and animation-timeline properties for control:

  • #
  • <>
  • ↗
  • But: if everything is animated, is anything animated?

    Shorthand, Again?#

    Again, you’ll very commonly see animation shorthand⁠—but again, writing out the separate properties can make things more clear⁠—or maybe you just want to adjust one aspect:

    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.

    Stan Lee, 1962