CSS Cheat Sheet
A comprehensive guide to essential CSS properties and techniques for web development.
Basic Syntax
selector {
property: value;
another-property: another-value;
}
Preview: CSS follows the selector-property-value pattern
Selectors
/* Element selector */
p {
color: blue;
}
/* Class selector */
.my-class {
font-size: 16px;
}
/* ID selector */
#my-id {
background-color: yellow;
}
/* Attribute selector */
input[type="text"] {
border: 1px solid gray;
}
/* Pseudo-classes */
a:hover {
color: red;
}
/* Descendant selector */
div p {
margin: 10px;
}
/* Child selector */
ul > li {
list-style: none;
}
Element selector example
Class selector example
ID selector example
Text and Font Properties
.text-styles {
font-family: Arial, sans-serif;
font-size: 18px;
font-weight: bold;
font-style: italic;
text-align: center;
text-decoration: underline;
text-transform: uppercase;
line-height: 1.5;
letter-spacing: 2px;
color: #333;
}
Styled Text Example
Box Model
.box-model {
width: 200px;
height: 100px;
padding: 20px;
border: 2px solid black;
margin: 10px;
background-color: lightblue;
}
Box Model Example
Colors and Backgrounds
.color-examples {
/* Color formats */
color: red;
color: #ff0000;
color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.5);
color: hsl(0, 100%, 50%);
/* Background properties */
background-color: lightgreen;
background-image: url('image.jpg');
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
Red text on light green background
Gradient background
Layout - Flexbox
.flex-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
flex-wrap: wrap;
gap: 20px;
}
.flex-item {
flex: 1;
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
}
Item 1
Item 2
Item 3
Layout - Grid
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px 200px;
grid-gap: 10px;
grid-template-areas:
"header header header"
"sidebar main main";
}
.grid-item {
background-color: lightcoral;
padding: 20px;
}
Grid Item 1
Grid Item 2
Grid Item 3
Positioning
.positioning {
position: static; /* Default */
position: relative; /* Relative to normal position */
position: absolute; /* Relative to positioned parent */
position: fixed; /* Relative to viewport */
position: sticky; /* Sticky positioning */
top: 10px;
right: 20px;
bottom: 30px;
left: 40px;
z-index: 100;
}
Absolute positioned
Relative positioned
Borders and Shadows
.borders-shadows {
border: 2px solid black;
border-radius: 10px;
border-top: 3px dashed red;
border-right: 3px dotted blue;
box-shadow: 5px 5px 10px rgba(0,0,0,0.3);
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
Box with border, shadow, and text shadow
Transforms and Animations
.transform {
transform: rotate(45deg);
transform: scale(1.2);
transform: translate(50px, 100px);
transform: skew(20deg, 10deg);
}
.animation {
animation: slideIn 2s ease-in-out;
transition: all 0.3s ease;
}
@keyframes slideIn {
from { opacity: 0; transform: translateX(-100px); }
to { opacity: 1; transform: translateX(0); }
}
Hover me for transform effect
Media Queries (Responsive Design)
/* Mobile first approach */
.responsive {
width: 100%;
padding: 10px;
}
/* Tablet styles */
@media screen and (min-width: 768px) {
.responsive {
width: 50%;
padding: 20px;
}
}
/* Desktop styles */
@media screen and (min-width: 1024px) {
.responsive {
width: 33.33%;
padding: 30px;
}
}
Responsive element (resize window to see effect in real implementation)
Common CSS Utilities
/* Display utilities */
.show { display: block; }
.hide { display: none; }
.invisible { visibility: hidden; }
/* Text utilities */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }
/* Margin and padding utilities */
.m-0 { margin: 0; }
.p-2 { padding: 0.5rem; }
.mt-4 { margin-top: 1rem; }
/* Float utilities */
.float-left { float: left; }
.float-right { float: right; }
.clearfix::after {
content: "";
display: table;
clear: both;
}
Centered text
Left aligned
Right aligned
This CSS cheat sheet covers the most essential properties and techniques for web styling. Practice with these properties to master CSS!