CSS

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.

PropertyDescriptionExample
box-sizingHow width/height are computed. border-box = includes padding + border (recommended).box-sizing: border-box
marginSpace from the box edge outward. margin: auto to center automatically.margin: 0 auto
paddingSpace from the box edge inward to the content.padding: 1rem 2rem
borderBorder — thickness, line style, color.border: 1px solid #000
overflowHandle overflowing content: visible | hidden | scroll | auto.overflow: hidden

2. Display — how an element renders

css
display: 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

css
position: 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

PropertyDescriptionExample
colorText color.color: #333
font-sizeText size.font-size: 1rem
font-weightBoldness: 100–900, bold.font-weight: 700
font-familyTypeface. Always include a final fallback.Inter, sans-serif
font-stylenormal | italic.font-style: italic
line-heightLine spacing. 1.4–1.6 is easy to read.line-height: 1.5
letter-spacingSpace between characters.0.02em
text-alignHorizontal alignment: left/center/right/justify.text-align: center
text-transformuppercase | lowercase | capitalize.text-transform: uppercase
text-decorationunderline | none | line-through.text-decoration: none
text-overflowellipsis — must combine the two properties below.ellipsis
css
/* Truncate a single line with "..." */ .truncate { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

5. Background

css
background-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

css
border: 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

PropertyDescriptionExample
rowAlong a row (default), equivalent to float:left.flex-direction: row
row-reverseAlong a row from end to start.flex-direction: row-reverse
columnAlong a column from top to bottom.flex-direction: column
column-reverseAlong a column from bottom to top.flex-direction: column-reverse

flex-wrap — wrap to a new line?

PropertyDescriptionExample
nowrapNo wrapping — force everything on one row (default).
wrapWraps to a new line once the parent width is filled.
wrap-reverseReverses the order of the lines.

justify-content — horizontal alignment (along the main axis)

PropertyDescriptionExample
flex-startPack to the left.
flex-endPack to the right.
centerCenter (≈ text-align: center).
space-betweenEven spacing, with the two ends flush against the edges.
space-aroundEven spacing with margins on the two outer sides.
space-evenlyEqual spacing between every item and the edges.

align-items — vertical alignment (cross axis)

The outer box needs a height to see the effect.

PropertyDescriptionExample
stretchFill the height (default).
flex-startPack to the top.
flex-endPack to the bottom.
centerCenter vertically.
baselineAlign 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; }
PropertyDescriptionExample
animation-nameThe @keyframes name to apply.
animation-durationDuration.0.4s
animation-timing-functionEasing curve: ease | linear | cubic-bezier(...)
animation-delayDelay before it starts.
animation-iteration-countNumber of runs. infinite = endless.
animation-directionnormal | reverse | alternate.
animation-fill-modeState 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: 0 or the sr-only class, not display: none.
  • Absolute centering: position: absolute; inset: 0; margin: auto or use flex/grid with place-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.
Chào bạn, mình là Nova. Có phần nào bạn muốn hiểu kỹ hơn không? Cứ nói với mình nhé.