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

Avoiding excessive DOM size

The DOM represents the structure of HTML elements on a webpage and directly impacts how quickly the browser can render and interact with the page. DOM size refers to the total number of elements (HTML tags), including nodes and their relationships (parent-child or sibling), styles, and attributes, that make up the structure of a webpage.

Each element in the DOM requires memory and processing time for rendering and scripting operations. A large DOM size can lead to slower performance, increased memory usage, and potential rendering issues. This is why avoiding excessive DOM size is crucial for optimizing web page performance and ensuring a smooth user experience.

Techniques to Avoid Excessive DOM Size

To avoid excessive DOM size, several techniques can be employed:

  • Optimize your HTML structure by simplifying markup. Use semantic HTML and avoid unnecessary nesting of elements, keeping the structure as flat as possible. Remove redundant elements, attributes, and inline styles.
  • Limit dynamic content by implementing lazy loading, which loads content dynamically only when needed. Also, use techniques such as pagination or infinite scroll to progressively load content as the user scrolls, rather than all at once.
  • Efficient use of CSS and JavaScript is also crucial. Minimize CSS complexity by reducing the number of CSS rules and selectors, and avoid excessive styles that apply to many elements. Optimize JavaScript by refactoring code to minimize DOM manipulation operations and improve performance.
  • Additionally, optimize rendering performance by prioritizing the loading and rendering of critical resources (CSS and JavaScript) necessary for above-the-fold content. Use asynchronous loading (the async attribute) or defer execution (the defer attribute) for non-critical scripts to improve initial rendering speed.
  • Regularly monitor and audit DOM size using browser developer tools such as Chrome DevTools to inspect and analyze DOM size and structure, identifying and addressing elements or scripts that contribute excessively to DOM size and performance bottlenecks.
  • Finally, consider the impact of JavaScript frameworks and libraries (such as React, Angular, or Vue.js) on DOM size and performance, optimizing their usage and evaluating alternatives if necessary.

Here’s an example of a complex DOM structure:

<div class="container">
    <div class="header">
        <h1>Welcome to My Website</h1>
        <p class="intro">
            This is the best place to find great content.
        </p>
    </div>
    <div class="main-content">
        <div class="section">
            <h2>Section 1</h2>
            <div class="section-content">
                <p>This is some text for section 1.</p>
                <div class="extra-info">
                    <p>Additional info 1</p>
                </div>
            </div>
        </div>
        <div class="section">
            <h2>Section 2</h2>
            <div class="section-content">
                <p>This is some text for section 2.</p>
                <div class="extra-info">
                    <p>Additional info 2</p>
                </div>
            </div>
        </div>
    </div>
    <div class="footer">
        <p>Footer content here.</p>
    </div>
</div>

The following is the same DOM in a simplified version:

<div class="container">
    <header>
        <h1>Welcome to My Website</h1>
        <p>
            This is the best place to find great content.
        </p>
    </header>
    <main>
        <section>
            <h2>Section 1</h2>
            <p>This is some text for section 1.</p>
            <p>Additional info 1</p>
        </section>
        <section>
            <h2>Section 2</h2>
            <p>This is some text for section 2.</p>
            <p>Additional info 2</p>
        </section>
    </main>
    <footer>
        <p>Footer content here.</p>
    </footer>
</div>

In this code, we replaced the generic <div> elements with semantic elements. Also, we removed unnecessary <div> wrappers around text and sections. By simplifying the DOM structure in this way, the HTML becomes more readable, maintainable, and efficient.

For more insights into how DOM size affects user experience and strategies to manage it, check out Jeremy Wagner’s article on web.dev (https://web.dev/articles/dom-size-and-interactivity). Additionally, the Lighthouse documentation offers a comprehensive guide on reducing DOM size:

https://developer.chrome.com/docs/lighthouse/performance/dom-size

By prioritizing simplicity, efficient resource loading, and responsiveness, you can ensure a faster and smoother user experience across various devices and network conditions.

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