CSS Fundamentals Course

Dive into the world of Cascading Style Sheets (CSS) and learn how to make your web pages beautiful and responsive. CSS is the language we use to style an HTML document.


1. What is CSS?

CSS stands for Cascading Style Sheets. It's a stylesheet language used to describe the presentation of a document written in HTML (or XML). This includes colors, layout, and fonts, making your web content visually appealing and organized. Without CSS, web pages would look like plain text documents.

HTML provides the structure, CSS provides the style!

📄+🎨+💻=🚀

2. Ways to Add CSS

There are three main ways to include CSS in your HTML documents:

2.1 Inline Styles

Applied directly to an HTML element using the `style` attribute. Useful for small, specific style changes, but generally not recommended for larger projects as it mixes content and presentation.

<p style="color: blue; font-size: 18px;">This text is styled inline.</p>

This text is styled inline.

2.2 Internal (Embedded) Styles

Placed within a `<style>` tag in the `<head>` section of your HTML document. Good for single-page styles or when you don't have external CSS files.

<!DOCTYPE html>
<html>
<head>
    <title>Internal CSS</title>
    <style>
        h1 {
            color: purple;
            text-align: center;
        }
    </style>
</head>
<body>
    <h1>Hello with Internal CSS!</h1>
</body>
</html>

Hello with Internal CSS!

2.3 External Stylesheets (Best Practice)

The most common and recommended method. CSS rules are written in a separate `.css` file and linked to the HTML document using the `<link>` tag in the `<head>` section. This keeps your HTML clean and allows styles to be reused across multiple pages.

`styles.css` content:

/* styles.css */
body {
    font-family: Arial, sans-serif;
    background-color: #f4f4f4;
}
.external-text {
    color: darkgreen;
    font-weight: bold;
}

`index.html` content:

<!DOCTYPE html>
<html>
<head>
    <title>External CSS</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <p class="external-text">This text is styled from an external stylesheet.</p>
</body>
</html>

This text is styled from an external stylesheet.


3. How to Run HTML/CSS Code Locally (Visual Studio Code)

Visual Studio Code (VS Code) is a popular, free, and open-source code editor that's excellent for web development. It offers many features and extensions to make coding easier. Here's how you can set it up to run your HTML and CSS code locally:

Step 1: Install Visual Studio Code

  • Go to the official VS Code website: code.visualstudio.com
  • Download and install the version appropriate for your operating system (Windows, macOS, Linux).
  • Follow the installation prompts.

Step 2: Install the Live Server Extension

The Live Server extension provides a local development server with live reload capability. This means your browser will automatically refresh every time you save changes to your HTML or CSS files.

  • Open VS Code.
  • Click on the **Extensions** icon in the Activity Bar on the side (it looks like four squares, one detached). Or press `Ctrl+Shift+X` (Windows/Linux) or `Cmd+Shift+X` (macOS).
  • In the search bar, type "Live Server".
  • Find the "Live Server" extension by Ritwick Dey and click the **Install** button.

Step 3: Create Your HTML and CSS Files

  • In VS Code, go to **File > Open Folder...** and create a new folder for your project (e.g., `my-css-project`).
  • Inside this folder, create two new files:
    • `index.html`
    • `styles.css`
  • Paste the following code into `index.html`:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My First VS Code Page</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <h1 class="main-heading">Welcome to VS Code Web Dev!</h1>
        <p class="styled-paragraph">This paragraph is styled using an external CSS file.</p>
        <button class="my-button">Click Me!</button>
    </body>
    </html>
  • Paste the following code into `styles.css`:
    body {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background-color: #f0f2f5;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 100vh;
        margin: 0;
        color: #333;
    }
    
    .main-heading {
        color: #2c3e50;
        font-size: 2.5em;
        margin-bottom: 20px;
        border-bottom: 3px solid #3498db;
        padding-bottom: 10px;
    }
    
    .styled-paragraph {
        background-color: #ecf0f1;
        padding: 15px 25px;
        border-radius: 8px;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
        font-size: 1.1em;
        text-align: center;
    }
    
    .my-button {
        background-color: #3498db;
        color: white;
        padding: 12px 25px;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        font-size: 1em;
        transition: background-color 0.3s ease;
    }
    
    .my-button:hover {
        background-color: #2980b9;
    }

