CSS Cheat Sheet


Universal Selector (*)

Selects all elements on the page.

* {

   margin: 0; 
padding: 0;
}

Element Selector (p, div, h1)

Targets all elements of a specific type.

p {

   color: blue;
}
div {
   border: 1px solid black;
}

Class Selector (.class)

Targets elements with a specific class.

.highlight {

   background-color: yellow;
}

ID Selector (#id)

Targets a unique element with a specific ID.

#header {

   font-size: 24px;
}

Descendant Selector (div p)

Targets elements nested inside another element.

div p {

   color: green;
}

Child Selector (div > p)

Targets direct child elements only.

div > p {

   font-weight: bold;
}

Background

Controls background color, image, size, etc.

body {

   background-color: lightblue; 
background-image: url('img.jpg');
background-size: cover;
}

Text Styling

Controls color, font-size, font-weight, alignment, etc.

p {

   color: red; 
font-size: 16px;
font-weight: bold;
text-align: center;
}

Box Model

Includes width, height, padding, border, and margin.

div {

   width: 200px; 
height: 100px;
padding: 10px;
border: 2px solid black;
margin: 20px;
}

Display

Defines layout behavior (block, inline, flex, grid, none).

span {

   display: inline;
}
div {
   display: block;
}
.container {
   display: flex;
}

Position

Controls element positioning (relative, absolute, fixed, sticky).

#box {

   position: absolute; 
top: 50px;
left: 100px;
}

Flexbox

One-dimensional layout system for alignment and spacing.

.flex-container {

   display: flex; 
justify-content: space-between;
align-items: center;
}

Grid

Two-dimensional layout system for rows and columns.

.grid-container {

   display: grid; 
grid-template-columns: 1fr 2fr;
gap: 10px;
}

Hover (:hover)

Applies styles when the mouse hovers over an element.

button:hover {

   background-color: green; 
color: white;
}

Media Query

Applies styles based on screen size for responsive design.

@media (max-width: 600px) {

   body { 
background-color: lightgray;
}
}