CSS Cheatsheet

Positioning

Use this CSS reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

position Values

position: static;    /* default — in normal flow */
position: relative;  /* offset from own normal position; creates stacking context */
position: absolute;  /* removed from flow; positioned relative to nearest non-static ancestor */
position: fixed;     /* removed from flow; positioned relative to viewport */
position: sticky;    /* hybrid: relative until threshold, then sticks like fixed */

Offset Properties

top: 20px;
right: 0;
bottom: 10%;
left: auto;

/* Logical equivalents */
inset-block-start: 0;    /* top in horizontal writing modes */
inset-block-end: 0;      /* bottom */
inset-inline-start: 0;   /* left in LTR */
inset-inline-end: 0;     /* right in LTR */

/* Shorthand: top right bottom left */
inset: 0;            /* all sides 0 — same as top:0 right:0 bottom:0 left:0 */
inset: 0 auto;       /* top/bottom 0, left/right auto */
inset: 10px 20px 30px 40px;

position: relative

.box {
  position: relative;
  top: 10px;    /* moves down 10px, original space preserved */
  left: 20px;   /* moves right 20px */
}
/* Creates a stacking context (if z-index is set) */
/* Acts as containing block for absolutely-positioned children */

position: absolute

.parent {
  position: relative; /* establishes containing block */
}
.child {
  position: absolute;
  top: 0;
  right: 0;
  /* Positioned relative to nearest ancestor with position != static */
  /* Removed from flow — siblings ignore its space */
}

/* Common: stretch to fill parent */
.overlay {
  position: absolute;
  inset: 0;  /* top:0 right:0 bottom:0 left:0 */
}

/* Center in parent */
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

position: fixed

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  /* stays at top of viewport on scroll */
}

.fab {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
}

Fixed elements are removed from the normal flow. Adding transform, perspective, or filter to an ancestor breaks position: fixed (makes it relative to that ancestor instead of viewport).

position: sticky

.header {
  position: sticky;
  top: 0;      /* sticks when top edge reaches 0 from viewport top */
  /* remains in flow; reserves its original space */
}

/* Sticky table headers */
th {
  position: sticky;
  top: 0;
  background: white;
}

/* Horizontal sticky */
.label {
  position: sticky;
  left: 0;
}

Sticky requires the element to have a scrollable ancestor and the offset property set. It only sticks within its containing block.

Containing Block

Determines the reference for top/right/bottom/left and percentage sizes on positioned elements:

  • position: relative/sticky — own normal position.
  • position: absolute — nearest ancestor where position != static.
  • position: fixed — the viewport (or nearest ancestor with transform/filter/perspective).
  • position: absolute in elements with transform — that transformed ancestor.

z-index and Stacking Order

z-index: auto;    /* default — participates in parent stacking context */
z-index: 0;
z-index: 10;
z-index: -1;      /* behind normal flow content */
z-index: 9999;

Default stacking order (back to front): 1. Background and borders of the stacking context element 2. Negative z-index children 3. Block-level elements (non-positioned) 4. Float elements 5. Inline elements 6. Positioned elements with z-index: auto or 0 7. Positive z-index positioned elements

Creates a stacking context: - position with z-index != auto - opacity < 1 - transform, filter, backdrop-filter, perspective, clip-path - isolation: isolate - will-change with above properties - mix-blend-mode != normal - contain: paint

/* Isolate component's z-index to its own context */
.modal-wrapper {
  isolation: isolate;
}

Float and Clear (Legacy)

float: left;
float: right;
float: none;  /* default */

clear: left;
clear: right;
clear: both;
clear: none;

/* Modern clearfix (display: flow-root is preferred) */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

/* Preferred */
.container { display: flow-root; }

clip-path

clip-path: none;
clip-path: inset(10px 20px 30px 40px);          /* top right bottom left */
clip-path: inset(10px 20px round 8px);          /* with border-radius */
clip-path: circle(50%);
clip-path: circle(50px at 50% 50%);
clip-path: ellipse(100px 50px at center);
clip-path: polygon(50% 0%, 100% 100%, 0% 100%); /* triangle */
clip-path: path("M 0 0 L 100 0 L 100 100 Z");   /* SVG path */
clip-path: url(#my-svg-clip);

transform

transform: none;
transform: translate(10px, 20px);
transform: translateX(10px);
transform: translateY(20px);
transform: translateZ(10px);     /* 3D */
transform: translate3d(10px, 20px, 30px);
transform: scale(1.5);
transform: scale(1.5, 0.8);     /* x, y */
transform: scaleX(2);
transform: scaleY(0.5);
transform: rotate(45deg);
transform: rotateX(45deg);      /* 3D */
transform: rotateY(45deg);
transform: rotateZ(45deg);
transform: skew(10deg, 5deg);
transform: skewX(15deg);
transform: skewY(15deg);
transform: perspective(500px) rotateY(45deg); /* local perspective */
transform: matrix(a, b, c, d, e, f);

/* Multiple transforms — applied right to left */
transform: translateX(50px) rotate(45deg) scale(1.2);
transform-origin: center;       /* default */
transform-origin: top left;
transform-origin: 50% 50%;
transform-origin: 20px 40px;
transform-origin: 0 0 10px;    /* 3D */

transform-box: border-box;      /* reference box for transform-origin */
transform-box: fill-box;
transform-box: content-box;

perspective (3D Transforms)

/* On the parent — applies to all children */
.scene {
  perspective: 800px;
  perspective-origin: 50% 50%;
}
.cube { transform: rotateY(45deg); }

/* Flat vs preserve-3d */
transform-style: flat;           /* default — children flattened */
transform-style: preserve-3d;    /* children keep 3D position */

/* Backface visibility */
backface-visibility: visible;    /* default */
backface-visibility: hidden;     /* hide back of rotated element */

Scroll Behavior

/* Smooth anchor scroll */
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

/* Offset for sticky headers */
.section { scroll-margin-top: 80px; }

/* Snap scrolling */
.container {
  overflow-x: scroll;
  scroll-snap-type: x mandatory;  /* axis: x|y|both; strictness: mandatory|proximity */
}
.slide {
  scroll-snap-align: start;       /* start | center | end */
  scroll-snap-stop: always;       /* prevent skipping */
}
.container { scroll-padding: 1rem; } /* offset snap points */

will-change

/* Hint browser to prepare compositing layer */
.animated { will-change: transform, opacity; }

/* Reset after animation */
.animated.done { will-change: auto; }
/* Do NOT set on too many elements — creates unnecessary layers */