HTML/CSS Cheat Sheet

HTML Basics

Document Structure

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Page Title</title> </head> <body> <!-- Content goes here --> </body> </html>

Basic HTML5 document structure:

  • DOCTYPE declares HTML5 document
  • html element is the root element
  • head contains metadata
  • body contains visible content

Text Elements

<h1>Main Heading</h1> <p>Paragraph text</p> <span>Inline text</span> <div>Block container</div>
  • h1-h6: Headings (decreasing importance)
  • p: Paragraph block element
  • span: Inline text container
  • div: Block-level container

Lists

  • <ul> - Unordered list
  • <ol> - Ordered list
  • <li> - List item

CSS Basics

Selectors

.class-name { property: value; } #id-name { property: value; } element { property: value; } element.class { property: value; } parent > child { property: value; }
  • . - Class selector
  • # - ID selector
  • element - Tag selector
  • > - Direct child selector
  • space - Descendant selector

Box Model

.box { margin: 10px; border: 1px solid black; padding: 15px; width: 200px; height: 100px; }

Box model from outside to inside:

  • margin: Outside spacing
  • border: Element border
  • padding: Inside spacing
  • content: Element content area

Colors

  • color: text color
  • background-color
  • border-color

Intermediate Topics

Flexbox

  • display: flex
  • justify-content
  • align-items
  • flex-direction

Grid

  • display: grid
  • grid-template-columns
  • grid-gap

Positioning

  • position: relative
  • position: absolute
  • z-index

Advanced Topics

Animations

  • @keyframes
  • animation
  • transition

Media Queries

  • @media screen
  • min-width/max-width
  • orientation

CSS Variables

  • :root { --variable }
  • var(--variable)

HTML Intermediate

Forms Advanced

<form action="/submit" method="POST"> <input type="text" pattern="[A-Za-z]{3,}" required> <input type="email" placeholder="Enter email"> <input type="number" min="0" max="100"> <input type="date" min="2024-01-01"> <select multiple> <option value="1">Option 1</option> </select> <textarea rows="4" cols="50"></textarea> </form>

Advanced form features:

  • pattern: Regex validation
  • required: Makes field mandatory
  • min/max: Value constraints
  • multiple: Multiple selections

Semantic Elements

<header>Site header</header> <nav>Navigation menu</nav> <main> <article> <section>Article section</section> </article> <aside>Sidebar content</aside> </main> <footer>Site footer</footer>
  • header: Introductory content
  • nav: Navigation links
  • main: Main content
  • article: Independent content
  • section: Thematic grouping
  • aside: Related content
  • footer: Footer content

Media Elements

<video controls width="400"> <source src="video.mp4" type="video/mp4"> <track kind="subtitles" src="captions.vtt"> </video> <audio controls> <source src="audio.mp3" type="audio/mpeg"> </audio> <picture> <source media="(min-width: 800px)" srcset="large.jpg"> <source media="(min-width: 400px)" srcset="medium.jpg"> <img src="small.jpg" alt="Responsive image"> </picture>
  • video: Video player with controls
  • audio: Audio player
  • picture: Responsive images
  • source: Multiple media sources
  • track: Subtitles/captions

CSS Advanced

Flexbox Layout

.container { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 20px; } .item { flex: 1 1 300px; /* grow shrink basis */ }
  • justify-content: Horizontal alignment
  • align-items: Vertical alignment
  • flex-wrap: Wrapping behavior
  • gap: Space between items
  • flex: Growth and shrink factors

Grid Layout

.grid-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); grid-gap: 20px; grid-auto-rows: minmax(100px, auto); } .grid-item { grid-column: span 2; grid-row: span 2; }
  • grid-template-columns: Column layout
  • grid-gap: Grid spacing
  • grid-auto-rows: Automatic row sizing
  • grid-column/row: Item spanning

Animations

@keyframes slide-in { 0% { transform: translateX(-100%); opacity: 0; } 100% { transform: translateX(0); opacity: 1; } } .animated { animation: slide-in 0.5s ease-out; transition: all 0.3s ease; } .animated:hover { transform: scale(1.1); }
  • @keyframes: Define animation sequence
  • animation: Apply animation
  • transform: Visual transformations
  • transition: Smooth state changes

CSS Variables

:root { --primary-color: #3498db; --spacing-unit: 8px; --max-width: 1200px; } .element { color: var(--primary-color); padding: calc(var(--spacing-unit) * 2); max-width: var(--max-width); }
  • :root: Global scope
  • --variable-name: Custom property
  • var(): Use variable
  • calc(): Calculate values

Media Queries

/* Mobile First */ @media screen and (min-width: 768px) { /* Tablet styles */ } @media screen and (min-width: 1024px) { /* Desktop styles */ } /* Print styles */ @media print { .no-print { display: none; } }
  • min-width: Minimum screen width
  • max-width: Maximum screen width
  • orientation: Device orientation
  • print: Print-specific styles

CSS Tricks & Best Practices

Modern CSS Reset

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; } html { font-size: 16px; -webkit-text-size-adjust: 100%; } body { line-height: 1.5; -webkit-font-smoothing: antialiased; } img, picture, video, canvas, svg { display: block; max-width: 100%; }
  • box-sizing: Better box model
  • text-size-adjust: Prevent mobile text scaling
  • font-smoothing: Better text rendering
  • max-width: Responsive media

