Bootstrap demo

CSS Commands Reference Guide

A comprehensive guide to CSS properties, selectors, and techniques with examples

Table of Contents

Introduction to CSS

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML and XML documents. It controls the visual appearance of web pages including layout, colors, fonts, and responsive behavior.

Basic CSS Structure

CSS
selector {
  property: value;
  /* This is a CSS comment */
}

CSS Syntax Fundamentals

CSS Comments

CSS
/* Single line comment */

/*
Multi-line
comment
*/

@ Rules

CSS
/* Import another CSS file */
@import url('styles.css');

/* Define character encoding */
@charset 'UTF-8';

/* Define custom fonts */
@font-face {
  font-family: 'MyFont';
  src: url('myfont.woff2') format('woff2');
}

/* Media queries for responsive design */
@media (max-width: 768px) {
  body { font-size: 14px; }
}

CSS Selectors

Basic Selectors

CSS
/* Element selector */
p { color: blue; }

/* Class selector */
.classname { font-weight: bold; }

/* ID selector */
#elementid { background: yellow; }

/* Universal selector */
* { margin: 0; padding: 0; }

Combinator Selectors

CSS
/* Descendant selector */
div p { line-height: 1.5; }

/* Child selector */
div > p { color: red; }

/* Adjacent sibling selector */
h1 + p { margin-top: 0; }

/* General sibling selector */
h1 ~ p { font-style: italic; }

Pseudo-classes

CSS
/* Link states */
a:link { color: blue; }
a:visited { color: purple; }
a:hover { text-decoration: underline; }
a:active { color: red; }

/* Form states */
input:focus { border-color: blue; }
input:disabled { opacity: 0.5; }
input:checked { background: green; }

