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 oneproperty: valuepair. CSS property names are case-insensitive; selector and class/id names are case-sensitive in HTML-oriented practice. - Selector types:
*universal;ptype/element;.leadclass;#introid.A Bdescendant;A > Bdirect child;A + Badjacent sibling;A ~ Bgeneral sibling.[attr="value"]attribute selector.:hover,:focus,:nth-child(an+b),:nth-of-type(an+b)pseudo-classes;::before,::afterpseudo-elements.
- Child vs descendant:
div > pselects only<p>elements whose immediate parent is<div>;div pselects 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!importantis ignored.!importantoverrides normal author declarations. - Inheritance: some properties such as
color,font-family,font-size,line-height, andvisibilityinherit; many layout properties such asmargin,padding,border,width, anddisplaydo not inherit by default. - CSS Box Model: from innermost to outermost: content → padding → border → margin.
paddingis space inside the border;marginis space outside the border. box-sizing: content-boxis the default: declaredwidth/heightdescribes the content area only.box-sizing: border-box: declared width/height includes content + padding + border.display: nonegenerates no box, removes the element and descendants from layout, and occupies no space.visibility: hiddenhides the element but keeps its box and space; descendants may be made visible again.
opacity: 0makes it transparent but it still occupies space and may receive pointer events unless disabled.- Positioning:
staticis default normal flow.relativekeeps normal space but is offset from its normal position.absoluteis removed from normal flow and positioned relative to the nearest positioned ancestor (position notstatic); if none exists, the initial containing block applies.fixedis positioned relative to the viewport.stickybehaves relative until a scroll threshold.z-indexcontrols stacking order in the same stacking context and normally applies to positioned elements; higher values are painted in front. - Float:
float: left|righttakes 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;clearprevents an element from sitting beside a float. - CSS units:
pxis a CSS pixel.emis relative to the element’s computed font size (for thefont-sizeproperty itself, relative to the parent).remis relative to the root element’s font size.vhis 1/100 of viewport height;vwis 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, andinnerHTMLmanipulate the node tree.firstChildincludes text nodes and whitespace;firstElementChildconsiders only element nodes. - Events: common events include
onclick,onload,onmouseover,onsubmit,onfocus, andonblur.onblurfires when an element loses focus.addEventListener(type, handler, useCapture)attaches a handler without overwriting earlier handlers for the same event; theonclickproperty 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.
InaddEventListener,truemeans capture;falseor 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 usesaddEventListenerwithtrue. - Table content alignment: horizontal alignment of inline content in table cells is done with
text-align; vertical alignment in table cells is done withvertical-align, e.g.top,middle,bottom. HTMLalign/valignare 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
- The browser parses HTML into a DOM tree, with element nodes, text nodes, and attributes.
- It parses CSS from browser defaults, external stylesheets, internal
<style>blocks, and inlinestyleattributes. - For each element and property, the browser performs the cascade: collect declarations, handle
!important, compare specificity, and use source order for ties. - Inherited properties are passed from parents where applicable; non-inherited properties take their initial or declared value.
- The browser computes the box model for each element: content size, padding, border, and margin.
- Layout places normal-flow boxes; floats, positioning, and flex/grid rules then adjust placement.
- 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.
Drawing the diagram…
Formulas
-
Specificity convention, ignoring
!important: -
where = number of ID selectors, = number of class/attribute/pseudo-class selectors, = number of element/pseudo-element selectors. Inline style is treated separately as higher than any author selector.
-
Content-box dimensions:
-
where is padding, is border, L/R = left/right, T/B = top/bottom.
-
Horizontal space occupied including margins:
-
where is margin.
-
Border-box content width:
-
provided the result is non-negative.
-
Percentage child width:
-
where is the percentage expressed as a decimal.
-
Adjacent positive vertical margins collapse to:
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; }.
- element rule has specificity
(0,0,1). - class rule has
(0,1,0), higher. - id rule has
(1,0,0), higher than class. - 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 . Horizontal border .
Border-box width px.
Occupied horizontal space 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 px.
Child percentage width is based on the containing block content width, so child content width px.
Child border-box width px.
It overflows the container content area by 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 px, not 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
| Concept | Behaviour |
|---|---|
display: none | No box; removed from layout; descendants also not rendered unless special cases do not apply. |
visibility: hidden | Box exists; occupies space; not painted; a descendant can override with visibility: visible. |
opacity: 0 | Fully transparent but still occupies space and may receive events. |
content-box | width = content only; padding and border add to rendered size. |
border-box | width includes content + padding + border. |
position: relative | Keeps normal space; offset from normal position. |
position: absolute | Removed from normal flow; relative to nearest positioned ancestor or initial containing block. |
position: fixed | Removed from normal flow; relative to viewport. |
float: left | Shifted 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 property | One handler; assigning again overwrites. |
addEventListener | Multiple 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 > pis direct child;div pis any descendant.display:noneremoves space;visibility:hiddenkeeps space.absoluteuses nearest positioned ancestor;fixeduses viewport.- Adjacent positive vertical margins collapse to the larger value.
addEventListeneradds 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:noneandvisibility: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, andcalc()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-sizingchanges the meaning of declared width. - Cascade and specificity questions often place an inline
styleagainst ids, classes, and element rules; inline normally wins unless!importantchanges the result. - Event questions test
addEventListenervsonclick,onblur, capture/target/bubble order, and the legacycaptureEvents(eventType)syntax. - Table-alignment questions expect CSS concepts:
text-alignfor horizontal andvertical-alignfor vertical alignment of table-cell content; old HTMLalign/valignare obsolete presentational attributes. - Common traps: treating
visibility:hiddenas removed from layout, treatingabsoluteas always relative to the viewport, using:nth-childas if it were:nth-of-type, and adding margins to aborder-boxwidth incorrectly.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- List the four box-model areas from innermost to outermost and compute content-box/border-box widths.
- Write selectors for descendant, direct child, adjacent sibling, class, id, and attribute cases.
- Resolve a cascade conflict using specificity, source order, inline style, and
!important. - Distinguish
display:none,visibility:hidden, andopacity:0. - Explain
static,relative,absolute,fixed,float, andz-indexwith one use case each. - Match CSS units
em,rem,vh,vw, andcalc()to their meanings. - Explain DHTML’s components and why DHTML is not a W3C specification.
- Trace W3C event propagation and choose capture or bubble in
addEventListener. - Use DOM methods such as
getElementById,querySelector,createElement,appendChild,innerHTML, andgetComputedStyle. - Solve table-alignment and margin-collapse questions without confusing layout space with visible space.
Attempt the sub-topic test first, then the subject test.