CSS

This CSS cheatsheet provides a complete

This CSS cheatsheet covers everything from basics to advanced topics for web designers and developers.

CSS Basics

Syntax

selector { property: value; }

Example

body { background-color: #f0f000; font-family: Arial, sans-serif; }

Selectors

Universal Selector

* { margin: 0; padding: 0; }

Type Selector

p { color: black; }

Class Selector

.container { width: 80%; }

ID Selector

#header { background-color: #333; }

Attribute Selector

input[type="text"] { border: 1px solid #ccc; }

Pseudo-classes

a:hover { color: red; } li:first-child { font-weight: bold; }

Pseudo-elements

p::first-letter { font-size: 2em; color: red; }

Colors and Backgrounds

Color

color: #ff0000; /* red */ color: rgb(255, 0, 0); color: rgba(255, 0, 0, 0.5); color: hsl(0, 100%, 50%);

Background

body { background-color: #f0f0f0; background-image: url("bg.jpg"); background-repeat: no-repeat; background-size: cover; background-position: center; }

Text and Typography

h1 { font-family: "Arial", sans-serif; font-size: 32px; font-weight: bold; line-height: 1.5; letter-spacing: 1px; text-align: center; text-transform: uppercase; color: #333; }

Box Model

Properties

div { width: 200px; height: 100px; padding: 10px; border: 2px solid #000; margin: 20px; box-sizing: border-box; /* includes padding and border in width/height */ }

Display

display: block; display: inline; display: inline-block; display: flex; display: grid; display: none;

Positioning

Position Property

position: static; /* default */ position: relative; /* relative to its normal position */ position: absolute; /* relative to nearest positioned ancestor */ position: fixed; /* relative to viewport */ position: sticky; /* sticks within parent container */

Top, Bottom, Left, Right

div { position: absolute; top: 10px; left: 20px; }

Flexbox

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

Flex Items

.item { flex: 1; /* grow/shrink factor */ order: 2; /* item order */ align-self: flex-start; /* override align-items */ }

Grid

.container { display: grid; grid-template-columns: 1fr 2fr 1fr; grid-template-rows: 100px auto 50px; grid-gap: 10px; } .item { grid-column: 1 / 3; /* start / end */ grid-row: 2 / 3; }

Pseudo-classes & Pseudo-elements

Hover

a:hover { color: red; text-decoration: underline; }

First Child

li:first-child { font-weight: bold; }

Before & After

p::before { content: "Note: "; color: red; }

Transitions & Animations

Transitions

button { background-color: blue; transition: background-color 0.5s ease; } button:hover { background-color: green; }

Animations

@keyframes slide { from { transform: translateX(0); } to { transform: translateX(100px); } } div { animation: slide 2s infinite alternate; }

Responsive Design

Media Queries

@media (max-width: 768px) { body { background-color: lightblue; } }

Relative Units

Advanced CSS

Variables

:root { --primary-color: #3498db; } p { color: var(--primary-color); }

Box Shadow

div { box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5); }

Gradients

background: linear-gradient(to right, red, yellow); background: radial-gradient(circle, red, yellow);

Overflow

overflow: visible; overflow: hidden; overflow: scroll; overflow: auto;

Z-index

div { position: relative; z-index: 10; }

Cursor

cursor: pointer; cursor: default; cursor: crosshair;

Opacity

div { opacity: 0.5; /* 0 to 1 */ }

Units in CSS

CSS Functions

Important Tips