CSS Transforms & Effects

3D Transforms

.element { transform: perspective(1000px) rotateY(45deg); transform-style: preserve-3d; backface-visibility: hidden; } .card-flip { transform: rotateX(180deg); transition: transform 0.6s; }
  • perspective: Depth of 3D space
  • rotateY/X/Z: 3D rotation
  • preserve-3d: Maintains 3D space
  • backface-visibility: Hidden back side

Advanced Filters

.image-effects { filter: brightness(1.2) contrast(1.1) saturate(1.2); } .blur-effect { backdrop-filter: blur(10px); background: rgba(255, 255, 255, 0.1); } .gradient-text { background: linear-gradient(45deg, #ff6b6b, #4ecdc4); -webkit-background-clip: text; color: transparent; }
  • filter: Image adjustments
  • backdrop-filter: Background effects
  • background-clip: Text effects

CSS Layout Patterns

Holy Grail Layout

.holy-grail { display: grid; grid-template: "header header header" auto "nav main aside" 1fr "footer footer footer" auto / auto 1fr auto; min-height: 100vh; } header { grid-area: header; } nav { grid-area: nav; } main { grid-area: main; } aside { grid-area: aside; } footer { grid-area: footer; }
  • grid-template: Combined grid properties
  • grid-area: Named grid areas
  • 1fr: Fractional unit
  • min-height: Full viewport height

Responsive Card Grid

.card-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); gap: clamp(1rem, 3vw, 2rem); padding: max(2vw, 1rem); } .card { aspect-ratio: 3/4; object-fit: cover; }
  • auto-fit: Automatic columns
  • minmax: Responsive sizing
  • clamp: Fluid spacing
  • aspect-ratio: Maintained proportions

Advanced Animations

Scroll-Triggered Animations

.scroll-reveal { opacity: 0; transform: translateY(20px); transition: all 0.6s ease-out; } .scroll-reveal.visible { opacity: 1; transform: translateY(0); } /* Modern approach */ @keyframes fade-up { from { opacity: 0; transform: translateY(20px); } } .animate-on-scroll { view-timeline: --reveal-timeline block; animation: fade-up linear both; animation-timeline: --reveal-timeline; animation-range: entry 20% cover 50%; }
  • view-timeline: Scroll-based timeline
  • animation-timeline: Custom timeline
  • animation-range: Trigger points

CSS State Machines

@property --progress { syntax: ''; initial-value: 0; inherits: false; } .progress-circle { --progress: 0; background: conic-gradient( from 0deg, hsl(calc(var(--progress) * 120), 70%, 50%) calc(var(--progress) * 360deg), #eee 0 ); transition: --progress 0.5s; } .progress-circle:hover { --progress: 1; }
  • @property: Custom properties
  • conic-gradient: Circular progress
  • calc(): Dynamic calculations
  • transition: Animated properties

Accessibility & SEO

ARIA Roles & States

<button role="tab" aria-selected="true" aria-controls="panel-1" >Tab 1</button> <div role="tabpanel" id="panel-1" aria-labelledby="tab-1" hidden >Content</div> <div role="alert" aria-live="polite"> Status update </div>
  • role: Element purpose
  • aria-*: Accessibility states
  • aria-live: Dynamic updates
  • hidden: Visual/semantic hiding

Semantic SEO

<article itemscope itemtype="http://schema.org/BlogPosting"> <h1 itemprop="headline">Title</h1> <meta itemprop="datePublished" content="2024-01-20"> <div itemprop="articleBody"> Content </div> </article> <meta name="description" content="Page description"> <link rel="canonical" href="https://example.com/page">
  • itemscope/itemtype: Schema.org markup
  • itemprop: Content properties
  • meta description: SEO description
  • canonical: Preferred URL

Divs & Advanced Styling

Div Containers & Classes

<div class="container wrapper dark-theme"> <div class="card shadow rounded"> <div class="card-header">Header</div> <div class="card-body">Content</div> </div> </div> /* Multiple classes */ .card.shadow.rounded { /* Styles applied when all classes present */ } /* Descendant selectors */ .card .card-header { /* Styles for nested elements */ }
  • Multiple classes: Combine styles
  • Nesting: Parent-child relationships
  • Class combinations: Specific targeting
  • Component structure: Organized layout

Advanced Styling Techniques

.advanced-container { /* Glass effect */ background: rgba(255, 255, 255, 0.1); backdrop-filter: blur(10px); border: 1px solid rgba(255, 255, 255, 0.2); /* Modern shadow */ box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06); /* Gradient border */ border-image: linear-gradient(45deg, #ff6b6b, #4ecdc4) 1; }
  • Glass morphism: Modern transparent effect
  • Layered shadows: Depth and elevation
  • Gradient borders: Decorative edges
  • Backdrop filter: Background effects

Class Naming Conventions

/* BEM Methodology */ .block {} .block__element {} .block--modifier {} /* Utility Classes */ .d-flex { display: flex; } .justify-between { justify-content: space-between; } .p-4 { padding: 1rem; } /* State Classes */ .is-active {} .has-error {} .--disabled {}
  • BEM: Block Element Modifier pattern
  • Utility: Single-purpose classes
  • State: Conditional styling
  • Naming: Clear, consistent conventions