Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 8

Diagnosing Layout and Accessibility Issues with Developer Tools

Recommended resources

Welcome to the final lesson of our first module! Over the past several lessons, you've built an impressive foundation, moving from basic project setup to creating a semantic, responsive storefront using modern CSS. In our last session, we focused on using mobile-first media queries to ensure your layout looks great on any device.

As you've seen, building layouts involves many interacting rules. Sometimes, they don't behave as expected. A stray margin, an incorrect Flexbox property, or a media query that doesn't trigger correctly can leave you scratching your head. This is a normal part of web development. The mark of an effective developer isn't just writing code, but also efficiently diagnosing and fixing it.

Today, we'll tackle this crucial skill. Our learning outcome is to use browser developer tools to diagnose and correct a layout or accessibility issue. We'll explore the powerful "DevTools" built into your browser, turning them from a mystery into your most trusted debugging partner. We'll intentionally introduce and then squash bugs in both the layout and accessibility of our storefront, giving you a practical, repeatable workflow for solving front-end problems.

Your Essential Debugging Toolkit: The Browser DevTools

Every modern browser comes with a suite of developer tools. Since you're using VS Code, you're likely using a browser like Chrome or Firefox, both of which have excellent DevTools. You can open them by right-clicking anywhere on your page and selecting "Inspect," or by pressing F12 (on Windows) or Cmd+Option+I (on Mac).

Go ahead and open catalog.html in your browser and bring up the DevTools. You'll see several panels, but for today, we'll focus on Elements, Layout, Lighthouse, and Accessibility.

To get a feel for the most fundamental panel, the Elements panel, let's start with a short reading.

View and change CSS | Chrome DevTools

This official guide from the Chrome team introduces the core workflow of inspecting an element and seeing its associated CSS.

Read the first section, View an element's CSS. This will walk you through selecting an element and finding the CSS rules that apply to it in the Styles pane. This is the first step in almost every debugging session.


Diagnosing and Fixing a Layout Bug

Now for some hands-on practice. Let's simulate a common layout problem. In our last lesson, we carefully styled the header to be responsive. On mobile, the navigation wraps to a new line, and on desktop, it sits neatly in a row with the logo and cart icon. Let's introduce a bug that breaks the vertical alignment on desktop screens.

  1. Introduce the Bug: In your styles.css file, find the .site-header rule (the one not inside a media query). Add a new property to it: align-items: flex-start;.

    /* This is your base mobile style */
    .site-header {
      display: flex;
      flex-wrap: wrap; 
      justify-content: space-between;
      align-items: center;
      padding: 1rem 2rem;
      background-color: #f8f9fa;
      border-bottom: 1px solid #dee2e6;
      gap: 1rem;
      align-items: flex-start; /* ADD THIS BUGGY LINE */
    }
    
  2. Observe the Bug: Save the file and refresh catalog.html in your browser. Make your browser window wide (over 768px). You should see that the logo, navigation links, and cart icon are all aligned to the top of the header, which looks awkward. They are no longer vertically centered.

  3. Debug and Fix: Now, let's use the DevTools to figure out why.

    • Right-click on the misaligned header and choose Inspect. The DevTools will open with the <header class="site-header"> element highlighted in the Elements panel.
    • Look at the Styles pane on the right. You'll see all the CSS rules applied to the .site-header. You should see the align-items: flex-start; property that you just added.
    • This is the key to debugging: The Styles pane shows you exactly what the browser is applying. To test if this property is the culprit, simply uncheck the checkbox next to it. The header should immediately snap back to the correct vertical alignment!
    • You've found the bug. You can now re-check the box and click on the value flex-start. Change it to center and press Enter. You've just live-edited the CSS to confirm the fix.
    • Finally, go back to your styles.css file in VS Code and change align-items: flex-start; to align-items: center; (or remove the line, since center was already there). Save the file, and your layout is permanently fixed.

This cycle—observe, inspect, test, and fix—is the fundamental workflow for debugging CSS.

Advanced Layout Debugging: Flexbox and Grid Inspectors

The Styles pane is great, but for complex layouts using Flexbox or Grid, DevTools gives us even more powerful visual tools.

Notice in the Elements panel that any element with display: flex or display: grid has a small badge next to it. Clicking this badge toggles a visual overlay on the page, showing you the outlines of the flex items or grid tracks.

For an even better experience, select a flex or grid container and go to the Layout pane in the DevTools sidebar. From here, you can enable overlays with more detail and color-coding.

Inspect and debug CSS flexbox layouts | Chrome DevTools

The Flexbox editor in Chrome DevTools allows you to interactively change properties like flex-direction and justify-content with the click of a button, providing instant visual feedback.

Let's read a quick guide on this feature.

Inspect and debug CSS flexbox layouts | Chrome DevTools

This guide focuses specifically on the tools for debugging Flexbox, which we use in our site header.

