This simple workaround addresses the bug in Chrome by providing a blank

element, preventing the unnecessary initiation of transitions during page load.

","image":"http://www.luping.net/uploads/20241115/173166521767371d4106b00.jpg","datePublished":"2024-11-15T18:24:27+08:00","dateModified":"2024-11-15T18:24:27+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"

How to Prevent CSS Transitions from Triggering on Page Load in Chrome?

Published on 2024-11-15
Browse:654

How to Prevent CSS Transitions from Triggering on Page Load in Chrome?

Suppressing CSS Transition Initiation on Page Load

In certain situations, CSS transitions may activate inadvertently during page load, causing elements to flicker. This issue can arise when applying color transitions on elements.

As per the example provided:

CSS:

p.green {
   color: green;
   transition: color .2s;
}

p.green:hover {
   color: yellow;
}
HTML:

<p class="green">The Flashing Text</p>

Upon page load, the text within the

element transitions from black (the initial color) to green. This behavior is unwanted and can be visually disruptive.

To prevent this unintended transition, a solution involves leveraging a unique aspect of Chrome's behavior. When a page contains a

element, Chrome triggers CSS transitions prematurely. By adding a single-space character within a script tag, the presence of this element can be emulated, thus suppressing the erroneous transition:
<script> </script>

This simple workaround addresses the bug in Chrome by providing a blank

element, preventing the unnecessary initiation of transitions during page load.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3