Learn CSS Styling

Master CSS fundamentals to style web pages with colors, layouts, typography, and responsive design.

beginner0 upvotes0 viewsšŸ“„ 6 lessons
šŸ“„

CSS Basics & Selectors

beginneršŸ•’ 1 min readfrom MDN Web Docs

What is CSS?

CSS (Cascading Style Sheets) is a language used to style and layout web pages. It controls colors, fonts, spacing, positioning, and responsive behavior.

CSS Syntax

CSS rules consist of a selector and a declaration block:

šŸŽØCSS
selector {
  property: value;
  property: value;
}
4 linesCSS

Common Selectors

SelectorSyntaxExample
Elementelementp { }
Class.classname.highlight { }
ID#idname#header { }
Attribute[attr=value]input[type="text"] { }
Descendantparent childdiv p { }

Specificity

Specificity determines which CSS rule applies when conflicts occur. Higher specificity wins:

  1. 1Inline styles (1000 points)
  2. 2IDs (100 points)
  3. 3Classes, attributes, pseudo-classes (10 points)
  4. 4Elements (1 point)
šŸ’”

Use classes for reusable styles and IDs sparingly for unique elements.

šŸ“„

The Box Model & Layout

beginneršŸ•’ 1 min readfrom MDN Web Docs

Understanding the Box Model

Every HTML element is a box with four layers:

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”

│ Margin │

│ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │

│ │ Border │ │

│ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │

│ │ │ Padding │ │ │

│ │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │

│ │ │ │ Content │ │ │ │

│ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ │ │

│ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ │

│ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │

ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Box Model Properties

  • Content: The actual element (text, images)
  • Padding: Space inside the border
  • Border: Line around padding
  • Margin: Space outside the border
šŸŽØCSS
.box {
  width: 200px;
  height: 100px;
  padding: 20px;
  border: 2px solid black;
  margin: 15px;
}
7 linesCSS

Display Property

ValueBehavior
blockTakes full width, starts new line
inlineOnly takes needed width, flows with text
inline-blockInline but respects width/height
noneHidden from layout
āš ļø

Use `box-sizing: border-box` to include padding and border in width calculations.

šŸ“„

Colors, Fonts & Typography

beginneršŸ•’ 1 min readfrom MDN Web Docs

Color Properties

CSS supports multiple color formats:

šŸŽØCSS
.element {
  color: red;                    /* Named color */
  color: #FF0000;                /* Hex */
  color: rgb(255, 0, 0);         /* RGB */
  color: rgba(255, 0, 0, 0.5);   /* RGBA with transparency */
  background-color: lightblue;
}
7 linesCSS

Font Families

šŸŽØCSS
body {
  font-family: 'Arial', 'Helvetica', sans-serif;
  font-size: 16px;
  font-weight: bold;        /* 400-700 or normal/bold */
  font-style: italic;
  line-height: 1.6;         /* Improves readability */
  letter-spacing: 2px;
}
8 linesCSS

Text Styling

PropertyValues
text-alignleft, center, right, justify
text-decorationunderline, overline, line-through
text-transformuppercase, lowercase, capitalize
text-shadowoffset-x offset-y blur color
šŸ’”

Use web-safe fonts or Google Fonts for consistent rendering across browsers.

Font Sizing Best Practices

  • Use rem for scalable sizing (relative to root font-size)
  • Use em for relative sizing within components
  • Avoid px for responsive designs
šŸ“„

CSS Styling Cheatsheet & Mini-Project

beginneršŸ•’ 1 min readfrom MDN Web Docs

Quick Reference Guide

Selectors

šŸŽØCSS
* { }                    /* Universal */
element { }              /* Element */
.class { }               /* Class */
#id { }                  /* ID */
element.class { }        /* Element with class */
parent > child { }       /* Direct child */
ancestor descendant { }  /* Any descendant */
7 linesCSS

Common Properties

šŸŽØCSS
/* Colors & Backgrounds */
color: #333;
background-color: white;
background-image: url('image.jpg');

/* Sizing */
width: 100%;
height: auto;
max-width: 1200px;

/* Spacing */
margin: 20px;
padding: 15px;
gap: 10px;

/* Text */
font-size: 16px;
font-weight: bold;
text-align: center;
line-height: 1.6;

/* Layout */
display: flex;
justify-content: center;
align-items: center;
25 linesCSS

Mini-Project: Responsive Card Layout

šŸŽØCSS
.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
  padding: 20px;
}

.card {
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.card h3 {
  margin-top: 0;
  color: #333;
}

.card p {
  color: #666;
  line-height: 1.6;
}
30 linesCSS
šŸ’”

Use `auto-fit` with `minmax()` for truly responsive grids that adapt to any screen size.

šŸ“„

Flexbox & Grid Layouts

intermediatešŸ•’ 1 min readfrom MDN Web Docs

Flexbox Basics

Flexbox is a one-dimensional layout system perfect for aligning items in rows or columns.

šŸŽØCSS
.container {
  display: flex;
  justify-content: center;      /* Horizontal alignment */
  align-items: center;          /* Vertical alignment */
  gap: 20px;                    /* Space between items */
  flex-direction: row;          /* row or column */
}

.item {
  flex: 1;                      /* Equal growth */
  flex-basis: 200px;            /* Base size */
}
12 linesCSS

CSS Grid Fundamentals

Grid is a two-dimensional layout system for complex layouts.

šŸŽØCSS
.grid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;  /* 3 columns */
  grid-template-rows: auto 200px;      /* 2 rows */
  gap: 15px;
}

.item {
  grid-column: 1 / 3;           /* Span columns 1-2 */
}
10 linesCSS

Responsive Layouts

šŸŽØCSS
@media (max-width: 768px) {
  .container {
    flex-direction: column;
  }
  
  .grid {
    grid-template-columns: 1fr;  /* Single column on mobile */
  }
}
9 linesCSS
šŸ’”

Use Flexbox for one-dimensional layouts and Grid for complex two-dimensional designs.

šŸ“„

Positioning & Transforms

intermediatešŸ•’ 1 min readfrom MDN Web Docs

Position Property

The position property controls how elements are placed:

šŸŽØCSS
.element {
  position: static;      /* Default, normal flow */
  position: relative;    /* Relative to normal position */
  position: absolute;    /* Relative to positioned parent */
  position: fixed;       /* Relative to viewport */
  position: sticky;      /* Hybrid of relative and fixed */
  
  top: 20px;
  left: 30px;
  z-index: 10;           /* Stacking order */
}
11 linesCSS

Transforms & Animations

šŸŽØCSS
.box {
  transform: rotate(45deg);
  transform: scale(1.5);
  transform: translateX(50px);
  transform: skew(10deg);
  
  transition: all 0.3s ease;  /* Smooth animation */
}

.box:hover {
  transform: scale(1.2) rotate(5deg);
}

@keyframes slide {
  from { left: 0; }
  to { left: 100px; }
}

.animated {
  animation: slide 2s infinite;
}
21 linesCSS

Z-Index Stacking

Higher z-index values appear on top. Only works with positioned elements.

āš ļø

Avoid excessive z-index values; keep them organized (10, 20, 30, etc.).

**Key Insight**: Transforms don't affect document flow, making them ideal for animations and visual effects.

Top Interview Questions

Missing something?

Suggest a topic or concept to add to this tutorial. AI will review and expand the content.