JcHub
Cheat sheetFree, no sign-upPDF

HTML5 & CSS Cheat Sheet: Flexbox and Grid

A practical reference guide to structure HTML5 pages and master modern web layouts using CSS Flexbox and Grid.

Updated on November 10, 2026

Essential HTML5 Structure

  • <meta name="viewport" content="width=device-width, initial-scale=1.0">Ensures proper mobile rendering. Without it, mobile browsers render pages at desktop width and scale them down.
  • <header>, <nav>, <main>, <footer>Semantic HTML5 elements that define structure. Avoid replacing everything with generic div tags for better SEO and accessibility.
  • <picture><source srcset="img.webp"><img src="img.png" alt="Card"></picture>Serves modern WebP images with fallbacks. Pair with optimized images to improve loading speed on slow mobile networks.
  • <input type="number" inputmode="numeric">Triggers a numeric keypad on mobile devices, perfect for phone numbers and payment verification input fields.

Box Model and CSS Properties

  • * { box-sizing: border-box; }Includes padding and border in element total dimensions. Place it at the top of your CSS to avoid sizing calculation bugs.
  • color: #008751;Sets text color with hex codes. Ensure text contrast meets Web Content Accessibility Guidelines for best legibility.
  • aspect-ratio: 16 / 9;Maintains fixed aspect ratio for media containers. Helps prevent cumulative layout shifts while images load.
  • object-fit: cover;Clips an image to fill its container without distortion. Requires explicit width and height values on the image element.

CSS Flexbox: Container

  • display: flex; flex-direction: column;Enables Flexbox and sets layout direction. In column mode, align-items controls horizontal positioning instead of vertical.
  • justify-content: space-between;Distributes items along the main axis. Note that space-between leaves no extra outer padding at container boundaries.
  • align-items: center;Aligns items along the cross axis. Default value stretch expands items to fill the full height of the container.
  • gap: 1.5rem;Sets consistent spacing between flex items. Replaces margin hacks and avoids manual removal of margins on outer items.

CSS Flexbox: Child Items

  • flex: 1 1 200px;Shorthand for flex-grow, flex-shrink, and flex-basis. Defines a initial width of 200px while allowing flex expansion.
  • align-self: flex-end;Overrides container align-items property for a single child. Great for pinning action buttons to the bottom of cards.
  • order: 2;Changes visual display order without altering HTML markup. Be careful as it can disrupt keyboard tab navigation order.

CSS Grid: Container

  • display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));Creates a responsive grid without media queries. Columns rewrap automatically based on available container width.
  • grid-template-rows: auto 1fr auto;Defines row heights. Ideal for creating sticky headers and footers while stretching main content dynamically.
  • grid-template-areas: "header header" "sidebar main";Defines named layout areas visually. Template area names must form exact rectangular shapes or the grid breaks.
  • gap: 1rem 2rem;Defines row gap (1rem) and column gap (2rem) cleanly without relying on complex outer element margins.

CSS Grid: Child Items

  • grid-column: 1 / -1;Spans a grid element across all available columns from the first line index to the last.
  • grid-area: header;Assigns the element to a named grid template area. Do not use quotation marks around area names in item properties.
  • place-self: center;Centers an item both horizontally and vertically inside its grid cell. Shorthand for align-self and justify-self.

Other resources

Need help with your website?

I build your custom website.

Showcase site, online shop, management tool: let’s talk about your project.

See the services