Beginner Friendly - CSS Tutorial

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
2. CSS Selectors
3. Colors
4. Text and Fonts
5. CSS Box Model
6. Display and Positioning
7. Flexbox
8. CSS Grid
9. Flexbox vs Grid
10. Responsive Design
11. Media Queries
12. Transitions
13. Transforms
14. CSS Animations
15. Common CSS Mistakes
16. Final Project
17. CSS Cheat Sheet

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;
}
Exercise: Change the paragraph color to green and increase the font size to 20px.

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;
}
Practice: Create a class called .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.

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.

5. CSS Box Model

Every HTML element can be considered as a box. The CSS box model consists of four main parts:

Content
The four parts
  1. Content
  2. Padding
  3. Border
  4. 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;
}
With 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.

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.

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;
}

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;
}
Flexbox Exercise: Create three cards in a row. Add a 20px gap, center them vertically, and make them wrap when the available width becomes smaller.

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.

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.
Simple rule: Use Flexbox when arranging items primarily along one dimension. Use Grid when you need control over rows and columns.

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;
    }

}

In this example, the layout uses a row on larger screens and changes to a column when the viewport width is 768px or smaller.

Practice: Create a two-column layout and change it to one column on screens smaller than 768px.

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;
}
Transitions are useful for hover effects and other smooth property changes.

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

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

The animation property controls the animation name, duration, timing, delay, repetition, and other animation behavior.

Exercise: Create an animation that changes the size of a box from small to large.

15. Common CSS Mistakes

Incorrect:
p {
    color: red
    font-size: 20px;
}
Correct:
p {
    color: red;
    font-size: 20px;
}

Make sure every CSS rule has opening and closing braces.

If the HTML uses class="card", the CSS class selector should be .card.

Check the file path and filename in the HTML link element.

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

  1. Header
  2. Profile
  3. About
  4. Skills
  5. Projects
  6. 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

20. Frequently Asked Questions

CSS stands for Cascading Style Sheets. It is used to control the appearance, spacing, and layout of HTML elements.

CSS is a stylesheet language used to describe the presentation of HTML documents.

You should understand basic HTML elements and the structure of an HTML document before learning CSS. Review the HTML tutorial if necessary.

The CSS box model describes an element using content, padding, border, and margin.

Both are important. Flexbox is useful for one-dimensional layouts, while Grid is designed for two-dimensional row and column layouts.

Media queries allow CSS rules to change according to conditions such as viewport width.

A useful next step is responsive web design. After that, you can continue with JavaScript and modern web development.