/* Structural pseudo-classes */
li:first-child { font-weight: bold; }
li:last-child { border-bottom: none; }
li:nth-child(odd) { background: #f2f2f2; }
li:nth-child(3n) { color: green; }

Box Model Properties

Width and Height

CSS
.element {
  width: 300px;           /* Fixed width */
  min-width: 100px;       /* Minimum width */
  max-width: 500px;       /* Maximum width */
  height: 200px;          /* Fixed height */
  min-height: 50px;       /* Minimum height */
  max-height: 400px;      /* Maximum height */
  box-sizing: border-box; /* Include padding and border in dimensions */
}

Margin and Padding

CSS
.element {
  margin: 10px;            /* All sides */
  margin: 10px 20px;       /* Top/bottom, right/left */
  margin: 5px 10px 15px;   /* Top, right/left, bottom */
  margin: 5px 10px 15px 20px; /* Top, right, bottom, left */
  
  padding: 10px;           /* All sides */
  padding: 10px 20px;      /* Top/bottom, right/left */
  padding: 5px 10px 15px;  /* Top, right/left, bottom */
  padding: 5px 10px 15px 20px; /* Top, right, bottom, left */
}

Layout Properties

Flexbox

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

.item {
  flex: 1;                       /* flex-grow, flex-shrink, flex-basis */
  flex-grow: 0;                  /* How much item grows relative to others */
  flex-shrink: 1;                /* How much item shrinks relative to others */
  flex-basis: auto;              /* Default size before growing/shrinking */
  align-self: auto;              /* Override align-items for specific item */
  order: 0;                      /* Visual order (doesn't affect source order) */
}

CSS Grid

CSS
.container {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;  /* Track sizes */
  grid-template-rows: 100px auto 100px;
  grid-template-areas: 
    "header header header"
    "sidebar content ads"
    "footer footer footer";
  gap: 20px;                      /* Space between grid items */
  justify-items: stretch;         /* stretch, start, end, center */
  align-items: stretch;           /* stretch, start, end, center */
}

.item {
  grid-column: 1 / 3;            /* Start / end */
  grid-row: 2;                   /* Row position */
  grid-area: header;             /* Named grid area */
  justify-self: center;          /* Individual alignment */
  align-self: center;
}

Typography Properties

Font Properties

CSS
.text {
  font-family: Arial, Helvetica, sans-serif; /* Font stack */
  font-size: 16px;          /* Absolute size */
  font-size: 1rem;          /* Relative to root */
  font-size: 1.2em;         /* Relative to parent */
  font-weight: normal;      /* normal, bold, 100-900 */
  font-style: normal;       /* normal, italic, oblique */
  font-variant: normal;     /* normal, small-caps */
  line-height: 1.5;         /* Unitless multiplier or fixed value */
}

Text Properties

CSS
.text {
  color: #333;              /* Text color */
  text-align: left;         /* left, right, center, justify */
  text-decoration: none;    /* none, underline, overline, line-through */
  text-transform: none;     /* none, capitalize, uppercase, lowercase */
  text-indent: 20px;        /* First line indent */
  letter-spacing: 1px;      /* Space between letters */
  word-spacing: 2px;        /* Space between words */
  white-space: normal;      /* normal, nowrap, pre, pre-wrap, pre-line */
  word-wrap: break-word;    /* break-word, normal */
  text-shadow: 1px 1px 2px rgba(0,0,0,0.5); /* x-offset, y-offset, blur, color */
}

Color and Background Properties

Color Values

CSS
.element {
  color: red;                     /* Named color */
  color: #ff0000;                 /* Hex color */
  color: rgb(255, 0, 0);          /* RGB */
  color: rgba(255, 0, 0, 0.5);    /* RGB with alpha */
  color: hsl(0, 100%, 50%);       /* HSL */
  color: hsla(0, 100%, 50%, 0.5); /* HSL with alpha */
  color: currentColor;            /* Use current text color */
}

Background Properties

CSS
.element {
  background-color: #fff;
  background-image: url('image.jpg');
  background-repeat: no-repeat;   /* repeat, repeat-x, repeat-y, no-repeat */
  background-position: center;    /* x-position y-position */
  background-size: cover;         /* cover, contain, or specific dimensions */
  background-attachment: fixed;   /* scroll, fixed, local */
  
  /* Shorthand */
  background: #fff url('image.jpg') no-repeat center / cover fixed;
}

Positioning Properties

Position

CSS
.element {
  position: static;    /* Default, normal flow */
  position: relative;  /* Positioned relative to normal position */
  position: absolute;  /* Positioned relative to nearest positioned ancestor */
  position: fixed;     /* Positioned relative to viewport */
  position: sticky;    /* Toggles between relative and fixed based on scroll */
}

Offset Properties

CSS
.element {
  top: 10px;
  right: 20px;
  bottom: 30px;
  left: 40px;
  z-index: 10; /* Stacking context, higher numbers appear on top */
}

Animation and Transition Properties

Transition

CSS
.element {
  transition: all 0.3s ease-in-out; /* property duration timing-function delay */
  transition-property: opacity, transform;
  transition-duration: 0.3s;
  transition-timing-function: ease-in-out; /* ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier() */
  transition-delay: 0.1s;
}

Animation

CSS
.element {
  animation: pulse 2s infinite; /* name duration timing-function delay iteration-count direction fill-mode */
  animation-name: pulse;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-delay: 0s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

Responsive Design Properties

Media Queries

CSS
/* Default styles for mobile first */

/* Tablet */
@media (min-width: 768px) {
  .element { width: 750px; }
}

/* Desktop */
@media (min-width: 992px) {
  .element { width: 970px; }
}

/* Large desktop */
@media (min-width: 1200px) {
  .element { width: 1170px; }
}

/* Specific device orientation */
@media (orientation: landscape) {
  .element { height: 100vh; }
}

Viewport Units

CSS
.element {
  width: 100vw;     /* 100% of viewport width */
  height: 50vh;     /* 50% of viewport height */
  font-size: 4vmin; /* 4% of viewport's smaller dimension */
  padding: 2vmax;   /* 2% of viewport's larger dimension */
}

CSS Functions

Calculation

CSS
.element {
  width: calc(100% - 80px);
  height: calc(50vh + 10px);
}

Custom Properties (CSS Variables)

CSS
:root {
  --main-color: #3498db;
  --padding: 15px;
  --max-width: 1200px;
}

.element {
  color: var(--main-color);
  padding: var(--padding);
  max-width: var(--max-width);
  background-color: var(--fallback-color, #f2f2f2); /* With fallback */
}

CSS Examples

Centering Elements

CSS
/* Method 1: Flexbox */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

/* Method 2: Grid */
.container {
  display: grid;
  place-items: center;
  height: 100vh;
}

/* Method 3: Absolute positioning */
.container {
  position: relative;
  height: 100vh;
}

.centered-element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Button Styles

CSS
.btn {
  display: inline-block;
  padding: 12px 24px;
  background-color: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  font-weight: bold;
  transition: all 0.3s ease;
  border: none;
  cursor: pointer;
}

.btn:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}