2026-08-14 08:17

Status:


Common CSS Elements

A quick-reference guide for common CSS properties, values, and how they interact with HTML elements.


1. Box Model Properties

PropertyCommon ValuesNotes
width / heightpx, %, auto, vh/vw, fit-contentApplies to block-level & replaced elements (img, video)
marginpx, %, auto, shorthand 10px 20pxSpace outside the border; margins can collapse vertically
paddingpx, %, shorthand 10px 20pxSpace inside the border, around content
border1px solid #000Shorthand for width/style/color
box-sizingcontent-box (default), border-boxborder-box includes padding/border in width/height calc
.box {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 1px solid #ccc;
}

2. Typography

PropertyCommon ValuesApplies To
font-family"Helvetica Neue", Arial, sans-serifText elements (p, h1-h6, span, a, etc.)
font-sizepx, rem, em, %Text elements
font-weightnormal, bold, 100–900Text elements
line-heightunitless (1.5), pxBlock & inline text elements
text-alignleft, center, right, justifyBlock-level containers
text-decorationnone, underline, line-throughInline elements, especially a
letter-spacingpx, emText elements

Tip: Use rem for font-size so text scales with the user’s root font settings — better accessibility than fixed px.


3. Color & Background

PropertyCommon ValuesNotes
colorhex, rgb(), rgba(), hsl(), named colorsSets text color
background-colorsame as aboveAny element
background-imageurl(...), linear-gradient(...)Block-level elements typically
background-sizecover, contain, px/%Works with background-image
opacity0–1Affects entire element incl. children
.card {
  background-color: rgba(255, 255, 255, 0.9);
  color: #222;
}

4. Display & Positioning

PropertyCommon ValuesElement Behavior
displayblock, inline, inline-block, flex, grid, noneChanges how the element participates in layout
positionstatic, relative, absolute, fixed, stickyControls positioning context
top / right / bottom / leftpx, %Used with non-static position
z-indexintegerStacking order; only works on positioned elements
floatleft, right, noneLegacy layout tool, still used for text wrap

Default display by element type:

  • div, p, h1–h6, ul, li → block
  • span, a, strong, em → inline
  • img, input, button → inline-block

5. Flexbox (Container & Items)

PropertyApplies ToCommon Values
display: flexParentTurns children into flex items
flex-directionParentrow, column, row-reverse
justify-contentParentflex-start, center, space-between, space-around
align-itemsParentflex-start, center, stretch
gapParentpx, rem — spacing between children
flex-grow / flex-shrinkChildinteger — how items expand/shrink
flex-basisChildpx, %, auto — starting size
.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
}

When to Use Flexbox

Flexbox is one-dimensional — it excels at laying items out in a single row or column, and distributing/aligning them within that line.

Use CaseWhy Flexbox Fits
Navigation barLogo, links, and buttons in a row, spaced apart
Centering contentjustify-content: center + align-items: center centers anything, any size
Card with icon + textAlign icon and text on one line regardless of text length
Button group / toolbarEven spacing between buttons that can wrap on small screens
Sidebar + main content (simple)Two flex items, one fixed width, one flex-grow: 1

Example — Navbar:

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}
<nav class="navbar">
  <div class="logo">Brand</div>
  <ul class="nav-links">...</ul>
  <button>Sign In</button>
</nav>

Example — Perfectly Centered Modal:

.overlay {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

6. Grid (Container & Items)

PropertyApplies ToCommon Values
display: gridParentTurns children into grid items
grid-template-columnsParent1fr 1fr 1fr, repeat(3, 1fr)
grid-template-rowsParentauto, 100px 200px
gapParentpx, rem
grid-column / grid-rowChildspan 2, 1 / 3

When to Use Grid

Grid is two-dimensional — it excels when you need to control rows and columns together, especially for overall page layout.

Use CaseWhy Grid Fits
Page layout (header/sidebar/main/footer)Define the whole layout structure in one place
Photo gallery / card gridEqual-sized cells that wrap automatically
Dashboard widgetsItems span multiple rows/columns of a defined grid
Forms with aligned labels & inputsColumns keep labels and inputs lined up across rows
Magazine-style/asymmetric layoutsPrecise placement via grid-column/grid-row spans

Example — Page Layout:

.page {
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }

Example — Responsive Card Gallery (no media queries needed):

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

Flexbox vs. Grid — Quick Decision Guide

QuestionAnswer
Laying out in one direction only (row or column)?Flexbox
Need to control rows and columns together?Grid
Content size should drive layout (items wrap naturally)?Flexbox
Layout structure should drive content placement?Grid
Aligning a handful of items (nav, buttons, icon+text)?Flexbox
Building the overall page/app skeleton?Grid
Can combine both?Yes — Grid for page layout, Flexbox inside individual grid cells (e.g., a card’s internal content)

7. Element-Specific Notes

HTML ElementCSS Interactions to Know
aDefault display: inline; needs display: block/inline-block to accept width/height. Pseudo-classes: :hover, :visited, :active
imgReplaced element — respects width/height directly; use object-fit: cover/contain to control cropping
button / inputOften need appearance: none to strip default browser styling
ul / ollist-style-type: none to remove bullets/numbers; padding-left controls indent
tableborder-collapse: collapse merges double borders between cells
bodyMargins default to 8px in most browsers — commonly reset with margin: 0

8. Common Resets

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
 
body {
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}

9. Units Cheat Sheet

UnitRelative ToBest For
pxFixedBorders, fine details
%Parent elementFluid widths
emParent font-sizeComponent-scoped spacing
remRoot font-sizeGlobal consistent spacing/type
vh / vwViewport height/widthFull-screen sections
frGrid containerGrid track sizing

10. Selectors Quick Reference

SelectorExampleMatches
.class.cardElements with class="card"
#id#headerElement with id="header"
elementpAll <p> tags
element, elementh1, h2Either matches
parent childnav aAny a inside nav
parent > childul > liDirect children only
[attr][disabled]Elements with that attribute
:hover, :focusa:hoverPseudo-class states
::before, ::after.icon::beforePseudo-elements

Last updated: reference guide — expand with your own notes as you learn new patterns.


References