CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents. It allows developers to control the layout, colors, fonts, and overall look of a web page.

Before CSS, web pages were styled directly inside HTML using attributes like bgcolor
or font
. CSS separated design from structure, making websites easier to build, maintain, and scale.
The main idea of CSS is that you can apply a set of styles to multiple elements at once, which saves time and ensures consistency across a website.
p { color: blue; font-size: 16px; }
- CSS means "Cascading Style Sheets".
- It controls how HTML elements are displayed.
- It makes websites more attractive and easier to manage.
Selectors in CSS are patterns used to target and style HTML elements. They tell the browser which elements to apply the styles to.
Without selectors, CSS rules would not know where to apply. For example, you may want to style only all paragraphs, a single button, or all elements with a specific class.
Here are some of the most common selectors:
p { color: red; }
#main { background-color: lightblue; }
.highlight { font-weight: bold; }
- Element Selector: targets all elements of a type, e.g.,
p
. - ID Selector: targets a unique element with an
id
, e.g.,#main
. - Class Selector: targets all elements with a given class, e.g.,
.highlight
.
Colors in CSS allow you to style the text, background, and borders of elements. You can use different formats to define colors, depending on your preference and design needs.
Common color formats include color names, HEX codes, RGB values, and HSL values. Each format gives you flexibility when working with colors.
h1 { color: blue; }
p { color: #ff5733; }
div { background-color: rgb(0, 255, 0); }
section { background-color: hsl(200, 100%, 50%); }
- Color Names: e.g.,
red
,blue
,green
. - HEX: e.g.,
#ff5733
, used a lot in web design. - RGB: defines color using red, green, and blue values.
- HSL: stands for hue, saturation, and lightness.
Text styling in CSS allows you to control the appearance of text, including its size, color, alignment, and decoration. It makes your webpage more readable and visually appealing.
p {
font-size: 16px;
color: navy;
text-align: center;
text-decoration: underline;}
- font-size: Controls how big or small the text appears.
- color: Sets the text color.
- text-align: Defines text alignment (left, right, center, justify).
- text-decoration: Adds effects like underline, overline, or line-through.
The CSS box model is the foundation of web design layout. Every element on a webpage is treated as a rectangular box, which consists of four main parts.
Understanding the box model helps you control spacing, borders, and overall layout structure.
div {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;}
- Content: The text or images inside the element.
- Padding: Space between the content and the border.
- Border: A line that surrounds the padding and content.
- Margin: Space between the element and other elements outside it.
The position and display properties in CSS are used to control how elements are placed and shown on a webpage.
div {
position: absolute;
top: 50px;
left: 100px;
display: inline-block;
}
- position: Defines how an element is placed. Common values include:
- static (default, follows normal flow)
- relative (moves relative to its normal position)
- absolute (positions relative to the nearest positioned parent)
- fixed (sticks to the screen even when scrolling)
- sticky (switches between relative and fixed when scrolling)
- display: Defines how an element is shown:
- block (starts on a new line, full width)
- inline (flows in line, like text)
- inline-block (inline but allows setting width/height)
- flex (for flexible layouts)
- grid (for grid layouts)
CSS Flexbox and Grid are modern layout systems that make it easier to design complex, responsive web layouts.
.container {
display: flex;
justify-content: center;
align-items: center;
}
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
- Flexbox:
- One-dimensional layout (works in a row or column).
- Useful for aligning items and distributing space.
- Key properties:
justify-content
,align-items
,flex-direction
.
- Grid:
- Two-dimensional layout (rows and columns).
- Useful for building entire page structures.
- Key properties:
grid-template-columns
,grid-template-rows
,gap
.
Responsive design means making websites look good and usable on all devices — desktops, tablets, and phones. It prevents content from breaking or looking too small on different screen sizes.
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
- Responsive Design: Ensures layouts adjust automatically to fit screens of different sizes.
- Media Queries: CSS rules that apply only under certain conditions (like screen width).
- Fluid Layouts: Using percentages instead of fixed pixels for widths.
- Flexible Images: Setting images to
max-width: 100%
so they shrink on small screens.
CSS animations and transitions bring life to websites. They make changes smoother and can grab the user’s attention without JavaScript.
div {
transition: background-color 0.5s ease;
}
div:hover {
background-color: lightgreen;
}
@keyframes slideIn {
from { transform: translateX(-100%); }
to { transform: translateX(0); }
}
.box {
animation: slideIn 1s ease-in-out;
}
- Transitions: Smoothly change CSS properties (like color, size, or opacity).
- Animations: Use
@keyframes
to create complex, multi-step effects. - Hover Effects: A common use of transitions, triggered when the user hovers.
- Attention Grabbers: Animations can highlight important elements (like buttons).
CSS is the foundation of styling on the web. By mastering its basics and advanced features, you can create professional, responsive, and engaging websites. Remember to always keep your code clean, organized, and reusable.
- Start Small: Master simple selectors and properties before moving to complex layouts.
- Practice: Apply what you learn by building small projects regularly.
- Stay Updated: CSS evolves constantly, so keep learning new features.
Here are some recommended resources to continue learning CSS:
❤️