Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical HTML and CSS

You're reading from   Practical HTML and CSS Elevate your internet presence by creating modern and high-performance websites for the web

Arrow left icon
Product type Paperback
Published in Nov 2024
Publisher Packt
ISBN-13 9781835080917
Length 492 pages
Edition 2nd Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Brett Jephson Brett Jephson
Author Profile Icon Brett Jephson
Brett Jephson
Lewis Coulson Lewis Coulson
Author Profile Icon Lewis Coulson
Lewis Coulson
Ana Carolina Silveira Ana Carolina Silveira
Author Profile Icon Ana Carolina Silveira
Ana Carolina Silveira
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Part 1: Introducing HTML and CSS
2. Chapter 1: Introduction to HTML and CSS FREE CHAPTER 3. Chapter 2: Structure and Layout 4. Chapter 3: Text and Typography Styling 5. Part 2: Understanding Website Fundamentals
6. Chapter 4: Creating and Styling Forms 7. Chapter 5: Adding Animation to Web Pages 8. Chapter 6: Themes, Color, and Polishing Techniques 9. Part 3: Building for All
10. Chapter 7: Using CSS and HTML to Boost Performance 11. Chapter 8: Responsive Web Design and Media Queries 12. Chapter 9: Ensuring Accessibility in HTML and CSS 13. Part 4: Advanced Concepts
14. Chapter 10: SEO Essentials for Web Developers 15. Chapter 11: Preprocessors and Tooling for Efficient Development 16. Chapter 12: Strategies for Maintaining CSS Code 17. Chapter 13: The Future of HTML and CSS – Advancements and Trends 18. Index 19. Other Books You May Enjoy

Minimizing render-blocking resources

Render-blocking resources are CSS and JavaScript files that prevent the browser from rendering the page until they are fully loaded and executed. This delay is particularly noticeable for users on slower connections or less powerful devices.

Cumulative delays caused by multiple render-blocking resources can significantly extend the time it takes for users to interact with and perceive content on the page. This delay directly impacts SEO metrics such as FCP and can lead to a lower search engine ranking.

Understanding and minimizing render-blocking resources is crucial for optimizing web page performance, particularly in terms of improving loading times and user experience.

Before we go any further, let’s look at the types of render-blocking resources:

  • CSS: External stylesheets (linked via the <link> tag) and inline <style> tags can block rendering because the browser needs CSS to properly style the page
  • JavaScript: Scripts (linked via the <script> tag) that are not marked as asynchronous (async) or deferred (defer) can block rendering because the browser must execute them before continuing to build the DOM

Since these resources can negatively impact SEO, developers use various techniques to minimize their effects and prevent or reduce render-blocking. Let’s explore these methods in detail.

How to minimize render-blocking resources with CSS

To minimize render-blocking resources, one effective technique is to optimize CSS delivery. This involves two key strategies: identifying and inlining critical CSS and deferring non-critical CSS.

Critical CSS refers to the minimal CSS required to render the above-the-fold content, which is the portion of the webpage visible without scrolling. By inlining this essential CSS, you ensure that the necessary styling is applied quickly, enhancing the initial loading performance.

We can inline the minimal CSS required for the above-the-fold content directly into the HTML. This can be done within the <head> section of your HTML document:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
        name="viewport"
        content="width=device-width,
                initial-scale=1.0"
    >
    <title>Optimized Page</title>
    <style>
        /* Critical CSS */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }
        header {
            background-color: #333;
            color: #fff;
            padding: 1em;
            text-align: center;
        }
        /* Add more critical styles as needed */
    </style>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    <main>
        <!-- Main content -->
    </main>
</body>
</html>

For non-critical CSS, it is recommended to load these styles asynchronously after the initial page content has been rendered. This can be achieved by using JavaScript to dynamically load stylesheets or by employing the <link rel="preload"> tag with the as="style" attribute.

