The Complete CSS Selectors and Properties Cheat Sheet: A Developer’s Guide

Introduction to CSS Selectors and Properties

CSS (Cascading Style Sheets) is the language that styles web content. CSS selectors target HTML elements for styling, while properties define how those elements should appear. Mastering CSS selectors and properties is essential for precise control over webpage appearance and responsive design.

Core CSS Concepts

  • The CSS Rule: selector { property: value; }
  • Cascading: Styles can be inherited and overridden based on specificity and source order
  • Specificity: Determines which styles take precedence when multiple rules target the same element
  • Inheritance: Some properties are passed from parent to child elements
  • Box Model: All HTML elements are boxes with content, padding, border, and margin

CSS Selectors by Type

Basic Selectors

SelectorSyntaxDescriptionExample
Universal*Selects all elements* { margin: 0; }
Type/ElementelementSelects all elements of specified typep { color: blue; }
Class.classnameSelects elements with specified class.highlight { background: yellow; }
ID#idnameSelects element with specified ID#header { height: 80px; }
Attribute[attr]Selects elements with specified attribute[disabled] { opacity: 0.5; }

Combinators

SelectorSyntaxDescriptionExample
DescendantA BSelects all B that are inside Adiv p { line-height: 1.5; }
ChildA > BSelects all B that are direct children of Aul > li { list-style: square; }
Adjacent SiblingA + BSelects B immediately after Ah2 + p { font-weight: bold; }
General SiblingA ~ BSelects all B that follow Ah2 ~ p { margin-left: 20px; }

Pseudo-Classes

