TutorialsCSSCSS Basics
Share:

CSS Basics

beginner

Part of CSS Fundamentals

Theory

CSS basics form the foundation of all styling. Understanding selectors, colors, the box model, and units is essential before moving to more complex layouts.

CSS Selectors

Selectors determine which elements are styled.

Element Selector

Targets all instances of a given HTML element:

h1 { color: navy; }
p { font-size: 16px; }

Class Selector

Targets elements with a specific class attribute. Prefixed with a dot (.):

.highlight { background: yellow; }
.card { border: 1px solid #ccc; }

HTML: <p class="highlight">This is highlighted.</p>

ID Selector

Targets a single element with a specific id. Prefixed with a hash (#):

#logo { width: 200px; }
#main-nav { background: #333; }

HTML: <div id="logo">...</div>

Use IDs sparingly — one per page, and prefer classes for reusable styles.

Universal Selector

Targets every element on the page:

* { margin: 0; padding: 0; box-sizing: border-box; }

Grouping Selectors

Apply the same styles to multiple selectors by separating with commas:

h1, h2, h3 { font-family: Arial, sans-serif; }

Descendant Selector

Targets elements nested inside another element:

article p { line-height: 1.8; }
/* Targets only <p> inside <article> */

Colors in CSS

CSS offers multiple ways to specify colors:

  • Named colors: red, blue, green, tomato, rebeccapurple — 140+ named colors
  • Hexadecimal (Hex): #FF5733 — six-digit code (RRGGBB). Can be shortened to 3 digits if pairs match: #F53
  • RGB: rgb(255, 87, 51) — red, green, blue values (0–255 each)
  • RGBA: rgba(255, 87, 51, 0.5) — RGB + alpha channel for opacity (0 to 1)
  • HSL: hsl(9, 100%, 60%) — hue (0–360), saturation (0–100%), lightness (0–100%)
  • HSLA: hsla(9, 100%, 60%, 0.5) — HSL + alpha
.example {
  color: #333;          /* dark gray hex */
  background: rgb(240, 240, 245);  /* light blue-gray */
  border-color: hsl(200, 50%, 40%); /* medium blue */
}

Backgrounds

The background shorthand sets all background properties:

.card {
  background: #fff url("pattern.png") no-repeat center/cover;
}

Individual properties:

  • background-color — solid color
  • background-image — image URL
  • background-repeatrepeat, no-repeat, repeat-x, repeat-y
  • background-positioncenter, top left, 50% 50%
  • background-sizecover, contain, custom dimensions
  • background-attachmentscroll, fixed, local

Borders

.box {
  border: 2px solid #333;          /* shorthand: width style color */
  border-radius: 8px;              /* rounded corners */
  border-top: 1px dashed red;      /* individual side */
  outline: 3px solid blue;         /* outline (outside border, not in box model) */
}

Border styles: solid, dashed, dotted, double, groove, ridge, inset, outset, none.

The Box Model

Every element in CSS is treated as a rectangular box consisting of:

+-------------------------------------------+
|              Margin (transparent)          |
|  +-------------------------------------+  |
|  |           Border                    |  |
|  |  +-------------------------------+  |  |
|  |  |        Padding                |  |  |
|  |  |  +-------------------------+  |  |  |
|  |  |  |      Content           |  |  |  |
|  |  |  |   (width x height)    |  |  |  |
|  |  |  +-------------------------+  |  |  |
|  |  +-------------------------------+  |  |
|  +-------------------------------------+  |
+-------------------------------------------+
  • Content — the actual content (text, image)
  • Padding — space between content and border (inside the element)
  • Border — line around the padding
  • Margin — space outside the border (between elements)

Box-Sizing

By default, width and height apply only to the content box. Padding and border add to the total size:

/* Default (content-box): width = content only */
.box { width: 300px; padding: 20px; border: 2px solid; }
/* Total width = 300 + 40 + 4 = 344px */
 
/* Border-box: width includes padding and border */
.box { width: 300px; padding: 20px; border: 2px solid; box-sizing: border-box; }
/* Total width = 300px (content shrinks to 256px) */

Always use box-sizing: border-box globally for intuitive sizing:

*, *::before, *::after { box-sizing: border-box; }

CSS Units

| Unit | Type | Description | |------|------|-------------| | px | Absolute | Pixels (1px = 1/96th of an inch) | | rem | Relative | Relative to root (<html>) font-size (default 16px) | | em | Relative | Relative to parent's font-size | | % | Relative | Percentage of parent element | | vw | Relative | 1% of viewport width | | vh | Relative | 1% of viewport height | | vmin | Relative | 1% of the smaller dimension (vw or vh) | | vmax | Relative | 1% of the larger dimension (vw or vh) | | cm, mm, in | Absolute | Physical units (rarely used on screen) |

Best practices:

  • Use rem for font sizes (respects user's browser settings)
  • Use % for widths of containers
  • Use vw/vh for full-screen sections
  • Use px for borders, shadows, and precise spacing

The default browser font size is 16px. So 1rem = 16px, 2rem = 32px, etc. Users can change their default font size, and rem-based designs respect that.

Practical Examples

Example: Selectors, Colors, and Backgrounds
html
Example: Box Model Visualization
html

Exercises

Style with Different Selectors

easy

Create an HTML page with at least 5 different elements. Style them using: element selector (for h1), class selector (for paragraphs), ID selector (for one div), descendant selector (for links inside nav), and grouping selector (for multiple headings).

Expected Output:

A styled page where h1, .intro, #main, nav a, and h2/h3 each have distinct styles applied through different selector types.

Box Model Practice

medium

Create three cards side by side. Each card should have a width of 300px, 20px padding, a 2px border, and 15px margin. Use box-sizing: border-box on all elements. The cards should have different background colors.

Expected Output:

Three cards displayed side by side (use inline-block or flex). Each card is visually the same size with text centered inside. Cards have margins creating space between them.

Mini Quiz

Mini Quiz

Mini Project

Mini Project: Styled Card Component

Design and build a reusable card component using CSS. The card should demonstrate your understanding of selectors, colors, backgrounds, borders, the box model, and CSS units.

Requirements:

    Bonus Challenge

    Create a card variant class (e.g., .card-dark) that overrides the colors while keeping the same structural styles. This demonstrates CSS reuse.