DEV Community

Cover image for An Introduction to CSS: Customizing with index.css and Classes
Jessica Papa
Jessica Papa

Posted on

An Introduction to CSS: Customizing with index.css and Classes

Cascading Style Sheets (CSS) are crucial for controlling the visual appearance of your HTML content. With CSS, you can style your website, change layouts, and make it look attractive. In this guide, we'll cover the basics of CSS, including using an index.css component and customizing styles with classes.

CSS operates as a separate layer from your HTML content, allowing you to maintain a clear separation between structure and presentation. To begin, create an index.css file within your project directory. This file will serve as the main stylesheet for your website.

Linking CSS to HTML

To apply the styles defined in your index.css file to your HTML, you need to link the two together.

Insert the following line within the

section of your HTML file:
<link rel="stylesheet" href="index.css">
Enter fullscreen mode Exit fullscreen mode

This tells the browser to fetch and apply the styles from your index.css file.

Styling by Class

One of the fundamental concepts in CSS is the use of classes to target specific elements for styling.

Classes allow you to apply the same set of styles to multiple elements without duplicating code. Let's walk through an example.

Suppose you have the following HTML structure:

DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="index.css">
</head>
<body>
    <h1 class="header">Welcome to My Website</h1>
    <p class="content">This is a brief introduction to CSS.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In your index.css file, you can define styles for the .header and .content classes:

/* index.css */

.header {
    font-size: 24px;
    color: #333;
    text-align: center;
}

.content {
    font-size: 16px;
    color: #666;
    line-height: 1.5;
}
Enter fullscreen mode Exit fullscreen mode

In this example, the .header class applies larger text and a centered alignment, while the .content class provides a smaller font size, a different color, and adjusted line spacing.

Image description

CSS is a powerful tool that enhances the visual appeal of your website. By using an index.css file and styling elements through classes, you can maintain a clean separation of concerns in your web development projects. This introductory guide provides a glimpse into the world of CSS, but there's much more to explore as you dive deeper into the art of web styling. Start experimenting with your own styles and classes to create beautifully designed web pages.

Happy coding!

Top comments (0)