diff --git a/content/fancy-title-divider/index.md b/content/fancy-title-divider/index.md index ec6000fe..1b8e306a 100644 --- a/content/fancy-title-divider/index.md +++ b/content/fancy-title-divider/index.md @@ -1,6 +1,6 @@ --- layout: layouts/post.njk -title: A fancy title divider I +title: A fancy title divider description: Use background and border-image to create a fancy title divider date: 2022-08-25 tags: posts diff --git a/content/length-to-integer/index.md b/content/length-to-integer/index.md new file mode 100644 index 00000000..cc8b0ba2 --- /dev/null +++ b/content/length-to-integer/index.md @@ -0,0 +1,55 @@ +--- +layout: layouts/post.njk +title: Remove the unit from any CSS value +description: A hacky CSS trick to transform an length value into an integer +date: 2024-09-19 +tags: posts +--- + +Do you want to convert a length value to a unitless value? + +It's possible to remove the unit from any CSS value to get an integer! + +⚠️ A hacky experimentation to use with caution ⚠️ + +```css +@property --unitless-val { + syntax: ''; + inherits: true; + initial-value: 0; +} +@property --_u { + syntax: ''; + inherits: true; + initial-value: 0; +} +:root { + --val: 25em; /* use any length unit (px, em, ex, ch, rem, ...)*/ + + /* we multiply then divide with the biggest value to get one unit */ + --_m: 3.35544e07; /* this value depend on the browser */ + --_u: calc(var(--val)*var(--_m)); + --unit: calc(var(--_u)/var(--_m)); /* = 1em */ + + --unitless-val: tan(atan2(var(--val),var(--unit))); /* = 25 */ + /* In the near future we can do + --unitless-val: calc(var(--val)/var(--unit)); + */ +} +/* we need a different value for firefox */ +@supports (-moz-appearance: none) { + :root { + --_m: 3.40282e38; + } +} +``` +You may think we can use `infinity` as the biggest value but it won't work because dividing by infinity will always result in 0. + +Tested on Firefox, Chrome and Edge. On Firefox, it works only with `px` because the value is converted to `px` inside the `--_u` variable. + +

+ See the Pen + Remove the unit from any CSS value! by Temani Afif (@t_afif) + on CodePen. +

+ \ No newline at end of file diff --git a/content/number-inside-box/index.md b/content/number-inside-box/index.md index d0249bfc..bbe9dd17 100644 --- a/content/number-inside-box/index.md +++ b/content/number-inside-box/index.md @@ -41,7 +41,7 @@ Make your numbers look fancy by placing each digit inside its own box. background: conic-gradient(from 90deg at var(--b) var(--_g)), conic-gradient(from -90deg at right calc(var(--b) + var(--g)) bottom var(--_g)); - background-size: calc(1ch + var(--w)) 100%; + background-size: calc(1ch + var(--w)) 100%; } ``` diff --git a/content/reveal-hover-mask-2/index.md b/content/reveal-hover-mask-2/index.md index 1d06efe0..2fcec441 100644 --- a/content/reveal-hover-mask-2/index.md +++ b/content/reveal-hover-mask-2/index.md @@ -27,7 +27,7 @@ img { transition: .5s; } img:hover { - -webkit-mask-position: 0% 0%,100% 100%; + mask-position: 0% 0%,100% 100%; } /* we update the angle for the alternative version */ img.alt {