Library

30 highlights

Design and the web

CSS Secrets

Lea Verou

Marked passages, in reading order

Highlights

  1. Location 387

    and for

  2. Location 737

    #58a); border-radius: 4px;

  3. Location 742

    this code that we can fix. The low-hanging fruit is the font metrics. If

  4. Location 831

    The inherit keyword can be used in any CSS property and it always corresponds to the computed value of the parent element (in pseudo-elements that is the element they are generated on). For example, to give form elements the same font as the rest of the page, you don’t need to re-specify it, just use inherit:

  5. Location 863

    for the

  6. Location 886

    (especially if we take future devices into account) that a website should

  7. Location 892

    Here are a few more tips to avoid needless media queries: Use percentages instead of fixed widths. When that’s not possible, use viewport-relative units (vw, vh, vmin, vmax), which resolve to a fraction of the viewport width or height. When you want a fixed width for larger resolutions, use max-width, not width, so it can still adapt to smaller ones without media queries. Don’t forget to set a max-width of 100% for replaced elements such as img, object, video, and iframe.

  8. Location 966

    the CSS parser knows what you mean. However, in the shorthand, the

  9. Location 1057

    able to adjust borders in CSS code, so developers ended up resorting to ugly hacks such as using multiple elements to emulate

  10. Location 1060

    solution By now, most of us have probably (over)used box-shadow to

  11. Location 1073

    deeppink;

  12. Location 1098

    #655; outline: 15px solid deeppink; Another good thing about outlines is that you can control

  13. Location 1121

    with offsets from a different corner than the top-left one, such as

  14. Location 1216

    solution The previous solution is more flexible, as it allows us to use

  15. Location 1312

    to

  16. Location 1729

    FIGURE 2.53 Our original attempt at pseudorandom

  17. Location 3068

    specifically

  18. Location 3916

    punctuation—the possibilities are endless!

  19. Location 3941

    } While emulating a text underline with border-bottom

  20. Location 3972

    In the future, we will not have to resort to such hacks for customizing our underlines. There are several properties planned in CSS Text Decoration Level 3 (w3.org/TR/css-text-decor-3), specifically for this, such as:

  21. Location 4389

    (Human-Computer Interaction) principle. This may sound surprising

  22. Location 5035

    <p>Some text […]</p> <figure>     <img src="adamcatlace.jpg" />     <figcaption>         The great Sir Adam Catlace was named after         Countess Ada Lovelace, the first programmer.     </figcaption> </figure> <p>More text […].</p>

    Note html snippet for a figure

  23. Location 5060

    A relatively new specification, CSS Intrinsic & Extrinsic Sizing Module Level 3 (w3.org/TR/css3-sizing), defined several new width and height keywords, one of the most useful of which was min-content. This keyword gives us the width of the largest unbreakable element inside the box (i.e., the widest word or image or fixed-width box). This is exactly what we need! Now, giving our figures an appropriate width and horizontally centering them as simple is two lines of code:

    Note min-content gives us the width of the largest unbreakable element inside the box (i.e., the widest word or image or fixed-width box). max-content, would give us the same width as we saw with display: inline-block fit-content gives us the same behavior as floats (which is often the same as min-content, but not always). This is exactly what we need! Now, giving our figures an appropriate width and horizontally centering them as simple is two lines of code: figure { width: min-content; margin: auto; } and with fallbacks figure { max-width: 300px; max-width: min-content; margin: auto; } figure > img { max-width: inherit; }

  24. Location 5065

    max-content, would give us the same width as we saw with display: inline-block

  25. Location 5066

    floats (which is often the same as min-content, but not always).

  26. Location 5068

    figure {     width: min-content;     margin: auto; }

    Note Awesome!

  27. Location 5072

    To offer a graceful fallback for older browsers, we could combine this technique with a fixed max-width, like so: figure {     max-width: 300px;     max-width: min-content;     margin: auto; } figure > img { max-width: inherit; }

  28. Location 5082

    Hat tip to Dudley Storey (demosthenes.info) for coming up with this use case (demosthenes.info/blog/662/Design-From-the-Inside-Out-With-CSS-MinContent).

  29. Location 5238

    In the past few years, there is a certain web design trend that has been growing in popularity: it’s what I call “fluid background width, fixed content width.” The typical characteristics of this pattern are:

  30. Location 5418

    Note that when using Flexbox, margin: auto doesn’t only center the element horizontally, but vertically as well.

Back to the library