EthicsComputerCourses
Join training +91 90532 10052

Cross-Browser Compatibility

HTML · Lesson 75 of 234 Intermediate 8 min read Updated

By the end of this lesson you will be able to check whether a feature works in the browsers your visitors use, write HTML that still works when a feature is missing, and plan a simple test on real devices, including an iPhone.

Your page will be opened on a new laptop in Pune, an old Android phone in Rohtak and an iPhone in Kochi. All three people must be able to finish their task, even if the page does not look identical everywhere.

Evergreen browsers and the three engines

An evergreen browser updates itself automatically every few weeks. Chrome, Edge, Firefox, Safari, Samsung Internet and Opera all work this way.

Every browser is built on a rendering engine, the part that reads HTML and CSS and draws the page. Think of cars: many brands, but only a few companies make the engines. There are three main browser engines today.

EngineMade byBrowsers that use it
BlinkGoogle and othersChrome, Edge, Opera, Samsung Internet, Brave
GeckoMozillaFirefox
WebKitAppleSafari on Mac, iPhone and iPad

Testing in Chrome and Edge tests one engine twice; Chrome, Firefox and Safari cover all three.

One more rule: in India, every browser on an iPhone or iPad uses WebKit, because of Apple's rules for iOS. Chrome on an iPhone is Safari's engine in a Chrome-style window, so a Safari bug appears in every iPhone browser.

Checking support before you use a feature

Before you use a new feature, check where it works.

  • MDN Web Docs. Every element page ends with a browser compatibility table showing which version of each browser added support.
  • Baseline. A shared label from the major browser makers. A feature is Baseline newly available once it works in the current versions of Chrome, Edge, Firefox and Safari, on desktop and mobile. It becomes Baseline widely available 30 months later, when most people have updated. MDN shows the badge at the top of each page.
  • caniuse.com. It shows support tables plus the share of users on each browser, and it can show figures for one country, such as India.

A practical rule: use widely available features freely, and newly available ones with a fallback. Teams write this down as a target, as shown in Browser Compatibility.

HTML fails gently

When a browser meets a tag or attribute it does not know, it ignores the tag and shows the content inside. An unknown type on an <input> becomes a plain text box. So much of HTML degrades without breaking.

Several elements are built around this idea. Their fallback content, the content inside them, is shown only when the browser cannot use the element itself.

HTML
<video controls width="640" height="360">
  <source src="/media/sports-day.webm" type="video/webm">
  <source src="/media/sports-day.mp4" type="video/mp4">
  <p>Your browser cannot play this video. <a href="/media/sports-day.mp4">Download the sports day video (MP4)</a>.</p>
</video>

<picture>
  <source srcset="/img/campus.avif" type="image/avif">
  <source srcset="/img/campus.webp" type="image/webp">
  <img src="/img/campus.jpg" alt="Main building of the college" width="800" height="500">
</picture>

The browser uses the first <source> format it supports. The <img> inside <picture> is the fallback and carries alt, width and height, so it is never optional. See Responsive Images: Picture & Srcset.

A <canvas> works the same way. Put a text or table version of the drawing inside it. Browsers that cannot draw, and people who cannot see the drawing, still get the facts.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Canvas Fallback</title>
</head>
<body>
  <h1>Monthly sales, Meera's Tea Stall</h1>
  <canvas id="chart" width="300" height="150">
    <p>Sales: August ₹18,000, September ₹24,000, October ₹31,000.</p>
  </canvas>

  <script>
    const ctx = document.querySelector("#chart").getContext("2d");
    const sales = [18, 24, 31];
    sales.forEach((value, i) => {
      ctx.fillStyle = "#b45309";
      ctx.fillRect(30 + i * 90, 150 - value * 4, 60, value * 4);
    });
  </script>
</body>
</html>

You see bars because your browser supports canvas, so the paragraph inside is not shown. For full accessibility, a real table next to the chart is even better; see Canvas API Fundamentals.

Feature detection: ask, do not guess

Feature detection means testing whether a feature exists before using it. It is the opposite of browser sniffing, which reads the browser's name from the user agent string and guesses. Sniffing breaks every time a browser adds a feature or changes its name, so avoid it.

You can detect features in three places:

  • CSS: @supports (display: grid) { ... } applies rules only where the browser understands them. See @supports Feature Queries.
  • JavaScript properties: "showPopover" in HTMLElement.prototype is true only where the Popover API exists.
  • JavaScript CSS checks: CSS.supports("selector(:has(a))") asks the CSS engine directly.

Run this example to see a support report for your own browser.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Feature Report</title>
</head>
<body>
  <h1>What does this browser support?</h1>
  <ul id="report"></ul>
  <button id="help">Delivery help</button>
  <div id="tip" popover>Delivery in Lucknow takes 2 days.</div>

  <script>
    const checks = {
      "dialog element": typeof HTMLDialogElement === "function",
      "popover attribute": "showPopover" in HTMLElement.prototype,
      "inert attribute": "inert" in HTMLElement.prototype,
      "lazy loading images": "loading" in HTMLImageElement.prototype,
      "CSS :has()": CSS.supports("selector(:has(a))")
    };
    for (const [name, ok] of Object.entries(checks)) {
      const li = document.createElement("li");
      li.textContent = name + ": " + (ok ? "yes" : "no");
      document.querySelector("#report").append(li);
    }

    const tip = document.querySelector("#tip");
    document.querySelector("#help").addEventListener("click", () => {
      if (checks["popover attribute"]) {
        tip.togglePopover();
      } else {
        tip.removeAttribute("popover");
        tip.hidden = !tip.hidden;
      }
    });
  </script>
