2026-08-14 08:17
Status:
Tags: Guide HTML Web Development Front End Programming Lanuages Programming Concepts CSS Notes Websites CheatSheet
Common CSS Elements
A quick-reference guide for common CSS properties, values, and how they interact with HTML elements.
1. Box Model Properties
| Property | Common Values | Notes |
|---|---|---|
width / height | px, %, auto, vh/vw, fit-content | Applies to block-level & replaced elements (img, video) |
margin | px, %, auto, shorthand 10px 20px | Space outside the border; margins can collapse vertically |
padding | px, %, shorthand 10px 20px | Space inside the border, around content |
border | 1px solid #000 | Shorthand for width/style/color |
box-sizing | content-box (default), border-box | border-box includes padding/border in width/height calc |
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 1px solid #ccc;
}2. Typography
| Property | Common Values | Applies To |
|---|---|---|
font-family | "Helvetica Neue", Arial, sans-serif | Text elements (p, h1-h6, span, a, etc.) |
font-size | px, rem, em, % | Text elements |
font-weight | normal, bold, 100–900 | Text elements |
line-height | unitless (1.5), px | Block & inline text elements |
text-align | left, center, right, justify | Block-level containers |
text-decoration | none, underline, line-through | Inline elements, especially a |
letter-spacing | px, em | Text 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
| Property | Common Values | Notes |
|---|---|---|
color | hex, rgb(), rgba(), hsl(), named colors | Sets text color |
background-color | same as above | Any element |
background-image | url(...), linear-gradient(...) | Block-level elements typically |
background-size | cover, contain, px/% | Works with background-image |
opacity | 0–1 | Affects entire element incl. children |
.card {
background-color: rgba(255, 255, 255, 0.9);
color: #222;
}4. Display & Positioning
| Property | Common Values | Element Behavior |
|---|---|---|
display | block, inline, inline-block, flex, grid, none | Changes how the element participates in layout |
position | static, relative, absolute, fixed, sticky | Controls positioning context |
top / right / bottom / left | px, % | Used with non-static position |
z-index | integer | Stacking order; only works on positioned elements |
float | left, right, none | Legacy layout tool, still used for text wrap |
Default display by element type:
div,p,h1–h6,ul,li→blockspan,a,strong,em→inlineimg,input,button→inline-block
5. Flexbox (Container & Items)
| Property | Applies To | Common Values |
|---|---|---|
display: flex | Parent | Turns children into flex items |
flex-direction | Parent | row, column, row-reverse |
justify-content | Parent | flex-start, center, space-between, space-around |
align-items | Parent | flex-start, center, stretch |
gap | Parent | px, rem — spacing between children |
flex-grow / flex-shrink | Child | integer — how items expand/shrink |
flex-basis | Child | px, %, 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 Case | Why Flexbox Fits |
|---|---|
| Navigation bar | Logo, links, and buttons in a row, spaced apart |
| Centering content | justify-content: center + align-items: center centers anything, any size |
| Card with icon + text | Align icon and text on one line regardless of text length |
| Button group / toolbar | Even 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)
| Property | Applies To | Common Values |
|---|---|---|
display: grid | Parent | Turns children into grid items |
grid-template-columns | Parent | 1fr 1fr 1fr, repeat(3, 1fr) |
grid-template-rows | Parent | auto, 100px 200px |
gap | Parent | px, rem |
grid-column / grid-row | Child | span 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 Case | Why Grid Fits |
|---|---|
| Page layout (header/sidebar/main/footer) | Define the whole layout structure in one place |
| Photo gallery / card grid | Equal-sized cells that wrap automatically |
| Dashboard widgets | Items span multiple rows/columns of a defined grid |
| Forms with aligned labels & inputs | Columns keep labels and inputs lined up across rows |
| Magazine-style/asymmetric layouts | Precise 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
| Question | Answer |
|---|---|
| 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 Element | CSS Interactions to Know |
|---|---|
a | Default display: inline; needs display: block/inline-block to accept width/height. Pseudo-classes: :hover, :visited, :active |
img | Replaced element — respects width/height directly; use object-fit: cover/contain to control cropping |
button / input | Often need appearance: none to strip default browser styling |
ul / ol | list-style-type: none to remove bullets/numbers; padding-left controls indent |
table | border-collapse: collapse merges double borders between cells |
body | Margins 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
| Unit | Relative To | Best For |
|---|---|---|
px | Fixed | Borders, fine details |
% | Parent element | Fluid widths |
em | Parent font-size | Component-scoped spacing |
rem | Root font-size | Global consistent spacing/type |
vh / vw | Viewport height/width | Full-screen sections |
fr | Grid container | Grid track sizing |
10. Selectors Quick Reference
| Selector | Example | Matches |
|---|---|---|
.class | .card | Elements with class="card" |
#id | #header | Element with id="header" |
element | p | All <p> tags |
element, element | h1, h2 | Either matches |
parent child | nav a | Any a inside nav |
parent > child | ul > li | Direct children only |
[attr] | [disabled] | Elements with that attribute |
:hover, :focus | a:hover | Pseudo-class states |
::before, ::after | .icon::before | Pseudo-elements |
Last updated: reference guide — expand with your own notes as you learn new patterns.
References
See also IHCJ - Module 2 - CSS Overview & HTML5 Elements for some more detail