Minimize the need for reflows and repaints to enhance CSS performance

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Unique Page</title>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
            will-change: contents;
        }

        * {
            box-sizing: border-box;
        }

        .center {
            position: absolute;
            /* margin: auto; */
            width: 300px;
            height: 300px;
            /* top: 0px;
            left: 0px;
            bottom: 0px;
            right: 0px; */
            outline: orangered 2px solid;
            pointer-events: none;
        }

        .block {
            width: 200px;
            height: 200px;
            background-color: orange;
            position: absolute;
            pointer-events: none;
        }

        .innerBlock {
            width: 100px;
            height: 100px;
            background-color: blue;
            position: absolute;
            pointer-events: none;
        }

        @keyframes drawC {
            0% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }

            25% {
                /* left: calc(100% - 100px);
                top: 0px; */
                transform: translateX(100px) translateY(0px);
            }

            50% {
                /* left: calc(100% - 100px);
                top: calc(100% - 100px); */
                transform: translateX(100px) translateY(calc(100px));
            }

            75% {
                /* left: 0px;
                top: calc(100% - 100px); */
                transform: translateX(0px) translateY(100px);
            }

            100% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }
        }

        @keyframes drawB {
            0% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }

            25% {
                /* left: calc(100% - 200px);
                top: 0px; */
                transform: translateX(100px) translateY(0px);
            }

            50% {
                /* left: calc(100% - 200px);
                top: calc(100% - 200px); */
                transform: translateX(100px) translateY(calc(100px));
            }

            75% {
                /* left: 0px;
                top: calc(100% - 200px); */
                transform: translateX(0px) translateY(100px);
            }

            100% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }
        }

        @keyframes draw {
            0% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }

            25% {
                /* left: calc(100% - 300px);
                top: 0px; */
                transform: translateX(calc(100vw - 300px)) translateY(0px);
            }

            50% {
                /* left: calc(100% - 300px);
                top: calc(100% - 300px); */
                transform: translateX(calc(100vw - 300px)) translateY(calc(100vh - 300px));
            }

            75% {
                /* left: 0px;
                top: calc(100% - 300px); */
                transform: translateX(0px) translateY(calc(100vh - 300px));
            }

            100% {
                /* left: 0px;
                top: 0px; */
                transform: translateX(0px) translateY(0px);
            }
        }
    </style>
</head>

<body>
</body>

</html>
<script>
    const ele = document.body
    const draw = (time) => {
        const template = document.createElement("template");
        template.innerHTML = `<div class="center" style="animation: draw ${time}s linear infinite">
            <div class="block " style="animation: drawB ${time/5}s linear infinite">
                <div class="innerBlock" style="animation: drawC ${time/10}s linear infinite">contained</div>
            </div>
        </div>`
        return template.content.firstChild;
    }
    let time = 10;
    const fragment = document.createDocumentFragment();
    while (++time < 500) {
        fragment.appendChild(draw(time));
    }
    ele.appendChild(fragment);
</script>

Hello everyone! I am facing some challenges while trying to optimize a page on my website. Here are the questions that I have:

  1. Why does Chrome's paint flashing cover all elements at once instead of individually as shown in the image below at the beginning: https://i.sstatic.net/teLpT.png

Eventually, it changes to look like this: a hole in the middle

https://i.sstatic.net/WwOTL.png 2. Mouse events on any element cause a slight decrease in frame rate. How can I optimize this?

  1. Should 'will-change' and 'contain' properties be used in such a circumstance? If yes, how should they be implemented? I do not see any benefit when adding them to div.center. If not, when should these properties be utilized?

  2. Do you have any suggestions for further optimization? I noticed that style recalculations still significantly impact performance.

Answer №1

Perhaps I have stumbled upon a solution:

  1. By adding will-change to the body/html, the issue was resolved
  2. The problem seemed to be caused by the devTool
  3. Although using will-change may lead to more reflow/less painting, it is still unclear why that is the case
  4. Simply using transform without will-change/contain resulted in a significant improvement in performance with painting and rendering time reduced to 0

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes Font Awesome 5 icons to vanish when changing the font-family?

I am facing an issue with the code I have below. It changes the font style successfully, but it also causes the icon to disappear. Can anyone provide tips on how to ensure that the icon remains visible while changing the font style? <link rel="styles ...

Disable browser suggestions in Material UI's Autocomplete component

