CSS Style Cheat Sheet
CSS (Cascading Style Sheets) is used to control the layout and styling of web pages. This cheat sheet covers the most commonly used properties and selectors.
Selectors
| Type | Example | Description |
|---|---|---|
| All | * { } |
Match all elements |
| Type | h1 { } |
Match specific HTML tag |
| Class | .header { } |
Match elements with class=“header” |
| ID | #main { } |
Match element with id=“main” |
| Descendant | div p { } |
Match all p inside div |
| Pseudo | a:hover { } |
State when hovered with mouse |
Box Model
.box {
width: 300px;
height: 200px;
padding: 10px; /* Padding */
border: 1px solid #ccc; /* Border */
margin: 20px; /* Margin */
box-sizing: border-box; /* Include border and padding */
}Typography
| Property | Description | Example |
|---|---|---|
font-family |
Font family | font-family: Arial, sans-serif; |
font-size |
Font size | font-size: 16px; |
font-weight |
Font weight | font-weight: bold; |
text-align |
Text alignment | text-align: center; |
line-height |
Line height | line-height: 1.5; |
color |
Font color | color: #333; |
Colors & Backgrounds
| Property | Description | Example |
|---|---|---|
background-color |
Background color | background-color: #f0f0f0; |
background-image |
Background image | background-image: url('bg.jpg'); |
background-size |
Background size | background-size: cover; |
opacity |
Opacity | opacity: 0.8; |
Positioning
position: static;: Default flow layoutposition: relative;: Offset relative to its own positionposition: absolute;: Position relative to nearest non-static ancestorposition: fixed;: Position relative to browser windowposition: sticky;: Fixed when scrolled to specific positionz-index: 10;: Control stack order
Display & Layout
display: block;: Block elementdisplay: inline;: Inline elementdisplay: inline-block;: Inline-block elementdisplay: flex;: Enable flex layoutdisplay: grid;: Enable grid layoutdisplay: none;: Hide element without taking space
Shorthand Properties
margin: 10px 5px 15px 20px;(Top Right Bottom Left)padding: 10px 5px;(Top-Bottom Left-Right)border: 1px dashed red;(Width Style Color)