Common CSS Attributes
A collection of commonly used CSS properties — ordered as a learning path from box model → layout → animation.
💡This page includes both a quick-reference table and sample code for each group of properties. New to CSS → read it in order. Already comfortable → use the table of contents on the left to jump.
1. Box Model — the foundation
Every element is a box made of 4 layers: content → padding → border → margin. Understanding the box model is the first prerequisite for laying things out correctly.
| Property | Description | Example |
|---|---|---|
box-sizing | How width/height are computed. border-box = includes padding + border (recommended). | box-sizing: border-box |
margin | Space from the box edge outward. margin: auto to center automatically. | margin: 0 auto |
padding | Space from the box edge inward to the content. | padding: 1rem 2rem |
border | Border — thickness, line style, color. | border: 1px solid #000 |
overflow | Handle overflowing content: visible | hidden | scroll | auto. | overflow: hidden |
2. Display — how an element renders
cssdisplay: block; /* takes the whole line, accepts width/height */ display: inline; /* only as wide as its content, does NOT accept width/height */ display: inline-block; /* inline + accepts width/height */ display: flex; /* 1D layout — see the Flexbox section */ display: grid; /* 2D layout — see the Grid section */ display: contents; /* element disappears, keeps its children */ display: none; /* removed from flow, takes no space */
⚠️
display: none differs from visibility: hidden — hidden still takes up space.3. Position — positioning an element
cssposition: static; /* default, ignores top/left/... */ position: relative; /* anchor for absolute children, shifts from its original spot */ position: absolute; /* leaves the flow, references the nearest relative ancestor */ position: fixed; /* references the viewport — always pinned */ position: sticky; /* pins once scrolled past a threshold */ top: 10px; /* position measured from the top */ bottom: 10px; /* from the bottom */ left: 10px; /* from the left */ right: 10px; /* from the right */ inset: 0; /* shorthand: top/right/bottom/left = 0 */ z-index: 10; /* stacking order */
4. Text & Font
| Property | Description | Example |
|---|---|---|
color | Text color. | color: #333 |
font-size | Text size. | font-size: 1rem |
font-weight | Boldness: 100–900, bold. | font-weight: 700 |
font-family | Typeface. Always include a final fallback. | Inter, sans-serif |
font-style | normal | italic. | font-style: italic |
line-height | Line spacing. 1.4–1.6 is easy to read. | line-height: 1.5 |
letter-spacing | Space between characters. | 0.02em |
text-align | Horizontal alignment: left/center/right/justify. | text-align: center |
text-transform | uppercase | lowercase | capitalize. | text-transform: uppercase |
text-decoration | underline | none | line-through. | text-decoration: none |
text-overflow | ellipsis — must combine the two properties below. | ellipsis |
css/* Truncate a single line with "..." */ .truncate { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
5. Background
cssbackground-color: #fff; background-image: url(bg.jpg); background-image: linear-gradient(to right, red, orange, yellow); background-image: -webkit-linear-gradient(left, red 0%, orange 50%, yellow 100%); background-size: cover; /* fill 100% of the box, may crop */ background-size: contain; /* show the whole image, may leave gaps */ background-position: center; background-repeat: no-repeat; background-attachment: fixed; /* fixed while scrolling — parallax */ background-clip: text; /* clip the background image to the text — combine with -webkit-text-fill-color: transparent */ /* Shorthand */ background: #fff url(bg.jpg) no-repeat center/cover fixed;
6. Border / Radius / Shadow
cssborder: 1px solid #ddd; border-radius: 0.5rem; border-radius: 50%; /* circle — apply to a square element */ outline: 2px solid blue; /* not counted in the box, does not push layout */ outline-offset: 4px; box-shadow: 2px 2px 7px rgba(0,0,0,.2); /* ↑ ↑ ↑ ↑ x y blur color positive x = to the right, positive y = downward */
7. Flexbox — one-dimensional layout
Enable flex with display: flex or display: inline-flex.
flex-direction — the main axis
| Property | Description | Example |
|---|---|---|
row | Along a row (default), equivalent to float:left. | flex-direction: row |
row-reverse | Along a row from end to start. | flex-direction: row-reverse |
column | Along a column from top to bottom. | flex-direction: column |
column-reverse | Along a column from bottom to top. | flex-direction: column-reverse |
flex-wrap — wrap to a new line?
| Property | Description | Example |
|---|---|---|
nowrap | No wrapping — force everything on one row (default). | |
wrap | Wraps to a new line once the parent width is filled. | |
wrap-reverse | Reverses the order of the lines. |
justify-content — horizontal alignment (along the main axis)
| Property | Description | Example |
|---|---|---|
flex-start | Pack to the left. | |
flex-end | Pack to the right. | |
center | Center (≈ text-align: center). | |
space-between | Even spacing, with the two ends flush against the edges. | |
space-around | Even spacing with margins on the two outer sides. | |
space-evenly | Equal spacing between every item and the edges. |
align-items — vertical alignment (cross axis)
The outer box needs a height to see the effect.
| Property | Description | Example |
|---|---|---|
stretch | Fill the height (default). | |
flex-start | Pack to the top. | |
flex-end | Pack to the bottom. | |
center | Center vertically. | |
baseline | Align text along the baseline. |
Item-level
css.parent { display: flex; gap: 1rem; } .child { flex: 1; /* shorthand: grow shrink basis (1 1 0) */ flex: 0 0 200px; /* fixed 200px — neither grows nor shrinks */ align-self: center; /* override align-items for this item only */ order: 2; /* change the order */ }
8. Grid — two-dimensional layout
css.grid { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* responsive */ grid-template-rows: auto 1fr auto; grid-template-areas: "header header" "side main" "footer footer"; gap: 1rem; } .item { grid-area: header; grid-column: 1 / 3; /* span from column 1 up to before column 3 */ grid-column: span 2; }
9. Transform & Transition
css/* Transform — change an element's appearance, does NOT re-layout */ transform: translate(-50%, -50%); transform: rotate(-45deg); transform: scale(1.1); /* zoom in */ transform: translate(10px, 0) rotate(45deg) scale(1.1); /* combine */ /* Transition — animate the change between 2 states */ transition: all 0.3s ease; transition: transform 0.2s, opacity 0.2s; transition: 0.4s; /* shorthand */
💡Prefer animating
transform & opacity— they run on the GPU and don't trigger reflow.10. Animation with @keyframes
css@keyframes fade-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } } .box { /* shorthand: name | duration | timing | delay | count | direction | fill-mode */ animation: fade-in 0.4s ease 0s 1 normal forwards; }
| Property | Description | Example |
|---|---|---|
animation-name | The @keyframes name to apply. | |
animation-duration | Duration. | 0.4s |
animation-timing-function | Easing curve: ease | linear | cubic-bezier(...) | |
animation-delay | Delay before it starts. | |
animation-iteration-count | Number of runs. infinite = endless. | |
animation-direction | normal | reverse | alternate. | |
animation-fill-mode | State before/after the animation runs: forwards keeps the final state. |
11. Commonly used selectors & pseudo-classes
css/* Pseudo-class */ li:nth-child(3) { ... } /* the 3rd element */ li:nth-child(odd) { ... } /* odd ones */ li:nth-child(2n+1) { ... } li:first-child { ... } li:last-child { ... } a:hover, a:focus { ... } /* Pseudo-element */ .box::before { content: ''; } .box::after { content: ''; } p::first-letter { font-size: 2em; } /* Combinator */ .parent > .child /* direct child */ .prev + .next /* immediately adjacent sibling */ .prev ~ .siblings /* all following siblings */
12. Cheat sheet — quick notes
box-sizing: border-box— set it globally on every element to avoid width headaches.margin: 0 auto— horizontally center a block element.- Hide an element while keeping accessibility:
opacity: 0or thesr-onlyclass, notdisplay: none. - Absolute centering:
position: absolute; inset: 0; margin: autoor use flex/grid withplace-items: center. float: left/right— an old technique; use flex/grid these days.- Background gradient + clip text:
background-clip: text; -webkit-text-fill-color: transparent.