
CSS Secrets
Lea Verou
Marked passages, in reading order
Highlights
-
Location 387
and for
-
Location 737
#58a); border-radius: 4px;
-
Location 742
this code that we can fix. The low-hanging fruit is the font metrics. If
-
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:
-
Location 863
for the
-
Location 886
(especially if we take future devices into account) that a website should
-
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.
-
Location 966
the CSS parser knows what you mean. However, in the shorthand, the
-
Location 1057
able to adjust borders in CSS code, so developers ended up resorting to ugly hacks such as using multiple elements to emulate
-
Location 1060
solution By now, most of us have probably (over)used box-shadow to
-
Location 1073
deeppink;
-
Location 1098
#655; outline: 15px solid deeppink; Another good thing about outlines is that you can control
-
Location 1121
with offsets from a different corner than the top-left one, such as
-
Location 1216
solution The previous solution is more flexible, as it allows us to use
-
Location 1312
to
-
Location 1729
FIGURE 2.53 Our original attempt at pseudorandom
-
Location 3068
specifically
-
Location 3916
punctuation—the possibilities are endless!
-
Location 3941
} While emulating a text underline with border-bottom
-
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:
-
Location 4389
(Human-Computer Interaction) principle. This may sound surprising
-
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
-
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; }
-
Location 5065
max-content, would give us the same width as we saw with display: inline-block
-
Location 5066
floats (which is often the same as min-content, but not always).
-
Location 5068
figure { width: min-content; margin: auto; }
Note Awesome!
-
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; }
-
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).
-
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:
-
Location 5418
Note that when using Flexbox, margin: auto doesn’t only center the element horizontally, but vertically as well.