Step 4: Open with Live Server

  • With `index.html` open in the editor, **right-click anywhere in the editor window**.
  • Select "Open with Live Server" from the context menu.
  • Alternatively, click the "Go Live" button in the bottom-right corner of the VS Code status bar.
  • Your default web browser will open, displaying your `index.html` file with the applied CSS styles.

Now, whenever you save changes to `index.html` or `styles.css` in VS Code, Live Server will automatically refresh your browser, showing your updates instantly!


4. CSS Selectors

Selectors are used to "find" (or select) the HTML elements you want to style.

4.1 Element Selector

Selects HTML elements based on their tag name.

/* CSS */
p {
    color: #333;
}

<!-- HTML -->
<p>This paragraph will be dark gray.</p>

This paragraph will be dark gray.

4.2 Class Selector

Selects HTML elements with a specific `class` attribute. Classes can be used on multiple elements.

/* CSS */
.highlight {
    background-color: yellow;
    font-weight: bold;
}

<!-- HTML -->
<p class="highlight">This text is highlighted.</p>
<span class="highlight">So is this.</span>

This text is highlighted.


So is this.

4.3 ID Selector

Selects the HTML element with a specific `id` attribute. An ID must be unique within a page.

/* CSS */
#main-title {
    color: red;
    text-decoration: underline;
}

<!-- HTML -->
<h1 id="main-title">Important Title</h1>

Important Title

4.4 Universal Selector

Selects all HTML elements on the page.

/* CSS */
* {
    margin: 0;
    padding: 0;
}

<!-- HTML -->
<p>No margin or padding here.</p>
<div>Neither here.</div>

No margin or padding here.

Neither here.

4.5 Group Selector

Selects multiple elements to apply the same style.

/* CSS */
h1, h2, p {
    font-family: sans-serif;
}

<!-- HTML -->
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>A paragraph.</p>

Heading 1

Heading 2

A paragraph.

4.6 Descendant Selector

Selects an element that is a descendant of another specified element.

/* CSS */
div p {
    border: 1px dashed gray;
    padding: 5px;
}

<!-- HTML -->
<div>
    <p>This paragraph is inside a div.</p>
</div>
<p>This paragraph is NOT inside a div.</p>

This paragraph is inside a div.

This paragraph is NOT inside a div.


5. Properties & Values

A CSS rule consists of a selector and a declaration block. The declaration block contains one or more declarations, each consisting of a CSS property name and a value, separated by a colon.

/* Selector */
h2 {
    /* Property: Value; */
    color: #007bff; /* Sets text color to blue */
    font-size: 24px; /* Sets font size to 24 pixels */
    margin-bottom: 10px; /* Adds space below the element */
}

Example Heading

This heading has custom color, size, and margin.


6. The CSS Box Model

Every HTML element is treated as a rectangular box. The CSS Box Model describes how these boxes are laid out and how their dimensions are calculated. It consists of:

  • Content: The actual content (text, image).
  • Padding: Space between content and border.
  • Border: A line around the padding and content.
  • Margin: Space outside the border, separating elements.

The `box-sizing` property controls how `width` and `height` are calculated.

Content-Box (Default)

Content

`width` and `height` apply to content only. Padding/border are added *outside*.
Total width: 150px (content) + 8px (padding) + 8px (border) = 166px

Border-Box

Content

`width` and `height` include padding/border. Padding/border are taken *from inside*.
Total width: 150px

/* CSS */
.box-content-example {
    box-sizing: content-box; /* Default */
    width: 150px;
    height: 70px;
    padding: 4px; /* 4px on all sides */
    border: 4px solid blue;
}

.box-border-example {
    box-sizing: border-box;
    width: 150px;
    height: 70px;
    padding: 4px;
    border: 4px solid green;
}

