

Day 12: max() trickery
source link: https://www.matuzo.at/blog/2022/100daysof-day12/
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.

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 started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.
I saw this interesting one-liner in a demo by Temani Afif.
.wrapper {
margin-inline: max(0px, ((100% - 64rem) / 2));
}
It’s basically a shorter way of writing this:
.wrapper {
max-width: 64rem;
margin: 0 auto;
width: 100%;
}
It’s not up to me to decide whether it’s smart to use this in production or not, but I want to break it down to fully understand what’s going on.
Let’s work our way from the inside out.
(100% - 64rem)
This takes the available width and subtracts the maximum width of the wrapper. What’s left is the remaining space.
((100% - 64rem) / 2)
We take the remaining space and divide it by 2. We have to divide it because we want to use it for the left and right margin of the wrapper.
max(0px, ((100% - 64rem) / 2));
If 100% is less than 64rem, we would get a negative number, which would break the layout. The max() function ensures that the margin
is always at least zero. It takes a comma separated list of expressions. The largest value in the list will be selected. If the value of our calculation is less than 0, max()
takes 0 instead because it’s larger than the negative number.
margin-inline: max(0px, ((100% - 64rem) / 2));
margin-inline sets both the left and the right margin to either 0 or our calculated value, which centers the element.
Recommend
-
9
Site of the Day : Max Central Application Thursday, November 20, 2003 Congratulations to Eric Dolecki for getting site of the day...
-
6
theme-color Meta Theme Color and Trickery Manuel Matuzovic on Jul 13, 2021 (Updated on Jul 14, 20...
-
9
Curiosum Posted on Oc...
-
5
Zero Trickery Custom Radios and Checkboxes Chris Coyier on Nov 17, 2021 Grow sales with a smart marketing platform.
-
5
Day 5: the max() function posted on September 30., 2022 It’s time to get me up on speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve...
-
8
Manuel Matuzovic: max() Trickery Let’s put those CSS skills to work! Claim
-
5
Day 33: Mathematical expressions in min(), max(), clamp() posted on November 9., 2022 It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I...
-
4
Fortnite settles child privacy and trickery claimsPublished1 day ago
-
4
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
-
7
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