CSS Web Programming Basics

CSS Overview & Implementation

  • Cascading Style Sheets (CSS): A stylesheet language used to describe the presentation and layout of HTML documents.

  • Implementation Methods:

    • Inline: Defined directly on an HTML element via the style attribute.

    • Internal: Created using <style> elements within the document <head>.

    • External: Authored in a separate .css file and linked in the <head> using <link rel="stylesheet" href="...">.

Selectors & Syntax


CSS Rule Parts
  • CSS Rule Structure: Consists of a selector targeting elements and a declaration block of key-value property pairs wrapped in curly braces {}.

  • Simple Selectors:

    • Universal (*): Matches all HTML elements in the document.

    • Type: Targets elements directly by tag name (e.g., p, h1).

    • Class (.className): Targets elements matching a specific class attribute.

    • ID (#idName): Targets a unique element with a specific id attribute.

    • Attribute ([element]): Targets elements based on attribute presence or value.

    • Grouping (h1, h2, p): Applies shared styles to multiple comma-separated selectors.

  • Pseudo-Selectors:

    • Pseudo-Classes (:pseudo-class): Apply styles based on element state or context (e.g., :hover, :focus, :active, :first-child, :nth-child(), :disabled, :checked).

    • Pseudo-Elements (::pseudo-element): Target specific parts of an element or insert content (e.g., ::before, ::after, ::first-letter, ::first-line, ::placeholder, ::selection, ::marker).

Box Model & Display Types


Box Model Diagram
  • Box Model Layers:

    • Content Box: The region where text or child elements reside.

    • Padding Box: Space surrounding the content box; inherits the element's background.

    • Border Box: Frame encompassing the padding box, defined by border properties.

    • Margin Box: Outer transparent spacing separating the element from surrounding boxes.

  • Sizing Concepts:

    • Intrinsic Sizing: Box size adjusts automatically based on its contained content.

    • Extrinsic Sizing: Box size is strictly determined by explicit CSS dimensions.

    • Default Box Sizing: User agent stylesheets set box-sizing: content-box by default, applying width and height solely to the content box while adding padding and borders externally.

  • Display Properties:

    • block: Starts on a new line and occupies full container width.

    • inline: Flows along with text and ignores vertical box properties like height or vertical margin.

    • inline-block: Flows inline while accepting explicit width, height, padding, and margin properties.

Inheritance

  • Inherited Properties by Default: Text and typography-related styles naturally inherit from parent to child elements (e.g., color, font-family, line-height).

  • Non-Inherited Properties: Layout and box-model properties do not inherit by default (e.g., margin, padding, border, width, height).

  • Forcing Inheritance: Properties can be manually forced to inherit using property: inherit or all: inherit.

Color Representations

  • Named Keywords: Predefined CSS color terms (e.g., red, tan, rebeccapurple, transparent).

  • Hexadecimal (HEX): Uses base-16 ranges (0−90-9, A-F\text{A-F}) translating to channel values 0−2550-255:

    • 6-digit: #RRGGBB (e.g., #000000 for black, #ffffff for white).

    • 8-digit (Alpha): #RRGGBBAA (e.g., #00000080 for 50%50\% opacity).

  • RGB / RGBA:

    • Standard syntax: rgb(red green blue) or rgb(r, g, b) using integers 0−2550-255 or percentages 0%−100%0\%-100\%.

    • Alpha syntax: rgb(r g b / alpha) or rgba(r, g, b, alpha) where alpha ranges from 00 to 11 or 0%0\% to 100%100\%.

  • HSL Format:

    • hsl(hue saturation lightness)

    • Hue: Color position on the 0∘−360∘0^\circ-360^\circ color wheel (0∘/360∘0^\circ/360^\circ = red, 180∘180^\circ = blue).

    • Saturation: Color intensity (0%0\% = grayscale, 100%100\% = full color).

    • Lightness: Brightness level (0%0\% = black, 100%100\% = white).

Sizing Units

  • Percentages (%): Relative to parent element dimensions.

  • Absolute Lengths (Fixed References):

    • px (Pixels): 1 px=1/96 in1\,\text{px} = 1/96\,\text{in}.

    • in (Inches): 1 in=96 px1\,\text{in} = 96\,\text{px}.

    • cm (Centimeters): 1 cm=37.8 px1\,\text{cm} = 37.8\,\text{px}.

    • mm (Millimeters): 1 mm=1/10 cm1\,\text{mm} = 1/10\,\text{cm}.

    • Q (Quarter-millimeters): 1Q=1/40 cm1\text{Q} = 1/40\,\text{cm}.

    • pc (Picas): 1 pc=1/6 in1\,\text{pc} = 1/6\,\text{in}.

    • pt (Points): 1 pt=1/72 in1\,\text{pt} = 1/72\,\text{in}.

  • Font-Relative Units:

    • em: Relative to element font size.

    • rem: Relative to root (<html>) font size (default 16 px16\,\text{px}).

    • ex / rex: Relative to font x-height (element / root).

    • ch / rch: Relative to width of character "0" (zero).

    • cap / rcap: Relative to height of capital letters.

    • lh / rlh: Relative to line height.

  • Viewport-Relative Units:

    • vw / vh: 1%1\% of viewport width / height.

    • vmin / vmax: 1%1\% of smaller / larger viewport dimension.

  • Container-Relative Units:

    • cqw / cqh: 1%1\% of container width / height.

    • cqmin / cqmax: 1%1\% of smaller / larger container dimension.

Flexbox Layout


Flexbox Axes
  • Overview: A 1D layout model for distributing space and aligning items along a single axis (display: flex).

  • Axes:

    • Main Axis: Primary direction determined by flex-direction (default horizontal).

    • Cross Axis: Axis running perpendicular to the main axis.

  • Key Properties:

    • flex-direction: row, row-reverse, column, column-reverse.

    • justify-content: Aligns items along main axis (flex-start, flex-end, center, space-between, space-around, space-evenly).

    • align-items: Aligns items along cross axis (flex-start, flex-end, center, stretch, baseline).

    • flex-wrap: Controls line wrapping (nowrap default, wrap).

Grid Layout


Grid Track
  • Overview: A 2D layout system managing rows and columns simultaneously (display: grid).

  • Core Components:

    • Grid Lines: Numbered horizontal and vertical dividing lines starting at index 11.

    • Grid Tracks: Space between two adjacent grid lines (rows or columns).

    • Grid Cell: The single intersection unit of a row and column track.

    • Grid Area: Group of contiguous grid cells spanning multiple tracks.

    • Grid Gap: Gutter spacing between tracks where content cannot be directly placed.

Positioning & Z-Index

  • Positioning Types:

    • static: Default normal document flow; ignores offset properties.

    • relative: Remains in document flow, offset relative to its original position.

    • absolute: Removed from flow; positioned relative to nearest non-static ancestor.

    • fixed: Removed from flow; positioned relative to viewport, staying fixed during scroll.

    • sticky: Acts as relative until reaching a scroll threshold, then acts as fixed.

  • Z-Index & Stacking:

    • z-index Property: Sets depth alignment along Z-axis using positive or negative integers.

    • Source Order Default: Without z-index, elements defined later in document source stack above earlier ones.

    • Stacking Context: Formed by positioned elements with specified z-index. Child elements stack exclusively within their parent's stacking context.

Media Queries

  • Purpose: Applies CSS rules conditionally based on device capabilities and viewport attributes.

  • Syntax: @media [not|only] mediatype and (mediafeature) { ... }

  • Media Types: all (default), print, screen.

  • Media Features: width, height, orientation (portrait / landscape), aspect-ratio.