7. Colors & Backgrounds

CSS allows you to control the colors of text, backgrounds, and other elements.

7.1 Text Color (`color`)

/* CSS */
.red-text {
    color: red;
}
.hex-color {
    color: #FF5733; /* Hexadecimal color code */
}
.rgb-color {
    color: rgb(0, 128, 0); /* RGB color code */
}

<!-- HTML -->
<p class="red-text">This text is red.</p>
<p class="hex-color">This text is orange-red.</p>
<p class="rgb-color">This text is green.</p>

This text is red.

This text is orange-red.

This text is green.

7.2 Background Color (`background-color`)

/* CSS */
.blue-bg {
    background-color: lightblue;
    padding: 10px;
    border-radius: 5px;
}

<!-- HTML -->
<div class="blue-bg">This div has a light blue background.</div>
This div has a light blue background.

7.3 Background Image (`background-image`)

You can set an image as the background of an element.

/* CSS */
.hero-section {
    background-image: url('https://placehold.co/600x150/87CEEB/FFFFFF?text=Background+Image');
    background-size: cover; /* Covers the entire element */
    background-position: center; /* Centers the image */
    color: white;
    padding: 40px;
    text-align: center;
    border-radius: 8px;
}

<!-- HTML -->
<div class="hero-section">
    <h2>Welcome to Our Site!</h2>
    <p>Enjoy the beautiful background.</p>
</div>

Welcome to Our Site!

Enjoy the beautiful background.


8. Text & Fonts

CSS offers extensive control over typography.

8.1 Font Family (`font-family`)

Specifies the font for an element. Always provide fallback fonts.

/* CSS */
.serif-font {
    font-family: "Times New Roman", Times, serif;
}
.sans-serif-font {
    font-family: Arial, Helvetica, sans-serif;
}

<!-- HTML -->
<p class="serif-font">This text uses a serif font.</p>
<p class="sans-serif-font">This text uses a sans-serif font.</p>

This text uses a serif font.

This text uses a sans-serif font.

8.2 Font Size (`font-size`)

Sets the size of the text. Can use pixels (`px`), ems (`em`), rems (`rem`), percentages (`%`), etc.

/* CSS */
.small-text {
    font-size: 14px;
}
.large-text {
    font-size: 2em; /* 2 times the parent element's font-size */
}

<!-- HTML -->
<p class="small-text">Small text.</p>
<p class="large-text">Large text.</p>

Small text.

Large text.

8.3 Font Weight (`font-weight`)

Specifies the thickness of the characters.

/* CSS */
.bold-text {
    font-weight: bold;
}
.light-text {
    font-weight: 300; /* Numeric values from 100 to 900 */
}

<!-- HTML -->
<p class="bold-text">This text is bold.</p>
<p class="light-text">This text is light.</p>

This text is bold.

This text is light.

8.4 Text Alignment (`text-align`)

Aligns the text within an element.

/* CSS */
.center-text {
    text-align: center;
}
.right-text {
    text-align: right;
}

<!-- HTML -->
<p class="center-text">Centered text.</p>
<p class="right-text">Right-aligned text.</p>

Centered text.

Right-aligned text.

8.5 Line Height (`line-height`)

Sets the height of each line of text.

/* CSS */
.tight-lines {
    line-height: 1.2;
}
.loose-lines {
    line-height: 2;
}

<!-- HTML -->
<p class="tight-lines">This paragraph has tight line spacing. It's good for compact text blocks.</p>
<p class="loose-lines">This paragraph has loose line spacing. It's often used for readability in longer texts.</p>

This paragraph has tight line spacing. It's good for compact text blocks.

This paragraph has loose line spacing. It's often used for readability in longer texts.


9. Layout Basics: Flexbox

Flexbox (Flexible Box Layout Module) is a one-dimensional layout method for arranging items in a single row or column. It's incredibly powerful for distributing space and aligning content.

9.1 `display: flex`

To use Flexbox, you set `display: flex` on the parent container. Its direct children become flex items.