SelectorDescriptionExample
:hoverElement being hovereda:hover { text-decoration: underline; }
:activeElement being clickedbutton:active { transform: scale(0.98); }
:focusElement with focusinput:focus { border-color: blue; }
:first-childFirst child of parentli:first-child { font-weight: bold; }
:last-childLast child of parentli:last-child { border-bottom: none; }
:nth-child(n)Nth child of parenttr:nth-child(odd) { background: #f5f5f5; }
:not()Elements not matchinginput:not([type="submit"]) { border: 1px solid #ccc; }

Pseudo-Elements

SelectorDescriptionExample
::beforeCreates content before elementp::before { content: "→ "; }
::afterCreates content after elementa::after { content: " ↗"; }
::first-letterSelects first letterp::first-letter { font-size: 2em; }
::first-lineSelects first linep::first-line { font-style: italic; }
::selectionSelected text::selection { background: yellow; }

CSS Properties by Category

Text Styling

PropertyDescriptionExample Values
colorText color#333, rgb(50, 50, 50), blue
font-familyFont typefaceArial, sans-serif, "Times New Roman", serif
font-sizeText size16px, 1.2em, 1.5rem, 120%
font-weightText thicknessnormal, bold, 600
font-styleText stylenormal, italic, oblique
text-alignHorizontal alignmentleft, right, center, justify
text-decorationText decorationnone, underline, line-through
text-transformCapitalizationnone, uppercase, lowercase, capitalize
line-heightLine spacing1.5, 20px, 150%
letter-spacingCharacter spacingnormal, 2px, -0.5px

Box Model & Layout

PropertyDescriptionExample Values
widthElement width300px, 50%, auto
heightElement height100px, 50vh, auto
paddingInner spacing10px, 10px 20px, 10px 5px 15px 20px
marginOuter spacing10px, auto, 0 auto 20px
borderBorder around element1px solid black, 2px dashed red
border-radiusRounded corners5px, 50%
box-sizingHow width/height are calculatedcontent-box, border-box
displayElement display typeblock, inline, flex, grid, none
positionPositioning methodstatic, relative, absolute, fixed, sticky
z-indexStacking order1, 100, -1

Flexbox

PropertyDescriptionExample Values
displayEnable flexboxflex, inline-flex
flex-directionDirection of flex itemsrow, column, row-reverse, column-reverse
justify-contentAlign along main axisflex-start, center, space-between, space-around
align-itemsAlign along cross axisstretch, center, flex-start, flex-end
flex-wrapWrap itemsnowrap, wrap, wrap-reverse
flexShorthand for grow/shrink/basis1, 1 0 auto, 0 1 200px
gapSpace between flex items10px, 1rem, 10px 20px

Grid

PropertyDescriptionExample Values
displayEnable gridgrid, inline-grid
grid-template-columnsDefine columns100px 200px, 1fr 2fr, repeat(3, 1fr)
grid-template-rowsDefine rows50px auto, repeat(3, 100px)
grid-columnItem column placement1 / 3, span 2
grid-rowItem row placement2 / 4, span 3
grid-gapSpace between grid items10px, 10px 20px
grid-template-areasNamed grid areas"header header" "sidebar content"

Colors & Backgrounds

PropertyDescriptionExample Values
background-colorBackground color#fff, rgba(255, 255, 255, 0.8)
background-imageBackground imageurl('image.jpg'), linear-gradient(red, blue)
background-sizeBackground image sizecover, contain, 100% 100%
background-positionBackground image positioncenter, top left, 50% 25%
background-repeatBackground image repetitionrepeat, no-repeat, repeat-x
opacityElement transparency1, 0.5, 0

Transitions & Animations

PropertyDescriptionExample Values
transitionProperty transitionall 0.3s ease, color 1s ease-in-out
transformVisual transformationscale(1.5), rotate(45deg), translateX(10px)
animationNamed animationfadeIn 1s ease-in-out, bounce 2s infinite
@keyframesDefine animation steps@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

Selector Specificity

Selector TypeSpecificity ValueExample
Inline styles1,0,0,0<div style="color: red;">
ID0,1,0,0#header
Class, attribute, pseudo-class0,0,1,0.active, [type="text"], :hover
Element, pseudo-element0,0,0,1div, ::before

Common CSS Challenges and Solutions

Challenge: Centering Elements

/* Horizontal centering for block elements */
.center-block {
  margin: 0 auto;
  width: [specific-width]; /* must have width */
}

/* Centering with flexbox (horizontal and vertical) */
.center-flex {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Absolute positioning center */
.center-absolute {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Challenge: Creating Responsive Layouts

/* Mobile-first approach */
.container {
  width: 100%;
  padding: 15px;
}

/* Tablet and larger */
@media (min-width: 768px) {
  .container {
    width: 750px;
    margin: 0 auto;
  }
}

/* Desktop and larger */
@media (min-width: 1024px) {
  .container {
    width: 980px;
  }
}

Challenge: Handling Text Overflow

/* Truncate with ellipsis */
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Wrap long words */
.word-wrap {
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

CSS Best Practices

  • Use a CSS Reset or Normalize.css to ensure consistent styling across browsers
  • Follow a naming convention (BEM, SMACSS) for clearer, more maintainable code
  • Minimize specificity to avoid complex overrides
  • Group related properties for better readability
  • Use shorthand properties when possible (margin, padding, border, etc.)
  • Comment your code for complex selectors or non-obvious styling choices
  • Avoid !important except as a last resort
  • Utilize CSS variables (custom properties) for consistent theming
  • Consider mobile-first design for responsive layouts
  • Use relative units (em, rem, %, vh, vw) for responsive designs

Tools and Resources

CSS Frameworks and Libraries

  • Bootstrap: Full-featured UI toolkit
  • Tailwind CSS: Utility-first CSS framework
  • Bulma: Modern CSS framework based on Flexbox
  • Foundation: Responsive front-end framework

CSS Preprocessors

  • Sass/SCSS: Extends CSS with variables, nesting, and more
  • Less: Similar to Sass with a different syntax
  • PostCSS: Tool for transforming CSS with JavaScript plugins

CSS Testing and Validation

  • W3C CSS Validator: Check for CSS errors
  • Can I Use: Browser compatibility tables
  • CSS Lint: Identifies problematic patterns

Learning Resources

  • MDN Web Docs: Comprehensive CSS documentation
  • CSS-Tricks: Articles and guides on CSS techniques
  • Flexbox Froggy: Interactive game to learn Flexbox
  • Grid Garden: Interactive game to learn CSS Grid
  • Codecademy CSS Courses: Structured learning paths

Remember that CSS is continuously evolving, with new selectors and properties being added. Stay updated with the latest specifications and browser implementations to take advantage of new features and improvements.

Scroll to Top