Encountering an issue with Material-ui Autocomplete: import Autocomplete from "@material-ui/lab/Autocomplete"; Currently using it in: <Autocomplete id="checkboxes-tags-demo" autoComplete={false} options={sta ...

Adjust the spacing between the title and other elements in an R Shiny application

How do I modify the font and size of a title, as well as add some spacing between the title and other elements? navbar <- navbarPage( Title = "Intervals", tabPanel("Data Import", sidebarLayout(sidebarPanel(fi ...

Unknown identifier in the onClick function

I want to create a JavaScript function that can show or hide a paragraph when clicking on an arrow. The challenge I'm facing is that I have a list of titles generated in a loop on the server, and each title is accompanied by an arrow. Initially, the c ...

Modifying the display property of an element using JavaScript

Hello, I'm encountering an issue with a section of my javascript code. I am attempting to make the #showAddress element display as block when the deliverservice radio button is clicked or checked. I have tried searching for solutions on Stack Overflow ...

What could be causing the svg to not be visible inside the div?

I am struggling to display an SVG element within a div as it is not visible at all. Can someone help me make the SVG visible in the div? Here is a link to the code: http://codepen.io/helloworld/pen/HjkhK <div style="height:100px;background:yellow;"> ...

What is the best way to ensure that the same information is included on every page of

Currently developing a dynamic website where certain components such as the fixed header, menubar, and footer are common across multiple pages. What is the best way to include these fixed components on every webpage? I am utilizing JSP and JavaScript for ...

Background cutting off right side of HTML website

While updating my supervisor's university website, I noticed a problem with the responsiveness. When resizing the browser window, a horizontal scroll bar would appear and the right side of the website would get covered by the background. On mobile dev ...

Tips for fading the text of list items when their checkbox is marked as done?

I am trying to figure out how to gray out a list item when its checkbox is checked. The code I currently have takes text input from a textbox and adds it to an unordered list when the add button is clicked. Each list item contains a checkbox within it. My ...

Creating parallel lines utilizing pseudo elements

Can someone assist me in drawing double lines under my paragraph using pseudo elements only without using display block? Here is my code snippet with display block: p:after { content: ""; display: block; width: 40px; margin: 20px auto 0; bord ...

Submit information from an HTML form to a PHP script and then send the response back to the client using AJAX,

Looking to run a PHP script using AJAX or JavaScript from an HTML form and then receive the results on the HTML page. This is what my changepsw.php file looks like: <?php // PHP script for changing a password via the API. $system = $_POST['syst ...

Trigger a function upon the addition of a class to a div element | Execute AnimatedNumber()

Is there a way to trigger a function when a specific div receives a certain class? I have an animated div that only appears when scrolling, and once it's visible, it gains the class "in-view". I want to execute a function every time this div has the ...

What steps should we take to ensure that the container beside the fixed left navigation bar is responsive?

Currently, I am working on creating a user interface that features a fixed navigation bar on the left hand side and a responsive content window. Here is what I have implemented so far: <div style="width: 15%; float: left; display: inline;"> <na ...

Using HTML and CSS to Dynamically Alter Cell Text Color Based on Value

Unfortunately, I am unable to find a straightforward solution. My goal is to adjust the text color inside my cell based on its value. Here is a basic table: <table> <tbody> <tr> <td>Quantity</td> <td> ...

Jquery not functioning properly for show and hide feature

I'm new to using Jquery and JqueryUI. I have a div named front, which I want to initially display on window load and then hide it by sliding after a delay of 5500 milliseconds. However, I'm encountering errors in the jquery.min.js file. The HTML ...

What could be the reason for Python requests giving me a different text output compared to when I manually visit the website?

I'm in the process of creating a basic 'stock-checker' for a T-shirt that I have my eye on. The link to the product is available here: https://i.stack.imgur.com/usSJN.jpg Upon visiting the page, I noticed that it displays 'Coming Soon ...

Transitioning CSS for transformative text effects

Is there a way to create an effect similar to the "HOVER ME" animation on a div? I want the text to move down and another text to appear from above when hovering over the div, without separate letters. The transition should be seamless and reversed when th ...

show information on a separate screen following the selection of a choice

After selecting an option, how can I make #formid appear where the form is filled in with the stock selected in the option section? <div class="form-group"> <label for="exampleFormControlSelect1">Item Name</label> <select clas ...

Troubleshooting Problem with HTML Links in jQuery Accordion

After experimenting with adding a link to the HTML section of the accordion, I discovered that the link appears bold and does not open. I attempted to create a class or ID to fix this issue, but my efforts were unsuccessful. http://jsfiddle.net/q2Gm9/ ...

Ways to capture all characters (including special characters like ) using Visual Studio Regex

What I'm Hoping to Achieve: I am looking to reformat multiple HTML files that have a similar structure in Visual Studio. While the HTML sections may vary in length, they all begin with a <div id="some_id"> tag and do not contain any other <d ...