Getting Started with CSS

Learn the fundamentals of CSS, from basic syntax to CSS3 features, and start styling web pages.

🎯 Introduction

CSS (Cascading Style Sheets) is a core web technology that defines how pages look. Whether you are new to web development or looking to build on your existing skills, understanding CSS is essential.

This article introduces CSS concepts, syntax, inheritance, and ways to style web pages. You will learn to control colors, fonts, and layouts, then explore more advanced CSS3 features to expand your design skills.

We will start with the basics and work toward more advanced techniques step by step.


🧨 What is CSS?

CSS stands for Cascading Style Sheets. It is a language for describing the presentation of HTML (HyperText Markup Language) and XML (Extensible Markup Language) documents. Used alongside HTML, it controls layout, colors, fonts, and other aspects of a page's appearance.

Why use CSS?

  • Separate content from presentation: HTML defines a page's structure and content, while CSS controls its appearance. Keeping them separate makes both easier to maintain.
  • Improve page loading efficiency: Centralizing styles reduces duplicate code and can help pages load faster.
  • Improve the user experience: CSS provides a wide range of design features for creating more interactive and engaging pages.

✍️ Three Ways to Apply CSS

There are three main ways to apply CSS:

  • Inline styles: Write CSS directly in an HTML element's style attribute.
  • Internal stylesheets: Place CSS in a <style> element in the HTML document, usually inside <head>.
  • External stylesheets: Store styles in a separate .css file and load it with a <link> element.

Inline Styles

Inline styles go directly in an HTML element's style attribute. They are quick and convenient, but difficult to maintain and reuse.

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

Internal Stylesheets

An internal stylesheet defines styles in a <style> element, usually inside the HTML document's <head>.

<head>
    <style>
        p {
            color: red;
        }
    </style>
</head>
<body>
    <p>This is a red paragraph.</p>
</body>

External Stylesheets

An external stylesheet stores CSS in a separate .css file, referenced with a <link> element. This approach works particularly well for larger websites because it promotes reuse and reduces duplication.

<head>
    <link rel="stylesheet" href="styles.css" />
</head>
<body>
    <p>This is a red paragraph.</p>
</body>

🧱 Basic Syntax: Selectors and Properties

CSS syntax consists of selectors and properties. Selectors identify the HTML elements to style, and properties specify the styles to apply.

Selectors

Selectors are central to CSS rules: they identify the elements on a page that a rule applies to. Common selectors include:

  • Type selectors: Select all elements of a given type.
    p {
        color: red;
    }
    
  • Class selectors: Select all elements with a given class name, prefixed with ..
    .myClass {
        color: blue;
    }
    
  • ID selectors: Select an element with a given ID, prefixed with #.
    #myId {
        color: green;
    }
    

Properties

Properties specify individual aspects of an element's style. Common examples include color, font-size, and background-color.

p {
    color: red;
    font-size: 16px;
}

🧬 CSS Inheritance

Inheritance is an important CSS concept. Some property values pass automatically from parent elements to their children, so you do not need to repeat the same styles on every element.

Inheritance Example

For example, setting the font on body lets paragraphs and other text elements inherit that font.

body {
    font-family: Arial, sans-serif;
}

p {
    color: blue;
}

Here, paragraph text uses Arial and appears in blue.


🚀 What is CSS3?

CSS3 is the third major generation of CSS. It introduced many features for modern web design, enabling richer visual effects and animation. Key additions include:

  • Rounded corners: Use border-radius to round an element's corners.
    div {
        border-radius: 10px;
    }
    
  • Shadows: Add shadows to elements to give the page a sense of depth.
    p {
        box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
    }
    
  • Gradient backgrounds: Create gradients with background-image and linear-gradient.
    body {
        background-image: linear-gradient(to right, red, yellow);
    }
    
  • Animations and transitions: Add motion and smooth visual changes to a page.
    .box {
        transition: all 0.3s ease;
    }
    .box:hover {
        transform: scale(1.2);
    }
    

❓ Knowledge Check

Q1: What are inline styles, and what are their drawbacks?

Inline styles are CSS declarations written directly in an HTML element's style attribute. They are difficult to reuse and maintain.

Q2: Why use an external stylesheet?

External stylesheets separate CSS from HTML content, making styles easier to manage, maintain, and reuse while reducing duplicate code.

Q3: Which features did CSS3 introduce?

CSS3 introduced rounded corners, shadows, gradient backgrounds, animations, and transitions, helping create more dynamic, modern pages.


🧪 Exercises

Exercise 1: Apply Inline Styles

Create a paragraph on a web page and use an inline style to make its text red.

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

Copy the code into your editor and preview the result!

Exercise 2: Set a Font with an External Stylesheet

Create a .css file that sets all paragraphs to Arial at 16px, then reference the stylesheet in your HTML file.

/* styles.css */
p {
    font-family: Arial, sans-serif;
    font-size: 16px;
}
<head>
    <link rel="stylesheet" href="styles.css" />
</head>
<body>
    <p>This is a paragraph.</p>
</body>

Copy the code into your editor and preview the result!


🎉 You have learned basic CSS syntax, inheritance, and several common CSS3 features! Keep practicing; next, we will explore more advanced techniques.