Read through the sections Discover CSS flexbox, Modify layouts, and Examine the layout. This will show you how to use the flex badge, the visual editor, and the layout overlay to understand how Flexbox is behaving.

Instructor Kevin Powell also provides an excellent video walkthrough of these debugging tools in action. He covers many of the techniques we've just discussed.

Debugging CSS, no extensions required - Using your devtools

This video by Kevin Powell is a practical tour of the most useful CSS debugging features in browser DevTools.

Watch the section where he discusses the Styles tab from the start. He explains how to spot overridden rules and typos. Then, jump to the section on Flexbox and Grid inspectors from this point to see a live demonstration of the visual debugging tools.


Diagnosing and Fixing an Accessibility Issue

A professional website must be usable by everyone, including people who rely on assistive technologies like screen readers. As a full-stack developer, ensuring your work is accessible is a core responsibility. DevTools provides powerful features to help you find and fix these issues.

Automated Audits with Lighthouse

A great starting point is an automated audit. DevTools includes a tool called Lighthouse that can analyze your page and report on performance, SEO, and accessibility.

  1. In DevTools, switch to the Lighthouse panel.
  2. In the "Categories" section, you can uncheck everything except Accessibility.
  3. Click the "Analyze page load" button.

Lighthouse will scan your catalog.html and generate a report. It looks for common, machine-detectable issues like missing image alt text, insufficient color contrast, or form inputs without labels. It's not a complete check, but it's excellent for catching common mistakes.

Manual Inspection with the Accessibility Tree

Automated tools can't tell you if the user experience makes sense. For that, we need to inspect the Accessibility Tree. This is a modified version of the DOM tree that assistive technologies use to understand the page structure and content.

Let's create and fix a common accessibility bug. Our product cards are clickable links. A screen reader user needs to know what each link is for. A link that just says "Link" is useless.

  1. Introduce the Bug: In catalog.html, find the <a> tag for your first product card. Add the attribute aria-label="More details" to it.

    <!-- Find your product card link -->
    <a href="#" class="product-card" aria-label="More details"> 
      <!-- ... rest of the card content ... -->
    </a>
    

    While aria-label is a valid attribute, "More details" is vague and unhelpful. If a user hears "More details, More details, More details," they have no idea which product is which. This is a common anti-pattern.

  2. Debug and Fix:

    • In DevTools, go to the Elements panel and click the "Switch to accessibility tree view" button at the top right (it looks like a small person).
    • Find the link for your product card in this new tree view. You will see that its accessible name is "More details". You have confirmed that this is what a screen reader will announce.
    • Now, let's fix it. Switch back to the DOM tree view by clicking the same button.
    • Find the <a> tag again. Double-click on the aria-label="More details" attribute and delete it.
    • Now, find the <h4> with the product name inside the card (e.g., <h4>Eco-Friendly Water Bottle</h4>). The browser is smart enough to use this visible text to give the link an accessible name.
    • Switch back to the accessibility tree view. You should now see that the link's name is "Eco-Friendly Water Bottle". This is much more descriptive and useful!

This video from the Chrome Developers team provides a fantastic, in-depth look at this entire workflow, from running Lighthouse to diving into the accessibility tree to fix a real-world bug.

Debugging accessibility with Chrome DevTools

This video provides a professional-level walkthrough of finding and fixing accessibility bugs using DevTools.

First, watch the introduction to understand the role of automated tools like Lighthouse. Next, watch the section from the demo start, where the presenter runs Lighthouse and then uses a screen reader to manually verify a bug. Finally, watch the explanation of the accessibility tree and see how it's used to pinpoint and fix the exact cause of the bug, which is very similar to the process we just followed.


Conclusion

Congratulations on completing the first module! You've built a solid, responsive, and accessible webpage, and today you've acquired the essential skill of debugging it like a pro. This ability to systematically find and fix problems will be invaluable throughout your career.

Here are the key takeaways from this lesson:

  • The Debugging Workflow: Observe a bug, inspect the element, use the Styles pane to test theories by toggling and editing CSS, and then apply the fix in your code.
  • Layout Tools: The Box Model diagram is your go-to for spacing issues. The Flexbox and Grid inspectors provide powerful visual overlays and interactive editors for modern layouts.
  • Accessibility Tools: Use Lighthouse for automated checks to catch common errors. Use the Accessibility Tree to understand how assistive technologies perceive your page and to debug issues with accessible names and roles.
  • Temporary Changes: Remember that any changes made in DevTools are temporary and will be lost on page reload. Always transfer your fixes back to your source files.

You now have a complete set of foundational skills for modern front-end development. Our storefront is looking great, but it's currently static. In our next lesson, we'll begin Module 2 and dive into JavaScript, the language that will allow us to add interactivity, manage data, and bring our e-commerce application to life. We'll start with the very basics: declaring values and understanding JavaScript's primitive types.