Introduction
Cascading Style Sheets (CSS) is the styling language that brings websites to life visually. CSS properties are the individual instructions that control how HTML elements appear and behave on a webpage. Mastering these properties is essential for creating responsive, accessible, and visually appealing websites.
Core CSS Concepts
CSS Syntax
selector {
property: value;
}
Selectors
Selector | Example | Description |
---|
Element | p | Selects all paragraph elements |
Class | .classname | Selects elements with class=”classname” |
ID | #idname | Selects element with id=”idname” |
Universal | * | Selects all elements |
Attribute | [attribute] | Selects elements with specified attribute |
Pseudo-class | :hover | Selects elements in a specific state |
Pseudo-element | ::before | Creates and styles content before an element |
Specificity Hierarchy (lowest to highest)
- Element selectors
- Class selectors
- ID selectors
- Inline styles
- !important declarations
Layout Properties
Box Model
Property | Description | Example |
---|
width | Element width | width: 300px; |
height | Element height | height: 200px; |
padding | Space inside element | padding: 10px 20px; |
border | Border around element | border: 1px solid black; |
margin | Space outside element | margin: 10px auto; |
box-sizing | How width/height is calculated | box-sizing: border-box; |
Position
Value | Description |
---|
static | Default position (follows document flow) |
relative | Positioned relative to normal position |
absolute | Positioned relative to nearest positioned ancestor |
fixed | Positioned relative to viewport |
sticky | Positioned based on scroll position |
Display
Value | Description |
---|
block | Takes full width, creates line break |
inline | Takes only needed width, no line break |
inline-block | Inline but respects width/height |
flex | Creates flexible container |
grid | Creates grid container |
none | Removes element from document flow |
Flexbox Properties
For Parent (Container)
.container {
display: flex;
flex-direction: row | row-reverse | column | column-reverse;
flex-wrap: nowrap | wrap | wrap-reverse;
justify-content: flex-start | flex-end | center | space-between | space-around | space-evenly;
align-items: stretch | flex-start | flex-end | center | baseline;
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
gap: 10px;
}
For Children (Items)
.item {
order: 0;
flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
Grid Properties
For Parent (Container)
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
grid-template-areas: "header header header" "sidebar main main" "footer footer footer";
grid-gap: 10px;
justify-items: start | end | center | stretch;
align-items: start | end | center | stretch;
}
For Children (Items)
.item {
grid-column: 1 / 3;
grid-row: 1 / 2;
grid-area: header;
justify-self: start | end | center | stretch;
align-self: start | end | center | stretch;
}
Typography Properties
Property | Description | Example |
---|
font-family | Font type | font-family: Arial, sans-serif; |
font-size | Text size | font-size: 16px; |
font-weight | Text thickness | font-weight: bold; |
font-style | Text style | font-style: italic; |
line-height | Space between lines | line-height: 1.5; |
text-align | Horizontal alignment | text-align: center; |
text-decoration | Line additions | text-decoration: underline; |
text-transform | Text case | text-transform: uppercase; |
letter-spacing | Space between letters | letter-spacing: 2px; |
word-spacing | Space between words | word-spacing: 4px; |
white-space | How whitespace is handled | white-space: nowrap; |
text-overflow | How overflow is handled | text-overflow: ellipsis; |
Color and Background Properties
Property | Description | Example |
---|
color | Text color | color: #333; |
background-color | Element background color | background-color: #f0f0f0; |
background-image | Background image | background-image: url('image.jpg'); |
background-size | Background image size | background-size: cover; |
background-position | Background image position | background-position: center; |
background-repeat | Background image repetition | background-repeat: no-repeat; |
background-attachment | Background scrolling behavior | background-attachment: fixed; |
opacity | Element transparency | opacity: 0.8; |
Transition and Animation Properties
Transitions
.element {
transition-property: all;
transition-duration: 0.3s;
transition-timing-function: ease-in-out;
transition-delay: 0s;
/* Shorthand */
transition: all 0.3s ease-in-out 0s;
}
Animations
@keyframes slide-in {
0% { transform: translateX(-100%); }
100% { transform: translateX(0); }
}
.element {
animation-name: slide-in;
animation-duration: 1s;
animation-timing-function: ease-out;
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: forwards;
/* Shorthand */
animation: slide-in 1s ease-out 0s 1 normal forwards;
}
Transform Properties
.element {
transform: translate(10px, 20px) rotate(45deg) scale(1.5) skew(10deg, 20deg);
}
Common Responsive Design Properties
Media Queries
@media screen and (max-width: 768px) {
/* Rules for screens smaller than 768px */
}
Viewport Units
Unit | Description |
---|
vw | 1% of viewport width |
vh | 1% of viewport height |
vmin | 1% of smaller dimension |
vmax | 1% of larger dimension |
Responsive Images
img {
max-width: 100%;
height: auto;
}
Common Challenges and Solutions
Challenge: Centering Elements
/* Horizontal centering */
.parent { text-align: center; } /* For inline elements */
.child { margin: 0 auto; } /* For block elements with width */
/* Vertical centering */
.parent {
display: flex;
align-items: center;
}
/* Perfect centering */
.parent {
display: flex;
justify-content: center;
align-items: center;
}
/* Alternative perfect centering */
.parent { position: relative; }
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Challenge: Creating a Sticky Header
header {
position: sticky;
top: 0;
z-index: 100;
background-color: white;
}
Challenge: Maintaining Aspect Ratio
.container {
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 aspect ratio */
position: relative;
}
.content {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
Best Practices and Tips
- Use a CSS Reset or Normalize.css to ensure consistent styling across browsers
- Implement a naming convention (BEM, SMACSS, etc.) for maintainable CSS
- Use shorthand properties when possible to write cleaner code
- Avoid using !important except as a last resort
- Minimize specificity conflicts by using classes instead of IDs when possible
- Use CSS variables (custom properties) for consistent theming
:root { --primary-color: #3498db;}.button { background-color: var(--primary-color);}
- Group related properties for better readability
- Comment your code for complex sections
- Use browser dev tools to debug CSS issues
- Optimize for performance by avoiding expensive properties (box-shadow, text-shadow, gradients, etc.)
Resources for Further Learning
Documentation
Tools
Learning Platforms
Advanced CSS
Remember that CSS is constantly evolving, with new properties being added regularly. Keep your skills updated by following CSS news and experimenting with new features!