1. Gyanastra
  2. Web Programming
  3. HTML, CSS, JavaScript, XML, Applets and Servlets
  4. CSS and DHTML
Free lesson, no account needed 11 min read 24 practice questions after it
Asked in past papers1 time
  • ISRO2015

Web Programming · HTML, CSS, JavaScript, XML, Applets and Servlets

CSS and DHTML

Overview

CSS (Cascading Style Sheets) is the W3C language used to describe the presentation of HTML or XML documents: colours, fonts, spacing, layout, borders, positioning and responsive behaviour.
DHTML (Dynamic HTML) is not a separate W3C language or standard; it is an umbrella term for using HTML + CSS + JavaScript + DOM together to make a web page change after it has loaded.
This lesson sits in Web Programming Core and assumes basic HTML structure, element/attribute syntax, and elementary JavaScript objects.
The exam tests precise definitions, selector behaviour, cascade and specificity, the box model, positioning, DOM methods, event propagation, and the difference between CSS presentation and DHTML behaviour.

Key concepts

  • CSS rule syntax: selector { property: value; ... }. A declaration is one property: value pair. CSS property names are case-insensitive; selector and class/id names are case-sensitive in HTML-oriented practice.
  • Selector types:
    • * universal; p type/element; .lead class; #intro id.
    • A B descendant; A > B direct child; A + B adjacent sibling; A ~ B general sibling.
    • [attr="value"] attribute selector.
    • :hover, :focus, :nth-child(an+b), :nth-of-type(an+b) pseudo-classes; ::before, ::after pseudo-elements.
  • Child vs descendant: div > p selects only <p> elements whose immediate parent is <div>; div p selects all <p> descendants at any depth.
  • Cascade: when several rules apply, the winner is decided by origin/importance, then specificity, then source order.
    For normal author styles, the practical order is: inline style > id selector > class/attribute/pseudo-class > element/pseudo-element; equal specificity means the later rule wins.
  • Specificity: convention used here: author selector score is (IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). Inline style is treated as a separate highest level when !important is ignored. !important overrides normal author declarations.
  • Inheritance: some properties such as color, font-family, font-size, line-height, and visibility inherit; many layout properties such as margin, padding, border, width, and display do not inherit by default.
  • CSS Box Model: from innermost to outermost: content → padding → border → margin. padding is space inside the border; margin is space outside the border.
  • box-sizing: content-box is the default: declared width/height describes the content area only. box-sizing: border-box: declared width/height includes content + padding + border.
  • display: none generates no box, removes the element and descendants from layout, and occupies no space. visibility: hidden hides the element but keeps its box and space; descendants may be made visible again.
    opacity: 0 makes it transparent but it still occupies space and may receive pointer events unless disabled.
  • Positioning: static is default normal flow. relative keeps normal space but is offset from its normal position. absolute is removed from normal flow and positioned relative to the nearest positioned ancestor (position not static); if none exists, the initial containing block applies. fixed is positioned relative to the viewport. sticky behaves relative until a scroll threshold. z-index controls stacking order in the same stacking context and normally applies to positioned elements; higher values are painted in front.
  • Float: float: left|right takes an element partly out of normal flow and shifts it to the side; inline content flows around it.
    A parent may not enclose floated children unless cleared or given a formatting context; clear prevents an element from sitting beside a float.
  • CSS units: px is a CSS pixel. em is relative to the element’s computed font size (for the font-size property itself, relative to the parent). rem is relative to the root element’s font size. vh is 1/100 of viewport height; vw is 1/100 of viewport width. % usually depends on the containing block property.
  • calc() performs CSS arithmetic, allowing mixed compatible units, e.g. width: calc(100% - 20px).
  • DHTML definition: Dynamic HTML is a combination of HTML/XHTML for structure, CSS for presentation, JavaScript for behaviour, and the DOM (Document Object Model) as the interface connecting script to document.
    It is not a W3C language and has no separate DTD of its own.
  • DOM basics: document.getElementById(id) returns the first element with that id; querySelector(cssSelector) returns the first match; querySelectorAll(cssSelector) returns a static list of matches. createElement, createTextNode, appendChild, firstChild, and innerHTML manipulate the node tree. firstChild includes text nodes and whitespace; firstElementChild considers only element nodes.
  • Events: common events include onclick, onload, onmouseover, onsubmit, onfocus, and onblur. onblur fires when an element loses focus. addEventListener(type, handler, useCapture) attaches a handler without overwriting earlier handlers for the same event; the onclick property holds only one handler at a time.
  • Event propagation (W3C DOM): capture phase from window down to target, target phase, then bubble phase from target back up.
    In addEventListener, true means capture; false or omitted means bubble. event.stopPropagation() halts further propagation; event.preventDefault() stops default browser action.
  • Legacy event capture: old Netscape JavaScript used captureEvents(eventType) on a window/document object to intercept matching events before they reach their target; the event type is supplied as an argument. Modern code uses addEventListener with true.
  • Table content alignment: horizontal alignment of inline content in table cells is done with text-align; vertical alignment in table cells is done with vertical-align, e.g. top, middle, bottom. HTML align/valign are presentational and obsolete in HTML5.
  • HTML is a markup language, not a programming language. It describes structure; CSS describes presentation; JavaScript supplies logic and dynamic behaviour.

