

Manuel Matuzovic: max() Trickery | CSS-Tricks - CSS-Tricks
source link: https://css-tricks.com/manuel-matuzovic-max-trickery/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Manuel Matuzovic: max() Trickery
Let’s put those CSS skills to work! Claim $50 in free hosting credit on Cloudways with code CSSTRICKS.
By way of a post by Manuel Matuzović which is by way of a demo by Temani Afif.
.wrapper {
margin-inline: max(0px, ((100% - 64rem) / 2));
}
You’d be doing yourself a favor to read Manuel’s breakdown of all what’s happening here, but it basically works out to the equivalent of this longer syntax:
.wrapper {
max-width: 64rem;
margin: 0 auto;
width: 100%;
}
…where:
- max() acecepts a comma-separated list of CSS numeric values, where the applied value is the largest (or as MDN puts it, the “most positive”) one in the set.
0px
is the first value in the set, ensuring that the smallest value is always going to be greater than zero pixels.(100% - 64rem)
is the second “value” in the set, but is expressed as a calculation (note thatcalc()
is unnecessary) that subracts the themax-width
of the element (64rem
) from its full availablewidth
(100%
). What’s left is the space not taken up by the element.((100% - 64rem) / 2))
divides that remaining space equally since we’re divying it between the inline boundaries of the element.max(0px, ((100% - 64rem) / 2))
compares0px
and(100% - 64rem) / 2)
. The largest value is used. That’ll be the result of the equation in most cases, but if64rem
is ever greater than the computed value of the element’s full100%
width, it’ll lock that value at zero to ensure it never results in a negative value.margin-inline
is the property that the winning value sets, which applies margin to the inline sides of the element — that’s the logical shorthand equivalent to setting the same value to themargin-left
andmargin-right
physical properties.
It’s the same sort of idea Chris shared a while back that uses the CSS max()
function to solve the “Inside Problem” — a container that supports a full-bleed background color while constraining the content inside it with padding
.
max()
, calc()
, margin-inline
… that’s a lot of newfangled CSS! And Manuel is right smack dab in the middle of writing about these and other modern CSS features over 100 days.
</div
Recommend
-
58
Open Source and New Year’s ResolutionsLate in 2016, I began to scratch a career-long itch. I had recently decided that I simply wouldn’t do anymore WordPress projects. I had too often pushed the blogging pla...
-
16
Just a quick update before boarding to Lyon for TPAC 2018 . This year 12 igalians will be at TPAC, 10 employees (Álex Gar...
-
8
theme-color Meta Theme Color and Trickery Manuel Matuzovic on Jul 13, 2021 (Updated on Jul 14, 20...
-
12
Curiosum Posted on Oc...
-
6
Zero Trickery Custom Radios and Checkboxes Chris Coyier on Nov 17, 2021 Grow sales with a smart marketing platform.
-
9
specificity Manuel Matuzovic’s CSS Specificity Demo Chris Coyier on Feb 25, 2022
-
5
Day 12: max() trickery posted on October 11., 2022 It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve star...
-
6
Fortnite settles child privacy and trickery claimsPublished1 day ago
-
5
GPU miners trickery: Watch out for painted memory on used graphics cards Sellers are trying to pass off discolored mining GPUs as new or "like new" By
-
8
Impact In runc 1.1.11 and earlier, due to an internal file descriptor leak, an attacker could cause a newly-spawned container process (from runc exec) to have a working directory in the host filesystem namespace, allowing for...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK