HTML and HTML5
Asked in interviews
AskedWhat is the difference between HTML and HTML5?display property decides block or inline.<script> stops the parser, and CSS holds back the first paint.<button> comes with a role, a name and keyboard support. A <div> has none.<!DOCTYPE html> only keeps the browser out of quirks mode.Asked in interviews
AskedWhat is the difference between HTML and HTML5?<!DOCTYPE html><head><body>Both sit inside one <html> root element<meta charset="UTF-8"> Put it in the first 1024 bytesTrapNo doctype means quirks mode, and layout differs by browser.
<meta name="viewport" content="width=device-width, initial-scale=1"> TrapWithout it, a phone lays the page out in a wide viewport, often 980px, and shrinks it. Never set user-scalable=.
Asked in interviews
AskedExplain the viewport meta tag.<br><img><input>No content and no closing tag<div><span>display: blockCSS changes how it renders, not what it may containTrap<div /> doesn't close the div. HTML ignores the slash.
id="header"class="card featured"data-user-id="123" el.dataset.userId contenteditable="true" contenteditable="plaintext-only" TipStyle with classes. An ID selector beats any number of class selectors, so rules on ids are hard to override.
<strong><b><>&<ul><ol><dl>If reordering changes the meaning, use <ol>TrapScreen readers don't announce <strong> by default. For bold looks alone, use CSS font-weight.
<header>TipSemantic elements give screen readers landmarks to jump to, and tell search engines what each part is.
<main id="main">One visible per page, without the header, nav or footer<aside><article><section><h1><h2><h3>Never jump from h2 to h4TrapPick a heading level for structure, never for its size.
<figure><figcaption>Keep the image's alt as well<time datetime="2024-03-15"> target="_blank" rel="noopener"Tipnoreferrer also hides the Referer header. Modern browsers add noopener to _blank links on their own.
Asked in interviews
AskedHow do you create a hyperlink that opens in a new tab?<form method="get"><form method="post"><label for="email">for matches the input's id<fieldset><legend>TrapPOST isn't encrypted. Only HTTPS protects the data.
type="email"type="date"type="number"<input list="browsers"> <datalist id="browsers">autocomplete="email" autocomplete="new-password" requiredpattern="[A-Z]{3}" minlength="8"<form novalidate>formaction="/save-draft" formnovalidateTrapUsers can skip client-side checks, so validate on the server too.
alt="Dog catching a ball"In a link or button, say what it doesalt=""<track kind="subtitles" src="en.vtt" srclang="en">kind="captions"For viewers who can't hear the audioTrapDon't write "Image of" or the file name. The old longdesc attribute is obsolete.
srcset="s.jpg 480w, l.jpg 1200w"sizes="50vw"<source media="..."><source type="image/webp"> Inside <picture>, with the required <img> lastloading="lazy"TrapLazy-loading the hero image delays Largest Contentful Paint.
<video controls><source src="v.mp4" type="video/mp4"> <source src="v.webm" type="video/webm"> autoplay mutedBrowsers block autoplay with soundpreload="metadata" Only a hint. With autoplay, it downloads anyway.canvas.getContext("2d") Games, image editing, thousands of points<svg viewBox="0 0 9 9">Icons, logos, diagramsTrapA canvas forgets each shape once it's drawn, and screen readers can't see it. Put fallback content inside.
<button>Focusable, works with the keyboard, announced as a buttonaria-label="Close" aria-live="polite" <html lang="en">Set it again on any part in another languageTrapARIA adds no behavior. A div with role="button" still needs tabindex and key handlers.
tabindex="0"<a href="#main">Skip to main content</a>TrapAvoid positive tabindex. It overrides the source order.
dialog.showModal() Focus moves in, and the rest of the page goes inerttabindex="-1"heading.focus()popovertarget="menu" <div id="menu" popover>Esc or a click outside closes itTrapA z-index can't escape its stacking context. Dialogs and popovers sit in the top layer instead.
<caption><th scope="col"><th scope="row"><td headers="q1 north"><div role="feed" aria-busy="true"> aria-posinset="3" aria-setsize="50" A plain script stops parsing while it downloads and runs. async runs the moment it arrives. defer waits for parsing to end.
<script src="app.js" defer>Modules defer by default<script async src="ga.js"></body>The old fix. defer in the head starts the download sooner.<link rel="stylesheet"> Trapasync runs scripts in finish order, not page order.
<style>Inline its CSS, under about 14 KB compressed. Inline CSS can't be cached.<script async blocking="render"> rel="dns-prefetch" rel="preconnect" <link rel="preload" href="font.woff2" as="font" crossorigin>rel="prefetch"TrapA font preload needs crossorigin, even on the same origin. Without it, the preload doesn't match the real request.
Asked in interviews
AskedWhat are resource hints in HTML?fetchpriority="high" On the hero <img>. Keep it in the HTML, not in CSS.width="800" height="600"TipINP replaced First Input Delay in March 2024.
Asked in interviews
AskedHow do you optimize HTML for Core Web Vitals?Cache-Control: no-cachemax-age=31536000, immutableLike app.3f9a2c.js. A new version gets a new name.http-equiv="Cache-Control" Not a standard value. Send real HTTP headers.Trapno-cache still stores the response, but checks with the server before reuse. no-store never stores it.
<title><meta name="description"> About 50 to 60 characters, then 150 to 160<meta name="robots" content="noindex"> rel="canonical"<meta name="keywords"> Google ignores itTrapIf robots.txt blocks a URL, crawlers never see its noindex.
property="og:title" property="og:image" og:type and og:url are required too<script type="application/ld+json"> <body data-variant="b"> Choose it on the server, so the page doesn't flickerTrapShowing Googlebot a different page is cloaking.
localStorage.setItem(k, v)Strings only. Use JSON.stringify for objects.sessionStorage.setItem(k, v)Cleared when the tab closesnavigator.geolocation.getCurrentPosition(onPos) Needs HTTPS and the user's permissiondraggable="true" e.preventDefault() In the target's dragover, or drop never firesTrapAny script can read both stores. Never keep session tokens there.
new Worker("w.js")w.postMessage(data) The worker has no DOM accessnew IntersectionObserver(cb) observer.observe(el) navigator.serviceWorker.register("/sw.js") Needs HTTPS. Serve it from the root to cover every page.<link rel="manifest" href="/manifest.json"> Lists name, icons, start_url, displaycustomElements.define("x-card", XCard)The name must contain a hyphenconnectedCallback() disconnectedCallback() attributeChangedCallback() Only for names in observedAttributes <button is="fancy-button"> Safari doesn't support it<link rel="import" href="card.html"> Chrome only, dropped in version 80. Ship an ES module now.this.attachShadow({ mode: "open" })Its styles and ids don't leak<template><slot name="header"><h1 slot="header"><template shadowrootmode="open"> For server-side renderingTrapDeclarative Shadow DOM works only at parse time. Setting innerHTML won't create the shadow root.
script-src 'self' https://cdn.example.com <script nonce="r4nd0m">A new random nonce on every responseTrapA <meta> policy can't set frame-ancestors or report-only mode. Send the header.
Asked in interviews
AskedExplain Content Security Policy (CSP).integrity="sha384-<hash>" crossorigin="anonymous" crossorigin="anonymous" crossorigin="use-credentials" <iframe src="widget.html" sandbox>TrapNever give a same-origin frame allow-scripts with allow-same-origin. Its script can remove the sandbox.
Nothing matches. Try a shorter word, or clear the search.
With Pro, you can:
Already have Pro? Sign in