These methods help ensure that the primary content loads faster, improving the overall user experience. Include the following in the <head> section of your HTML document to preload the non-critical CSS file and apply it once the page has loaded:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
        name="viewport"
        content="width=device-width,
                initial-scale=1.0"
    >
    <title>Optimized Page</title>
    <style>
        /* Critical CSS */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }
        header {
            background-color: #333;
            color: #fff;
            padding: 1em;
            text-align: center;
        }
    </style>
    <link
        rel="preload"
        href="styles/non-critical.css"
        as="style"
        onload="this.rel='stylesheet'"
    >
    <noscript>
        <link
            rel="stylesheet"
            href="styles/non-critical.css"
        >
    </noscript>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    <main>
        <!-- Main content -->
    </main>
</body>
</html>

The <noscript> element is used to provide fallback content for users who have JavaScript disabled in their browsers. In the context of deferring non-critical CSS, the <noscript> block ensures that the non-critical CSS is still applied even if JavaScript is not available.

How to minimize render-blocking resources with JavaScript

To optimize JavaScript execution and improve webpage performance, several techniques can be employed:

  • First, the async attribute should be used for external JavaScript files that are not essential for the initial rendering of the page. This allows the script to download asynchronously and execute as soon as it’s ready, without blocking other resources. Use the async attribute for scripts that can run independently of other scripts and don’t depend on the DOM being fully loaded:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta
            name="viewport"
            content="width=device-width,
                    initial-scale=1.0"
        >
        <title>Optimized Page</title>
    </head>
    <body>
        <!-- Page content -->
        <script
            src="path/to/non-essential-script.js"
            async
        ></script>
    </body>
    </html>
  • Second, the defer attribute should be used for scripts that need to be executed in order but don’t need to block the DOM construction; scripts with defer are executed after the HTML has been fully parsed. We can use the defer attribute for scripts that need to be executed in order and after the HTML has been parsed:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta
            name="viewport"
            content="width=device-width,
                    initial-scale=1.0"
        >
        <title>Optimized Page</title>
        <script src="path/to/script1.js" defer></script>
        <script src="path/to/script2.js" defer></script>
    </head>
    <body>
        <!-- Page content -->
    </body>
    </html>
  • Additionally, loading JavaScript conditionally can further enhance performance. This includes lazy loading, which delays the loading of non-critical JavaScript until it is needed, such as after a user action or when it becomes visible. We can lazy load non-critical JavaScript to improve initial load performance:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta
            name="viewport"
            content="width=device-width,
                    initial-scale=1.0"
        >
        <title>Optimized Page</title>
    </head>
    <body>
        <!-- Page content -->
        <script>
            document.addEventListener(
                'DOMContentLoaded',
                function() {
                    var lazyScript =
                        document.createElement('script');
                    lazyScript.src =
                        'path/to/non-critical-script.js';
                    document.body.appendChild(lazyScript);
                }
            );
        </script>
    </body>
    </html>
  • On-demand loading involves using dynamic script loading techniques, such as document.createElement('script'), to load scripts only when they are necessary. We should load scripts only when they are necessary using dynamic script creation:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta
            name="viewport"
            content="width=device-width,
                    initial-scale=1.0"
        >
        <title>Optimized Page</title>
    </head>
    <body>
        <!-- Page content -->
        <button id="loadScriptButton">Load Script</button>
        <script>
            document.getElementById(
                'loadScriptButton'
            ).addEventListener(
                'click',
                function() {
                    var script =
                        document.createElement('script');
                    script.src =
                        'path/to/conditional-script.js';
                    document.body.appendChild(script);
                }
            );
        </script>
    </body>
    </html>

By using these techniques, you can significantly minimize render-blocking resources of your webpage by ensuring that JavaScript is loaded and executed in the most efficient manner.

By understanding how render-blocking resources affect page load times and employing techniques to minimize their impact, you can significantly improve the perceived speed and user experience of your website.

Prioritize loading critical resources first, defer or asynchronously load non-critical resources, and regularly monitor and optimize your site’s performance to ensure fast and efficient rendering. These practices not only enhance user satisfaction but also contribute positively to SEO and conversion rates.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image