Lessons

Background image flickering on hover

Specifying one file as the background for an element and a second file for the hover effect is bad practice. To download any file, the browser needs to spend at least 0.1 seconds even on the fastest Internet connection. It turns out that at this very moment the element remains without a background. It looks like a blink, flicker or glitch of the background image.

Here’s what code causes the flicker effect:

.block {
    background-image: url(./photo.jpg);
}
.block:hover {
    /*When a background is specified in a style with a pseudo-class, it causes a display delay*/
    background-image: url(./photo-hover.jpg);
}

Such a defect is perceived as a visual defect, and worsens the image of the site in the eyes of the visitor.

Interactive example:

Demonstration
PHP code
<?php
if(isset($_GET['photo'])) {
    usleep(100000);
    header('Content-Type: image/jpeg');
    echo file_get_contents('photo-hover.jpg');
    die;
}
?><!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <style>
            .block {
                width: 300px;
                height: 300px;
                background-size: contain;
                background-image: url(./photo.jpg?&t=<?=microtime(true)?>);
            }
            .block:hover {
                background-image: url(./background_is_changing.php?photo=1&t=<?=microtime(true)?>);
            }
            .btn {
                color: #000;
                display: inline-block;
                margin-bottom: 5px;
                text-decoration: none;
                padding: 5px 10px;
                border: 1px solid #000;
                border-radius: 5px;
            }
            .btn:hover {
                background-color: #ddd;
            }
            .btn.active {
                background-color: #000;
                color: #fff;
            }
        </style>
    </head>
    <body style="font-family: sans-serif;">
        <a class="btn" href="./background_is_changing.php">Обновить страницу</a><br />
        <h1>Наведите курсор на картинку, чтобы она поменяла фон</h1>
        <p>После первого наведения картинка закешируется и моргание пропадёт. Чтобы увидеть его снова, обновите страницу.</p>
        <div class="block"></div>
    </body>
</html>

PRO subscription for working with the service

Promo
Checking 50 pages in the tool "Checking the page"
190 ₽
The subscription is activated for 10 days and is linked to a specific site.
PRO subscription
3,500 pages per week in tools "Checking the page" and "Full site scan".
1 580 ₽
The subscription is activated for 30 days and you can scan any site.
Wallet
Additional pages that are credited to a separate balance. For checking large sites.
200 ₽
Number of pages
An active PRO subscription is required.
We use cookies. By continuing to use the site, you agree to the processing of personal data in accordance with privacy policy. I agree