Complete CSS Properties Cheat Sheet: Essential Guide for Web Developers

Introduction

Cascading Style Sheets (CSS) is the styling language that brings websites to life visually. CSS properties are the individual instructions that control how HTML elements appear and behave on a webpage. Mastering these properties is essential for creating responsive, accessible, and visually appealing websites.

Core CSS Concepts

CSS Syntax

selector {
  property: value;
}

Selectors

SelectorExampleDescription
ElementpSelects all paragraph elements
Class.classnameSelects elements with class=”classname”
ID#idnameSelects element with id=”idname”
Universal*Selects all elements
Attribute[attribute]Selects elements with specified attribute
Pseudo-class:hoverSelects elements in a specific state
Pseudo-element::beforeCreates and styles content before an element

Specificity Hierarchy (lowest to highest)

  1. Element selectors
  2. Class selectors
  3. ID selectors
  4. Inline styles
  5. !important declarations

Layout Properties

Box Model

PropertyDescriptionExample
widthElement widthwidth: 300px;
heightElement heightheight: 200px;
paddingSpace inside elementpadding: 10px 20px;
borderBorder around elementborder: 1px solid black;
marginSpace outside elementmargin: 10px auto;
box-sizingHow width/height is calculatedbox-sizing: border-box;

Position

ValueDescription
staticDefault position (follows document flow)
relativePositioned relative to normal position
absolutePositioned relative to nearest positioned ancestor
fixedPositioned relative to viewport
stickyPositioned based on scroll position

Display

ValueDescription
blockTakes full width, creates line break
inlineTakes only needed width, no line break
inline-blockInline but respects width/height
flexCreates flexible container
gridCreates grid container
noneRemoves element from document flow

Flexbox Properties

For Parent (Container)

.container {
  display: flex;
  flex-direction: row | row-reverse | column | column-reverse;
  flex-wrap: nowrap | wrap | wrap-reverse;
  justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
  align-items: stretch | flex-start | flex-end | center | baseline;
  align-content: flex-start | flex-end | center | space-between | space-around | stretch;
  gap: 10px;
}

For Children (Items)

.item {
  order: 0;
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

Grid Properties

For Parent (Container)

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto;
  grid-template-areas: "header header header" "sidebar main main" "footer footer footer";
  grid-gap: 10px;
  justify-items: start | end | center | stretch;
  align-items: start | end | center | stretch;
}

For Children (Items)

.item {
  grid-column: 1 / 3;
  grid-row: 1 / 2;
  grid-area: header;
  justify-self: start | end | center | stretch;
  align-self: start | end | center | stretch;
}

Typography Properties

PropertyDescriptionExample
font-familyFont typefont-family: Arial, sans-serif;
font-sizeText sizefont-size: 16px;
font-weightText thicknessfont-weight: bold;
font-styleText stylefont-style: italic;
line-heightSpace between linesline-height: 1.5;
text-alignHorizontal alignmenttext-align: center;
text-decorationLine additionstext-decoration: underline;
text-transformText casetext-transform: uppercase;
letter-spacingSpace between lettersletter-spacing: 2px;
word-spacingSpace between wordsword-spacing: 4px;
white-spaceHow whitespace is handledwhite-space: nowrap;
text-overflowHow overflow is handledtext-overflow: ellipsis;

Color and Background Properties

PropertyDescriptionExample
colorText colorcolor: #333;
background-colorElement background colorbackground-color: #f0f0f0;
background-imageBackground imagebackground-image: url('image.jpg');
background-sizeBackground image sizebackground-size: cover;
background-positionBackground image positionbackground-position: center;
background-repeatBackground image repetitionbackground-repeat: no-repeat;
background-attachmentBackground scrolling behaviorbackground-attachment: fixed;
opacityElement transparencyopacity: 0.8;

Transition and Animation Properties

Transitions

.element {
  transition-property: all;
  transition-duration: 0.3s;
  transition-timing-function: ease-in-out;
  transition-delay: 0s;
  
  /* Shorthand */
  transition: all 0.3s ease-in-out 0s;
}

Animations

@keyframes slide-in {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(0); }
}

.element {
  animation-name: slide-in;
  animation-duration: 1s;
  animation-timing-function: ease-out;
  animation-delay: 0s;
  animation-iteration-count: 1;
  animation-direction: normal;
  animation-fill-mode: forwards;
  
  /* Shorthand */
  animation: slide-in 1s ease-out 0s 1 normal forwards;
}

Transform Properties

.element {
  transform: translate(10px, 20px) rotate(45deg) scale(1.5) skew(10deg, 20deg);
}

Common Responsive Design Properties

Media Queries

@media screen and (max-width: 768px) {
  /* Rules for screens smaller than 768px */
}

Viewport Units

UnitDescription
vw1% of viewport width
vh1% of viewport height
vmin1% of smaller dimension
vmax1% of larger dimension

Responsive Images

img {
  max-width: 100%;
  height: auto;
}

Common Challenges and Solutions

Challenge: Centering Elements

/* Horizontal centering */
.parent { text-align: center; } /* For inline elements */
.child { margin: 0 auto; } /* For block elements with width */

/* Vertical centering */
.parent { 
  display: flex;
  align-items: center;
}

/* Perfect centering */
.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Alternative perfect centering */
.parent { position: relative; }
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Challenge: Creating a Sticky Header

header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: white;
}

Challenge: Maintaining Aspect Ratio

.container {
  width: 100%;
  height: 0;
  padding-bottom: 56.25%; /* 16:9 aspect ratio */
  position: relative;
}
.content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

Best Practices and Tips

  1. Use a CSS Reset or Normalize.css to ensure consistent styling across browsers
  2. Implement a naming convention (BEM, SMACSS, etc.) for maintainable CSS
  3. Use shorthand properties when possible to write cleaner code
  4. Avoid using !important except as a last resort
  5. Minimize specificity conflicts by using classes instead of IDs when possible
  6. Use CSS variables (custom properties) for consistent theming
    :root {  --primary-color: #3498db;}.button {  background-color: var(--primary-color);}
    
  7. Group related properties for better readability
  8. Comment your code for complex sections
  9. Use browser dev tools to debug CSS issues
  10. Optimize for performance by avoiding expensive properties (box-shadow, text-shadow, gradients, etc.)

Resources for Further Learning

  1. Documentation

  2. Tools

  3. Learning Platforms

  4. Advanced CSS

Remember that CSS is constantly evolving, with new properties being added regularly. Keep your skills updated by following CSS news and experimenting with new features!

Scroll to Top