What is HTML?
HTML (HyperText Markup Language) describes the structure and meaning of a web page. The browser parses it into the DOM; CSS styles it and JavaScript adds behavior.
Follow-up: is HTML a programming language? No, it has no variables, conditions or loops.
<h1>Orders</h1> <p>You have <strong>3</strong> open orders. <a href="/orders">View them</a>.</p>What is the difference between a tag, an element and an attribute in HTML?
A tag is the markup in angle brackets (
<a>), an element is everything from the start tag to the end tag including the content, and an attribute is a name and value on the start tag (href="/docs"). After parsing, tags are gone: the DOM is a tree of elements.What is
<!DOCTYPE html>and why is it needed?<!DOCTYPE html>tells the browser to render in standards mode. Without it, browsers fall back to quirks mode, which imitates old browser bugs, so the same CSS can lay out differently. It is a declaration, not a tag; the doctype page shows the old HTML 4 forms.What is the basic structure of an HTML document?
A doctype, an
<html>root, a<head>with metadata that is not displayed, and a<body>with the visible content.HTML<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>My first page</title> </head> <body> <h1>Hello</h1> <p>This text is inside the body, so it is visible.</p> </body> </html>Mention
langon<html>,<meta charset="utf-8">early in the head, and the viewport tag, without which phones render the page at desktop width.What is the difference between
<head>and<header>?<head>holds metadata (title, meta tags, stylesheets, scripts), is never displayed, and appears once.<header>is visible introductory content for the page or a section, such as a logo, title and navigation, and a page can have several.What is the difference between block and inline elements?
A block element starts on a new line and takes the full width (
<div>,<p>); an inline element flows inside text and is as wide as its content (<span>,<a>).HTML<!DOCTYPE html> <style> div, p { background: #dbeafe; } span, a { background: #fde68a; } span { width: 300px; height: 80px; } /* ignored: span is inline */ </style> <div>A div takes the whole line.</div> <p>A paragraph with <span>a span</span> and <a href="#">a link</a> that stay in the text.</p>The trap:
width,heightand vertical margins do not apply to inline text elements such as<span>and<a>. The CSSdisplayproperty changes the type.What is the difference between
<div>and<span>?Both are generic containers with no meaning:
<div>is block-level and groups layout,<span>is inline and wraps text. Use them only when no semantic element such as<nav>or<button>fits, because those tell screen readers what the content is. More on the span page.What are void (empty) elements in HTML?
Void elements cannot have content, so they have no end tag:
<img>,<br>,<hr>,<input>,<meta>,<link>,<source>and a few others.Follow-up: what about
<br />? The slash is allowed and ignored. On other elements it means nothing:<div />opens a div, and everything after it ends up inside.What is the difference between
idandclass?An
idmust be unique and identifies one element; aclasscan be shared by many elements, and an element can have several.HTML<!DOCTYPE html> <style> .tag { background: #e0e7ff; padding: 2px 8px; border-radius: 4px; } #featured { outline: 2px solid #f59e0b; } </style> <p> <span class="tag">html</span> <span class="tag" id="featured">css</span> <span class="tag">javascript</span> </p> <a href="#featured">Jump to #featured</a>Ids are targets for fragment links,
<label for>andgetElementById. Use classes for styling, since id selectors have higher specificity. See class selectors.What are the new features in HTML5?
Semantic elements (
<header>,<nav>,<main>,<article>), native<audio>and<video>,<canvas>, new input types with built-in validation,data-*attributes, APIs such as Web Storage and Web Workers, and the short doctype.Follow-up: is there an HTML6? No. HTML is now a Living Standard, updated continuously.
What is the
altattribute on an image, and when should it be empty?altis the image's text alternative, read by screen readers, shown when the image fails to load, and used by search engines. Describe what the image shows or does in its context.<img src="chart.png" alt="Sales grew from 40 to 65 orders a day in March"> <a href="/"><img src="logo.svg" alt="Coddy home"></a> <img src="divider.png" alt="">Use
alt=""for decorative images. Leavingaltout is worse: some screen readers then read the file name.What types of lists are there in HTML?
Three:
<ul>for unordered items,<ol>for steps or rankings, and<dl>for term and description pairs.HTML<!DOCTYPE html> <ul> <li>Milk</li> <li>Bread</li> </ul> <ol start="3"> <li>Third step</li> <li>Fourth step</li> </ol> <dl> <dt>HTML</dt> <dd>Structure of the page</dd> <dt>CSS</dt> <dd>How it looks</dd> </dl>Only
<li>(plus<script>and<template>) may be a direct child of<ul>or<ol>, so a nested list goes inside an<li>. More on the lists page.How do you create a table in HTML, and what do
colspanandrowspando?A
<table>holds rows (<tr>) of header cells (<th>) and data cells (<td>), optionally grouped in<thead>and<tbody>.colspanstretches a cell across columns,rowspanacross rows.HTML<!DOCTYPE html> <style> table { border-collapse: collapse; } th, td { border: 1px solid #94a3b8; padding: 4px 10px; } </style> <table> <caption>Timetable</caption> <thead> <tr><th scope="col">Day</th><th scope="col">9:00</th><th scope="col">10:00</th></tr> </thead> <tbody> <tr><th scope="row">Mon</th><td colspan="2">Lab (two hours)</td></tr> <tr><th scope="row">Tue</th><td>Maths</td><td rowspan="2">Physics</td></tr> <tr><th scope="row">Wed</th><td>English</td></tr> </tbody> </table>Use tables for data, never for layout. See colspan and rowspan.
What are HTML entities, and when do you need them?
Entities are codes for characters that would be read as markup or are hard to type, like
<,&, or€.HTML<!DOCTYPE html> <p>To start a paragraph, write <p>.</p> <p>Tom & Jerry © 1940, price 10 €</p>On a UTF-8 page only
<,&and quotes inside attribute values need escaping. Escaping user input this way is the basis of XSS prevention.How do you open a link in a new tab, and why add
rel="noopener"?Add
target="_blank", plusrel="noopener"so the new page cannot reach yours throughwindow.opener.<a href="https://example.com" target="_blank" rel="noopener noreferrer">Example</a>Without it, the opened page could redirect your tab to a phishing page (reverse tabnabbing). Chrome 88, Firefox 79 and Safari 12.1 and later apply
noopenertotarget="_blank"by default (see MDN on noopener), but writing it is still expected. See opening links in a new tab.How do you write a comment in HTML, and are comments visible to users?
Write
<!-- text -->. Comments are not rendered but are sent with the page, so anyone can read them in View Source; never put keys or internal notes there. They cannot be nested: the first-->ends the comment.What are the ways to add CSS to an HTML page?
Three: an inline
styleattribute, an internal<style>element, and an external stylesheet linked with<link rel="stylesheet">.<head> <link rel="stylesheet" href="styles.css"> <!-- external --> <style>p { line-height: 1.6; }</style> <!-- internal --> </head> <body> <p style="color: crimson;">Inline</p> <!-- inline --> </body>Use external stylesheets on real sites, since the browser caches them. Inline styles beat stylesheet rules unless a rule uses
!important. See external CSS.Is HTML case-sensitive?
Tag and attribute names are not; the parser lowercases them. Many attribute values are:
id="Main"andid="main"are different ids, and class names are compared exactly (quirks mode makes CSS class and id matching case-insensitive). Write everything lowercase.What is the difference between HTML and XHTML?
XHTML is HTML in XML syntax, read by a parser that stops at the first error; the HTML parser repairs mistakes. Only a page served as
application/xhtml+xmlgets the XML parser, sotext/htmlis HTML however it is written.HTML XHTML Names Case-insensitive Lowercase only Void elements <br><br />End tags Some optional Always required What are boolean attributes, and what does
disabled="false"do?A boolean attribute is true when present and false when absent, whatever its value. So
disabled="false"still disables the element.<button disabled>Disabled</button> <button disabled="false">Still disabled</button> <button>Enabled</button>To turn one off, remove the attribute (
removeAttribute("disabled")) or set the propertybutton.disabled = false. Others:checked,required,readonly,multiple,open.
HTML Basics Interview Questions
Document structure, elements and attributes, lists, tables, links and entities, as asked in campus placements and first rounds.