Day 12: max() trickery

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;
w…


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

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.

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2022-10-11T00:00:00+00:00) Day 12: max() trickery. Retrieved from https://www.scien.cx/2022/10/11/day-12-max-trickery-2/

MLA
" » Day 12: max() trickery." Manuel Matuzović | Sciencx - Tuesday October 11, 2022, https://www.scien.cx/2022/10/11/day-12-max-trickery-2/
HARVARD
Manuel Matuzović | Sciencx Tuesday October 11, 2022 » Day 12: max() trickery., viewed ,<https://www.scien.cx/2022/10/11/day-12-max-trickery-2/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 12: max() trickery. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/11/day-12-max-trickery-2/
CHICAGO
" » Day 12: max() trickery." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2022/10/11/day-12-max-trickery-2/
IEEE
" » Day 12: max() trickery." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2022/10/11/day-12-max-trickery-2/. [Accessed: ]
rf:citation
» Day 12: max() trickery | Manuel Matuzović | Sciencx | https://www.scien.cx/2022/10/11/day-12-max-trickery-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.