Home >Web Front-end >CSS Tutorial >How Do I Debug CSS `calc()` Values?

How Do I Debug CSS `calc()` Values?

Linda Hamilton
Linda HamiltonOriginal
2024-12-14 02:15:09489browse

How Do I Debug CSS `calc()` Values?

How to debug CSS calc() values?

Problem

I have a relatively complex formula, for example: transform: scale(var(--image-scale)) translateY( calc((1px var(--element-height) (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y)))

How to debug the calculated value?
Also, is there a way to verify/highlight formula errors?

I tried this

 position: fixed;</p>
<pre class="brush:php;toolbar:false">display: block;
left:0;
right: 0;
background: yellow;
padding: 5px;
z-index: 100;
content: calc((1px * var(--element-height) * (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y));

Didn’t work

The only way I found was to Part of the calculation is placed in an unused numeric attribute, for example in the gif below background-position-x so it will display the calculated value on the calculation tab - useful but not very convenient (notice background-position-x changes as the page scrolls):

enter image description here
<div>


<pre class="brush:php;toolbar:false">cssProps: true

})
const results = Splitting ();

var parallaxedElements = document.querySelectorAll('.section');

document.addEventListener('scroll', function(e) {

parallaxedElements
Array.from(parallaxedElements).forEach((el) => {
  var bcr = el.getBoundingClientRect();
  if (bcr.y < 0 &amp;&amp; Math.abs(bcr.y) <= bcr.height) {
    el.style.setProperty("--scrolled-out-y", Math.round(Math.abs(bcr.y) * 10000 / bcr.height) / 10000);
  }
});
</p>
<p>})

< pre>html {

scroll-behavior: smooth;

}

body {

font-family: "Roboto";
font-size: 14px;
line-height: 1.4;
scroll-behavior: smooth;

}

.section {

position: relative;
background-attachment: fixed;
z-index: 1;
--image-scale: 1.2;
--scrolled-out-y: 0;

}

.section__background {

position: -webkit-sticky;
position: sticky;
top: 0;
width: 100%;
height: 100vh;
overflow: hidden;

}

.section__background:after {

content: "";
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
z-index: 1;
background: linear-gradient(to bottom, black, 100% white);
background: rgba(0, 0, 0, 0.4);
opacity: calc(1 + ((var(--viewport-y) * 1.5)));

}

.section__background>img {

height: 150vh;
width: 100%;
object-fit: cover;
position: absolute;
left: 0;
top: 0px;
user-select: none;
transform: scale(var(--image-scale)) translateY(calc((-1px * var(--element-height) * (var(--image-scale) - 1)) * var(--scrolled-out-y)));

}
/* .indicator:after {

    position: fixed;
    display: block;
    left: 0;
    right: 0;
    background: pink;
    padding: 5px;
    z-index: 100;
    content: calc((1px * var(--element-height) * (var(--image-scale) - 1)) / 2 * var(--scrolled-out-y));
} */

.section__container {

padding-bottom: 50vh;
overflow: hidden;
align-items: flex-start;
position: relative;
z-index: 4;

}

.section__heading {

color: #fff;
text-transform: uppercase;
font-size: 45px;
line-height: 1.2;
font-weight: 800;
letter-spacing: 8px;
margin: 0;
overflow: hidden;
position: relative;
padding-bottom: 50px;
margin-bottom: 50px;

}

.section__heading :after {

content: "";
position: absolute;
top: 200px;
left: 0;
right: 0;
height: 2px;
transform: translateX(calc(var(--scrolled-out-y) * 100% - 70%));
background: #b38c6b;

}

.section__content {

display: flex;
color: white;
flex-direction: column;

}

.section__content p p {

margin-top: 20px;

}

.splitting {

--char-percent: calc(var(--char-index) / var(--char-total));

}

.splitting .char {

display: inline-block;
opacity: calc(1 + ((var(--viewport-y) * 1.5) - var(--char-percent)));

}

<link rel=' stylesheet' href='https://unpkg.com/splitting/dist/splitting.css'></p>
<p><section data-scroll> <div class="section__background"></p>
<pre class="brush:php;toolbar:false"><div>


<div>
<div>


The above is the detailed content of How Do I Debug CSS `calc()` Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Why Doesn't Margin-Top Work on Span Elements in CSS?Next article:Why Doesn't Margin-Top Work on Span Elements in CSS?

Related articles

See more