Knowledge / Computer Science and Engineering / Web fundamentals: HTML, CSS and JavaScript

Web fundamentals: HTML, CSS and JavaScript

Computer Science and Engineering (B.E./B.Tech) · Web, cloud and design · free study notes for exams, placements and interviews

In short

HTML marks up content and structure, CSS controls presentation and layout, and JavaScript adds behaviour by scripting the Document Object Model (DOM) in the browser. Together they are the three languages every browser runs natively (WebAssembly is the only other, and it is a compiled target rather than an authoring language), so every website, dashboard, admin panel and hybrid mobile app rests on them. Indian university syllabi examine them in "Web Technologies"/"Internet and Web Programming" papers and labs (AICTE model curriculum, CBCS), and GATE CS does not test them directly, though UGC-NET Computer Science and most PSU/IT written tests do. In placements at TCS, Infosys, Wipro, Accenture, Cognizant, Zoho, Freshworks and product startups, the front-end round almost always opens with the box model, flexbox, specificity, closures, this, hoisting, promises and the event loop.

Explained

HTML: structure and semantics

CSS: selectors, cascade and the box model

JavaScript: language core

DOM, events and the browser

Asynchronous JavaScript and the event loop

Standards, tooling and workflow

Key points

Formulas and examples

Specificity = (a, b, c, d) — a = 1 if the declaration is in a style attribute; b = number of id selectors; c = number of class, attribute and pseudo-class selectors; d = number of element and pseudo-element selectors. Compare a, then b, then c, then d (no carrying between columns). Unitless count, not a single score.
Rendered width (box-sizing: content-box) = width + 2 × padding + 2 × border; horizontal space occupied = that + 2 × margin. Units: px, em, rem or %.
Rendered width (box-sizing: border-box) = width (padding and border inside it); content width = width − 2 × padding − 2 × border.
Flex item final size = flex-basis + free space × (own flex-grow ÷ Σ flex-grow) when free space is positive; when negative, shrink is distributed in proportion to (flex-shrink × flex-basis).
Grid free space for fr = container inline size − fixed track sizes − (number of gaps) × gap; each 1fr track = free space ÷ Σ fr.

Example 1 — Specificity contest. Which colour wins for <p id="intro" class="lead"> inside <div id="main">?

p                        { color: black; }   /* (0,0,0,1) */
.lead                    { color: blue;  }   /* (0,0,1,0) */
#main p                  { color: green; }   /* (0,1,0,1) */
#intro                   { color: red;   }   /* (0,1,0,0) */
div#main p.lead          { color: teal;  }   /* (0,1,1,2) */

Example 2 — Box model arithmetic. A card has width: 300px; padding: 20px; border: 5px; margin: 10px.

Example 3 — Flex and grid sizing. A flex container is 600 px wide with gap: 0 and three items of flex-basis: 100px; grow factors are 1, 2 and 3.

Example 4 — Event loop order. Predict the output:

console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
(async () => { console.log('4'); await null; console.log('5'); })();
console.log('6');

**Example 5 — Closures and var in a loop.** A GST-rate button list for an Indian billing page:

const rates = [5, 18, 40];                 // GST slabs since 22 Sep 2025
for (var i = 0; i < rates.length; i++) {
  setTimeout(() => console.log(rates[i]), 0);   // undefined ×3
}
for (let j = 0; j < rates.length; j++) {
  setTimeout(() => console.log(rates[j]), 0);   // 5, 18, 40
}
function makeGst(rate) {                   // closure over `rate`
  return amount => +(amount * rate / 100).toFixed(2);
}
const gst18 = makeGst(18);
console.log(gst18(1500));                  // 270
ConcernHTMLCSSJavaScript
RoleStructure and meaningPresentation and layoutBehaviour and logic
NatureMarkup, declarativeDeclarative rule setImperative programming language
Failure modeUnknown tags ignored, page still rendersInvalid declaration skipped, rest appliesUncaught error can stop the whole script
Error styleForgiving, auto-corrected by parserForgivingThrows at runtime
StandardHTML5 (WHATWG living standard)CSS3 modules (W3C)ECMAScript (TC39, annual edition)
localStoragesessionStorageCookie
LifetimeUntil explicitly clearedUntil the tab closesUntil its expiry date
Size per originAbout 5 MBAbout 5 MBAbout 4 KB
Sent to serverNoNoYes, with every matching request
Typical useTheme, draft form dataOne-visit wizard stateSession id, auth token

Where it is used

Interview questions

Common mistakes

Related topics

Front-end frameworks: React basics · REST APIs · Authentication and authorisation: sessions, OAuth and JWT · Back-end development with Node.js · Caching, queues and CDNs · System design basics for freshers

Open in the Knowledge reader →Find jobs →