/* CSS */
.flex-container {
    display: flex;
    border: 2px dashed #6366f1; /* Indigo 500 */
    padding: 10px;
    background-color: #e0e7ff; /* Indigo 100 */
}
.flex-item {
    background-color: #818cf8; /* Indigo 400 */
    color: white;
    padding: 15px;
    margin: 5px;
    border-radius: 5px;
}

<!-- HTML -->
<div class="flex-container">
    <div class="flex-item">Item 1</div>
    <div class="flex-item">Item 2</div>
    <div class="flex-item">Item 3</div>
</div>
Item 1
Item 2
Item 3

9.2 `justify-content`

Aligns flex items along the main axis (default is horizontal). Common values: `flex-start`, `flex-end`, `center`, `space-between`, `space-around`.

/* CSS */
.justify-center {
    display: flex;
    justify-content: center;
    border: 2px dashed #a855f7; /* Purple 500 */
    padding: 10px;
    background-color: #f3e8ff; /* Purple 100 */
}
.justify-space-between {
    display: flex;
    justify-content: space-between;
    border: 2px dashed #a855f7;
    padding: 10px;
    background-color: #f3e8ff;
    margin-top: 10px;
}
.flex-item-small {
    background-color: #c084fc; /* Purple 400 */
    color: white;
    padding: 10px;
    margin: 5px;
    border-radius: 5px;
}

<!-- HTML -->
<div class="justify-center">
    <div class="flex-item-small">Center 1</div>
    <div class="flex-item-small">Center 2</div>
</div>
<div class="justify-space-between">
    <div class="flex-item-small">Between 1</div>
    <div class="flex-item-small">Between 2</div>
    <div class="flex-item-small">Between 3</div>
</div>
Center 1
Center 2
Between 1
Between 2
Between 3

9.3 `align-items`

Aligns flex items along the cross axis (default is vertical). Common values: `flex-start`, `flex-end`, `center`, `stretch`.

/* CSS */
.align-center {
    display: flex;
    align-items: center; /* Vertically centers items */
    height: 100px; /* Give container a height to see effect */
    border: 2px dashed #14b8a6; /* Teal 500 */
    padding: 10px;
    background-color: #ccfbf1; /* Teal 100 */
}
.flex-item-variable {
    background-color: #2dd4bf; /* Teal 400 */
    color: white;
    padding: 10px;
    margin: 5px;
    border-radius: 5px;
}
.flex-item-variable:nth-child(2) {
    padding: 20px 10px; /* Taller item */
}

<!-- HTML -->
<div class="align-center">
    <div class="flex-item-variable">Short</div>
    <div class="flex-item-variable">Tall Item</div>
    <div class="flex-item-variable">Medium</div>
</div>
Short
Tall Item
Medium

10. Responsive Design: Media Queries

Responsive web design is about making web pages look good on all devices (desktops, tablets, and phones). Media queries are a CSS technique that allows you to apply different styles based on device characteristics, most commonly screen width.

/* CSS */
.responsive-box {
    background-color: lightblue;
    padding: 20px;
    text-align: center;
    font-size: 18px;
    border: 2px solid blue;
    border-radius: 8px;
    transition: background-color 0.3s ease; /* Smooth transition */
}

/* Styles for screens smaller than 768px (e.g., mobile) */
@media (max-width: 767px) {
    .responsive-box {
        background-color: lightcoral;
        font-size: 14px;
        border-color: red;
    }
}

<!-- HTML -->
<div class="responsive-box">
    Resize your browser window to see me change!
    <p class="text-xs mt-2">(I'm red on small screens, blue on larger ones)</p>
</div>
Resize your browser window to see me change!

(I'm red on small screens, blue on larger ones)

To see the media query effect, try resizing your browser window or viewing this on a mobile device. On smaller screens, the box will turn red and the font size will decrease.


Congratulations!

You've completed the CSS Fundamentals Course! You now have a solid understanding of how to style your web pages using CSS, from basic properties to layout with Flexbox and adapting to different screen sizes with Media Queries.

Keep practicing and experimenting to master these concepts!