How it works

  1. The browser parses HTML into a DOM tree, with element nodes, text nodes, and attributes.
  2. It parses CSS from browser defaults, external stylesheets, internal <style> blocks, and inline style attributes.
  3. For each element and property, the browser performs the cascade: collect declarations, handle !important, compare specificity, and use source order for ties.
  4. Inherited properties are passed from parents where applicable; non-inherited properties take their initial or declared value.
  5. The browser computes the box model for each element: content size, padding, border, and margin.
  6. Layout places normal-flow boxes; floats, positioning, and flex/grid rules then adjust placement.
  7. DHTML JavaScript responds to events, reads or changes the DOM, and may change inline styles or class names, causing the browser to recalculate layout and repaint.
Figure. Notice capture goes from window through document and ancestors to target; bubbling returns through ancestors, document, and window.

Formulas

  • Specificity convention, ignoring !important: Specificity=100a+10b+c\text{Specificity} = 100a + 10b + c

  • where aa = number of ID selectors, bb = number of class/attribute/pseudo-class selectors, cc = number of element/pseudo-element selectors. Inline style is treated separately as higher than any author selector.

  • Content-box dimensions: Wtotal=Wcontent+PL+PR+BL+BRW_{\text{total}} = W_{\text{content}} + P_L + P_R + B_L + B_R Htotal=Hcontent+PT+PB+BT+BBH_{\text{total}} = H_{\text{content}} + P_T + P_B + B_T + B_B

  • where PP is padding, BB is border, L/R = left/right, T/B = top/bottom.

  • Horizontal space occupied including margins: Woccupied=Wtotal+ML+MRW_{\text{occupied}} = W_{\text{total}} + M_L + M_R

  • where MM is margin.

  • Border-box content width: Wcontent=Wborder-box−PL−PR−BL−BRW_{\text{content}} = W_{\text{border-box}} - P_L - P_R - B_L - B_R

  • provided the result is non-negative.

  • Percentage child width: Wcontent child=p×Wcontent containing blockW_{\text{content child}} = p \times W_{\text{content containing block}}

  • where pp is the percentage expressed as a decimal.

  • Adjacent positive vertical margins collapse to: gap=max⁡(Mbottom,Mtop)\text{gap} = \max(M_{\text{bottom}}, M_{\text{top}})

Worked examples

Example 1 — specificity and cascade.
Markup: <p id="x" class="note" style="color: orange;">T</p> and rules p { color: red; }, .note { color: blue; }, #x { color: green; }.

  1. element rule has specificity (0,0,1).
  2. class rule has (0,1,0), higher.
  3. id rule has (1,0,0), higher than class.
  4. inline style="color: orange" is above author selectors under the convention used here.
    Answer: orange.

Example 2 — content-box numeric.
An element has width: 200px; padding: 10px 20px; border: 5px solid; margin: 15px; box-sizing: content-box.
Horizontal padding =20+20=40=20+20=40. Horizontal border =5+5=10=5+5=10.
Border-box width =200+40+10=250=200+40+10=250 px.
Occupied horizontal space =250+15+15=280=250+15+15=280 px.
Answer: border-box width 250 px; total horizontal space including margins 280 px.

Example 3 — border-box container and percentage child.
A container has width: 400px; box-sizing: border-box; padding: 20px; border: 10px. A child has width: 100%; box-sizing: content-box; padding: 10px; border: 5px. Ignore scrollbars.
Container content width =400−(20+20)−(10+10)=340=400-(20+20)-(10+10)=340 px.
Child percentage width is based on the containing block content width, so child content width =100%×340=340=100\%\times340=340 px.
Child border-box width =340+(10+10)+(5+5)=370=340+(10+10)+(5+5)=370 px.
It overflows the container content area by 370−340=30370-340=30 px.
Answer: child renders 370 px wide and overflows by 30 px.