</body>
</html>

The button uses the Popover API where it exists and otherwise just shows the text. Both paths work.

Polyfills, used carefully

A polyfill is a script that adds a missing feature to an older browser, like a spare part that fits an older car model.

  • Load a polyfill only for browsers that need it, behind a feature check, so up-to-date browsers do not download extra code.
  • Host the file yourself, or use a trusted package with a fixed version. In 2024 the domain of a popular polyfill service, polyfill.io, changed owners and started sending harmful code to the many sites that linked to it.
  • Remove polyfills once the feature is widely available.

Testing on real browsers and devices

Tools help, but nothing replaces a real device. Plan a short test like this.

  1. Step 1: test in Chrome, Firefox and Safari to cover all three engines.
  2. Step 2: test on a real Android phone in Chrome, on mobile data, not office Wi-Fi.
  3. Step 3: test on a real iPhone. Safari cannot be installed on Windows, so borrow a phone from a friend or use a cloud device-testing service.
  4. Step 4: on each device, complete the main task: fill the form, open the menu, pay or submit.
  5. Step 5: note anything broken, check MDN or caniuse for that feature, and add a fallback.

Remote debugging of iPhone Safari needs Safari's Web Inspector on a Mac. Without one, test carefully by hand.

Common mistakes

  • Testing only in Chrome. You may be testing one engine through several browsers.
  • Browser sniffing. Detect the feature, not the browser name.
  • No <img> inside <picture>. Nothing shows, and there is no alt text.
  • Loading polyfills for everyone from an unknown CDN. It slows the page and risks your users' security.
  • Expecting pixel-perfect sameness. Aim for a page that works everywhere and looks good everywhere.

Interview questions

1. What are the main browser engines, and which browsers use them?

Blink powers Chrome, Edge, Opera and Samsung Internet. Gecko powers Firefox, and WebKit powers Safari. On iPhones in most countries, including India, every browser uses WebKit.

2. What does Baseline mean on MDN?

Baseline marks features that work across Chrome, Edge, Firefox and Safari on desktop and mobile. "Newly available" means it works in all of their current versions. "Widely available" means 30 months have passed since then, so most users have it.

3. Why is feature detection better than user agent sniffing?

It tests what the browser can actually do, so it keeps working when browsers update. User agent strings are unreliable and often copied by other browsers.

4. How does HTML provide fallbacks?

Unknown elements and attributes are ignored, and their content is shown. Elements such as <video>, <canvas> and <picture> show their inner fallback content, or the inner <img>, only when the browser cannot use them.

5. What are the risks of polyfills?

They add download size, and a polyfill loaded from a third-party server can be changed to serve harmful code. Load them conditionally, pin versions or self-host them, and remove them when no longer needed.

Key points

  • Evergreen browsers update themselves; three engines matter: Blink, Gecko and WebKit.
  • Every iPhone browser in India uses WebKit, so test real iOS Safari.
  • Check support on MDN, the Baseline badge and caniuse before using new features.
  • Use the fallback content of <video>, <picture> and <canvas>.
  • Detect features with @supports, in checks and CSS.supports(), never by browser name.
  • Load polyfills only when needed, from a trusted, pinned source.

Exercise

Rohan is building a "Share your score" page for a college quiz in Jaipur. Write a complete page with a <canvas> bar chart of three scores that has a text fallback inside it, and a button that uses a <dialog> to show the score details. Use feature detection: if HTMLDialogElement is missing, show the details in a normal paragraph instead.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Quiz Scores</title>
</head>
<body>
  <h1>Quiz scores</h1>
  <canvas id="bars" width="300" height="120">
    <p>Round 1: 6, Round 2: 8, Round 3: 9 (out of 10).</p>
  </canvas>
  <button id="show">Show details</button>
  <dialog id="details">
    <p>Total 23 out of 30. Well done, Rohan!</p>
    <form method="dialog"><button>Close</button></form>
  </dialog>
  <script>
    const ctx = document.querySelector("#bars").getContext("2d");
    [6, 8, 9].forEach((s, i) => ctx.fillRect(20 + i * 90, 120 - s * 12, 60, s * 12));

    const details = document.querySelector("#details");
    document.querySelector("#show").addEventListener("click", () => {
      if (typeof HTMLDialogElement === "function") {
        details.showModal();
      } else {
        details.setAttribute("open", "");
      }
    });
  </script>
</body>
</html>

The canvas carries a text fallback, and the button checks for HTMLDialogElement before calling showModal(). In a browser without dialog support, setting open simply leaves the content visible as normal text.

Quick Quiz

1. Which engine does Chrome use on an iPhone in India?

2. A feature is "Baseline widely available". What does that mean?

3. Which is an example of feature detection?

4. Inside <picture>, which element is the required fallback?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call