CSS Tutorial

"This is JavaScript tutorial and this is for learning JavaScript"

By Harry

11/02/2025

CSS Tutorial

CSS Tutorial: A Comprehensive Guide

Welcome to this comprehensive CSS tutorial! This guide will help you master the art of styling websites using CSS (Cascading Style Sheets). Whether you're a beginner or looking to expand your knowledge, this tutorial covers everything from basic styles to advanced layout techniques and animations.

Introduction to CSS

CSS is the language used to style HTML content, enabling you to control the layout, colors, fonts, and overall visual presentation of a website.

Why Learn CSS?

Setting Up CSS

To use CSS, you can either include it directly in your HTML file (inline or internal) or as an external stylesheet. Here’s how to set up CSS:

Inline CSS

<p style="color: blue;">This is a blue paragraph.</p>

Internal CSS

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            color: blue;
        }
    </style>
</head>
<body>
    <p>This is a blue paragraph.</p>
</body>
</html>

External CSS

Create a separate CSS file (e.g., styles.css) and link it to your HTML file.

<link rel="stylesheet" href="styles.css">

In styles.css:

p {
    color: blue;
}

CSS Basics

Now, let’s dive into the basics of CSS, including selectors, properties, and how to apply styles to HTML elements.

Selectors

Selectors are used to target HTML elements and apply styles. Some common selectors include:

/* Element selector */
p {
    color: green;
}
 
/* Class selector */
.example {
    font-size: 20px;
}
 
/* ID selector */
#header {
    background-color: lightgray;
}

Colors and Backgrounds

CSS allows you to set text colors, background colors, and images.

/* Text color */
h1 {
    color: darkblue;
}
 
/* Background color */
body {
    background-color: lightyellow;
}
 
/* Background image */
div {
    background-image: url('background.jpg');
}

Fonts and Text Styling

Control the typography of your website using fonts, text alignment, and decoration properties.

h1 {
    font-family: Arial, sans-serif;
    font-size: 32px;
    text-align: center;
    text-decoration: underline;
}

Intermediate CSS

After mastering the basics, you can start exploring more intermediate CSS concepts such as box model, positioning, and layout techniques.

Box Model

The box model is fundamental to understanding how elements are sized and spaced in CSS. It consists of four components: content, padding, border, and margin.

div {
    width: 200px;
    padding: 10px;
    border: 1px solid black;
    margin: 20px;
}

Positioning

CSS provides several ways to position elements on a page:

div {
    position: relative;
    top: 10px;
    left: 20px;
}

Flexbox

Flexbox is a powerful layout model that allows you to create flexible and responsive layouts.

.container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
 
.item {
    flex: 1;
    margin: 10px;
}

Grid

CSS Grid Layout provides a two-dimensional layout system, making it easy to design complex web layouts.

.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 20px;
}
 
.item {
    background-color: lightblue;
    padding: 20px;
}

Advanced CSS

Now that you’re comfortable with intermediate concepts, it’s time to explore advanced topics like CSS animations, transitions, and responsive design.

CSS Animations

CSS animations allow you to animate transitions between different styles.

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}
 
div {
    animation: fadeIn 2s ease-in-out;
}

CSS Transitions

Transitions allow you to smoothly change property values over a specified duration.

button {
    background-color: blue;
    transition: background-color 0.5s ease;
}
 
button:hover {
    background-color: green;
}

Media Queries

Media queries enable responsive design by applying different styles based on the device's characteristics (e.g., screen width).

/* For screens wider than 600px */
@media (min-width: 600px) {
    body {
        background-color: lightgreen;
    }
}
 
/* For screens narrower than 600px */
@media (max-width: 600px) {
    body {
        background-color: lightpink;
    }
}

Conclusion

Congratulations on completing this CSS tutorial! You’ve learned the basics of CSS, intermediate layout techniques like Flexbox and Grid, and advanced topics like animations and responsive design. With CSS, you can create beautiful, responsive websites that look great on any device. Keep practicing and experimenting to further develop your CSS skills.

Happy styling!