Learn CSS
Written by Admin Updated 15 min read
Before You Start
This tutorial assumes that you already know the basic structure of an HTML webpage.
If you are new to HTML, first read: How to Create a Basic HTML Website
Introduction to CSS
CSS stands for Cascading Style Sheets. It is used to control the appearance and layout of HTML elements.
HTML provides the structure of a webpage, while CSS controls how that structure looks and how elements are arranged.
HTML
Structure
CSS
Presentation and layout
JavaScript
Behaviour and interaction
Topics Covered
1. CSS Syntax
A CSS rule consists of a selector followed by one or more declarations.
selector {
property: value;
}
| Part | Meaning |
|---|---|
| Selector | Selects the HTML element. |
| Property | Specifies what you want to change. |
| Value | Specifies the value of the property. |
p {
color: blue;
font-size: 18px;
}
2. CSS Selectors
Selectors determine which HTML elements a CSS rule will apply to.
Element Selector
p {
color: blue;
}
Class Selector
.important {
font-weight: bold;
}
ID Selector
#header {
background-color: black;
}
Group Selector
h1, h2, h3 {
font-family: Arial, sans-serif;
}
.highlight
and use it to change the text color of two
paragraphs.
3. CSS Colors
CSS provides several ways to specify colors.
Named Colors
p {
color: red;
background-color: yellow;
}
Hexadecimal Colors
p {
color: #333333;
background-color: #f5f5f5;
}
RGB Colors
p {
color: rgb(0, 0, 255);
}
Background Color
.card {
background-color: #f8f9fa;
}
Exercise:
Create a heading with a dark background and
a light text color.
p {
color: red;
background-color: yellow;
}p {
color: #333333;
background-color: #f5f5f5;
}p {
color: rgb(0, 0, 255);
}.card {
background-color: #f8f9fa;
}4. Text and Fonts
Typography controls how text appears on a webpage.
Font Family
body {
font-family: Arial, sans-serif;
}
Font Size
h1 {
font-size: 36px;
}
Font Weight
strong {
font-weight: 700;
}
Font Style
em {
font-style: italic;
}
Line Height
p {
line-height: 1.6;
}
Text Alignment
h1 {
text-align: center;
}
Text Decoration
a {
text-decoration: none;
}
Text Transform
Exercise:
Create a heading that is centered, uppercase,
and uses a font size of 32px.
body {
font-family: Arial, sans-serif;
}h1 {
font-size: 36px;
}strong {
font-weight: 700;
}em {
font-style: italic;
}p {
line-height: 1.6;
}h1 {
text-align: center;
}a {
text-decoration: none;
}5. CSS Box Model
Every HTML element can be considered as a box. The CSS box model consists of four main parts:
The four parts
- Content
- Padding
- Border
- Margin
Content
The actual text or content.
Padding
Space between content and border.
Border
Surrounds the padding and content.
Margin
Space outside the element.
Practical Box Model Example
.card {
width: 300px;
padding: 20px;
border: 1px solid #cccccc;
margin: 20px;
}
box-sizing
The box-sizing property determines
how an element's width and height are calculated.
* {
box-sizing: border-box;
}
border-box, the declared width
includes the content, padding, and border.
Margin, Padding, and Borders
Directional Properties
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
Shorthand
.box {
margin: 10px 20px;
padding: 10px 20px;
}
Practice:
Create a card with 20px padding, a 1px border,
and 30px margin.
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;
}
.box {
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}.box {
margin: 10px 20px;
padding: 10px 20px;
}6. Display and Positioning
Display Property
Value
Description
block
Starts on a new line and takes available width.
inline
Appears within the current line.
inline-block
Combines inline placement with box dimensions.
none
Removes the element from the layout.
Position Property
Value
Purpose
static
Default positioning.
relative
Positions relative to its normal location.
absolute
Positions relative to a containing positioned element.
fixed
Remains fixed relative to the viewport.
sticky
Combines normal positioning with sticky behavior.
.box {
position: relative;
top: 10px;
left: 20px;
}
Positioning becomes much easier to understand
when you practice with small layouts rather than
memorizing the property names.
| Value | Description |
|---|---|
block |
Starts on a new line and takes available width. |
inline |
Appears within the current line. |
inline-block |
Combines inline placement with box dimensions. |
none |
Removes the element from the layout. |
| Value | Purpose |
|---|---|
| static | Default positioning. |
| relative | Positions relative to its normal location. |
| absolute | Positions relative to a containing positioned element. |
| fixed | Remains fixed relative to the viewport. |
| sticky | Combines normal positioning with sticky behavior. |
.box {
position: relative;
top: 10px;
left: 20px;
}7. CSS Flexbox
Flexbox is a CSS layout system designed primarily for arranging elements in one dimension.
Basic Flex Container
.container {
display: flex;
}
Flex Direction
.container {
display: flex;
flex-direction: row;
}
.container {
display: flex;
}.container {
display: flex;
flex-direction: row;
}
You can also use column,
row-reverse, and
column-reverse.
Justify Content
.container {
display: flex;
justify-content: space-between;
}
Align Items
.container {
display: flex;
align-items: center;
}
Flex Wrap
.container {
display: flex;
flex-wrap: wrap;
}
Gap
.container {
display: flex;
gap: 20px;
}
8. CSS Grid
CSS Grid is a two-dimensional layout system that works with rows and columns.
Two Columns
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
Three Columns
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
Four Columns
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
}
Rows and Columns
.grid {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto;
gap: 20px;
}
Grid Exercise:
Create a three-column project gallery using
repeat(3, 1fr) and gap.
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
}.grid {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto;
gap: 20px;
}repeat(3, 1fr) and gap.
9. Flexbox vs Grid
| Flexbox | Grid |
|---|---|
| Primarily one-dimensional. | Two-dimensional. |
| Works along a row or column. | Works with rows and columns. |
| Useful for navigation and alignment. | Useful for page and card layouts. |
10. Responsive Design
Responsive web design allows a webpage to adapt to different screen sizes such as desktops, tablets, and smartphones.
Viewport
A responsive page normally includes the viewport meta tag in its HTML document.
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
Relative Units
| Unit | Common Use |
|---|---|
| % | Relative to a containing element. |
| em | Relative to font size. |
| rem | Relative to the root font size. |
| vw | Relative to viewport width. |
| vh | Relative to viewport height. |
Responsive Images
img {
max-width: 100%;
height: auto;
}
11. CSS Media Queries
Media queries allow CSS rules to change based on characteristics such as viewport width.
Practical Responsive Example
.container {
display: flex;
flex-direction: row;
gap: 20px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}
.container {
display: flex;
flex-direction: row;
gap: 20px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
}In this example, the layout uses a row on larger screens and changes to a column when the viewport width is 768px or smaller.
12. CSS Transitions
Transitions create smooth changes between different property values.
.button {
background-color: blue;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: darkblue;
}
13. CSS Transforms
Transforms can change the position, size, rotation, or shape of an element.
Scale
.card:hover {
transform: scale(1.05);
}
Other Transform Functions
-
translate() - moves an element
-
scale() - changes its size
-
rotate() - rotates an element
-
skew() - skews an element
.card:hover {
transform: scale(1.05);
}translate() - moves an element
scale() - changes its size
rotate() - rotates an element
skew() - skews an element
14. CSS Animations
CSS animations allow an element to change from one style to another over time.
@keyframes
@keyframes moveBox {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.box {
animation: moveBox 2s ease-in-out;
}
Animation Property
@keyframes moveBox {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.box {
animation: moveBox 2s ease-in-out;
}
The animation property controls
the animation name, duration, timing, delay,
repetition, and other animation behavior.
15. Common CSS Mistakes
p {
color: red
font-size: 20px;
}
Correct:
p {
color: red;
font-size: 20px;
}
class="card", the CSS
class selector should be
.card.
For a detailed explanation of connecting an external CSS file, see Article 2: HTML and CSS Tutorial .
16. Final Project: Responsive Personal Profile Page
Now combine the CSS concepts you have learned to create a responsive personal profile page.
Project Sections
- Header
- Profile
- About
- Skills
- Projects
- Contact
Stage 1: Create the HTML Structure
<header>
<nav>
<a href="#">Home</a>
<a href="#about">About</a>
<a href="#skills">Skills</a>
<a href="#projects">Projects</a>
<a href="#contact">Contact</a>
</nav>
</header>
<main>
<section>
<h1>Your Name</h1>
<p>Web Developer</p>
</section>
<section id="about">
<h2>About Me</h2>
</section>
<section id="skills">
<h2>Skills</h2>
</section>
<section id="projects">
<h2>Projects</h2>
</section>
<section id="contact">
<h2>Contact</h2>
</section>
</main>
Stage 2: Add Colors and Typography
body {
font-family: Arial, sans-serif;
color: #333333;
}
header {
background-color: #222222;
}
h1 {
font-size: 40px;
}
section {
padding: 40px 20px;
}
Stage 3: Build the Navigation with Flexbox
nav {
display: flex;
justify-content: center;
align-items: center;
gap: 20px;
}
Stage 4: Build the Skills or Projects with Grid
.projects {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
Stage 5: Make the Page Responsive
@media (max-width: 768px) {
nav {
flex-direction: column;
}
.projects {
grid-template-columns: 1fr;
}
}
Stage 6: Add a Hover Effect
.project {
transition: transform 0.3s ease;
}
.project:hover {
transform: scale(1.03);
}
Project Goal
Build a complete responsive personal profile page using HTML and native CSS. Use Flexbox for navigation, Grid for project cards, media queries for mobile layouts, and a transition for interaction.
17. CSS Cheat Sheet
| CSS Property / Concept | Purpose |
|---|---|
color |
Text color |
background-color |
Element background |
font-family |
Font family |
font-size |
Text size |
font-weight |
Text thickness |
margin |
Outer spacing |
padding |
Inner spacing |
border |
Element border |
display |
Layout/display behavior |
display: flex |
Flexbox layout |
display: grid |
Grid layout |
position |
Positioning |
transition |
Smooth property changes |
transform |
Move, scale, rotate, or skew elements |
animation |
CSS animation |
18. What You Have Learned
After completing this CSS tutorial, you should understand the following concepts:
- CSS syntax and selectors
- Colors and backgrounds
- Typography and text formatting
- The CSS box model
- Margin, padding, and borders
- Display and positioning
- Flexbox
- CSS Grid
- Responsive design
- Media queries
- Transitions and transforms
- CSS animations
19. What's Next?
You now have a foundation in CSS and can create structured, styled, and responsive webpages.
The next step is to learn responsive web design in greater depth and understand how websites adapt to different devices and screen sizes.
Continue to Responsive Web Design