Example 4 — margin collapse.
Two block siblings have no border or padding. First margin-bottom: 30px; second margin-top: 50px.
Adjacent vertical margins collapse; the gap is max⁡(30,50)=50\max(30,50)=50 px, not 8080 px.
Answer: 50 px gap.

Example 5 — match-the-list style.
Match A display:none, B visibility:hidden, C position:fixed, D z-index:-1 with effects: (i) element removed from layout entirely; (ii) element invisible but space retained; (iii) positioned relative to viewport; (iv) placed behind higher stacking-order elements in the same context.
A→i, B→ii, C→iii, D→iv.
Answer: A-i, B-ii, C-iii, D-iv.

Comparison

ConceptBehaviour
display: noneNo box; removed from layout; descendants also not rendered unless special cases do not apply.
visibility: hiddenBox exists; occupies space; not painted; a descendant can override with visibility: visible.
opacity: 0Fully transparent but still occupies space and may receive events.
content-boxwidth = content only; padding and border add to rendered size.
border-boxwidth includes content + padding + border.
position: relativeKeeps normal space; offset from normal position.
position: absoluteRemoved from normal flow; relative to nearest positioned ancestor or initial containing block.
position: fixedRemoved from normal flow; relative to viewport.
float: leftShifted left; content wraps; parent height may collapse unless cleared.
:nth-child(2n)Selects elements that are even-numbered children among all siblings.
:nth-of-type(2n)Selects even-numbered elements among siblings of the same type.
onclick propertyOne handler; assigning again overwrites.
addEventListenerMultiple handlers allowed; third argument controls capture/bubble.

Revision summary

  • Box model order: content, padding, border, margin.
  • content-box: total width = width + left/right padding + left/right border.
  • border-box: content width = declared width − padding − border.
  • Specificity order: inline > id > class/attribute/pseudo-class > element/pseudo-element; later wins on ties.
  • div > p is direct child; div p is any descendant.
  • display:none removes space; visibility:hidden keeps space.
  • absolute uses nearest positioned ancestor; fixed uses viewport.
  • Adjacent positive vertical margins collapse to the larger value.
  • addEventListener adds handlers; onclick = overwrites.
  • DOM event flow: capture → target → bubble.
  • Legacy Netscape method: captureEvents(eventType).
  • DHTML = HTML + CSS + JavaScript + DOM; it is not a separate W3C language.

Previous-year exam insights

  • Direct concept questions ask for the box-model order, the selector for direct children, and the exact difference between display:none and visibility:hidden.
  • Statement-based questions test whether DHTML is a W3C language, whether HTML is a programming language, and whether CSS/JavaScript/DOM together constitute DHTML.
  • Match-the-list questions commonly pair display:none, visibility:hidden, position:fixed, z-index, em, rem, vh, and calc() with their effects or meanings.
  • Numeric box-model questions require careful separation of content width, border-box width, and total occupied space including margins; do not forget that box-sizing changes the meaning of declared width.
  • Cascade and specificity questions often place an inline style against ids, classes, and element rules; inline normally wins unless !important changes the result.
  • Event questions test addEventListener vs onclick, onblur, capture/target/bubble order, and the legacy captureEvents(eventType) syntax.
  • Table-alignment questions expect CSS concepts: text-align for horizontal and vertical-align for vertical alignment of table-cell content; old HTML align/valign are obsolete presentational attributes.
  • Common traps: treating visibility:hidden as removed from layout, treating absolute as always relative to the viewport, using :nth-child as if it were :nth-of-type, and adding margins to a border-box width incorrectly.

Practice at PARIKSHA

You are ready for the PARIKSHA tests of this sub-topic when you can:

  1. List the four box-model areas from innermost to outermost and compute content-box/border-box widths.
  2. Write selectors for descendant, direct child, adjacent sibling, class, id, and attribute cases.
  3. Resolve a cascade conflict using specificity, source order, inline style, and !important.
  4. Distinguish display:none, visibility:hidden, and opacity:0.
  5. Explain static, relative, absolute, fixed, float, and z-index with one use case each.
  6. Match CSS units em, rem, vh, vw, and calc() to their meanings.
  7. Explain DHTML’s components and why DHTML is not a W3C specification.
  8. Trace W3C event propagation and choose capture or bubble in addEventListener.
  9. Use DOM methods such as getElementById, querySelector, createElement, appendChild, innerHTML, and getComputedStyle.
  10. Solve table-alignment and margin-collapse questions without confusing layout space with visible space.

Attempt the sub-topic test first, then the subject test.

Pages 1–2 of 2