CSS (Cascading Style Sheets) is a stylesheet language that controls how HTML elements are presented. It separates content (HTML) from design (CSS), making websites easier to maintain, more flexible, and visually consistent. Core principles:
A CSS rule has two parts:
p {
color: blue;
font-size: 16px;
}
Common Selectors
Every element is a box made of:
- Content → the text or image.
- Padding → space between content and border.
- Border → line around padding and content.
- Margin → space outside the element.
* {
box-sizing: border-box;
}
This makes the declared width include padding and borders, simplifying layout.Flexbox provides a way to align and distribute space inside a container.
Main Concepts
display: flex;- Main axis → defined by flex-direction: row | column.
- Cross axis → perpendicular direction.
Key Properties
justify-content → alignment along main axis (flex-start, center, space-between).stretch, center).flex-grow, flex-shrink, flex-basis → control item sizing.Use cases
CSS Grid is a two-dimensional system for layouts with rows and columns.
Main Concepts
display: grid;.grid-template-columns: 1fr 1fr 1fr;grid-template-rows: auto 100px;
grid-column: 1 / 3;grid-row: 2 / 3;
Features
grid-gap for spacing.grid-template-areas for semantic layouts.Use cases
CSS controls the look and readability of text.
Properties
font-family: Arial, sans-serif;- Import fonts via Google Fonts or
@font-face.
font-size: 16px;(absolute) or1.2em,1rem(relative).
font-weight: bold;,font-style: italic;.
line-height: 1.5;letter-spacing: 0.1em;
text-align: center;,justify;.
Best Practices
CSS supports multiple ways to define colors:
#3498db)rgba(52, 152, 219, 0.8))hsl(207, 70%, 54%))Effects
box-shadow, text-shadow.linear-gradient, radial-gradient.opacity.Variables
:root {
--main-color: #3498db;
}
button {
background: var(--main-color);
}
background-image: url("image.jpg");background-size: cover | contain;background-position: center;
object-fit: cover;ensures responsive scaling.
Responsive design adapts websites to different screen sizes.
Media Queries
@media (max-width: 768px) {
body { font-size: 14px; }
}
Breakpoints (common)
Properties
transition: property duration timing-function;animation-name, animation-duration, animation-iteration-count.Pseudo-classes define special states of elements.
:hover→ when mouse hovers:focus→ when element is focused (keyboard/tab):active→ when clicked
:first-child,:last-child,:nth-child(2n)
::beforeand::after→ insert content before/after an element
block__element--modifier).@supports for feature detection.All the documentation in this page is taken from :