Getting Started with CSS: An Overview of Syntax and Linking CSS to HTML

Enes Akkal
3 min readDec 10, 2023

--

Cascading Style Sheets (CSS) is the language used to present an HTML document in a readable and aesthetically pleasing format. CSS controls the visual aspect of a web page, allowing you to set the layout, colors, fonts, and much more. In this article, we will explore the basics of CSS syntax and how to connect CSS to your HTML to bring your web pages to life.

Understanding CSS Syntax

CSS is composed of a series of rules, each targeting an HTML element to which you want to apply styles. A CSS rule consists of a selector and a declaration block:

selector {
property: value;
}
  • Selector: This targets the HTML element that you want to style.
  • Property: This is the aspect of the element you want to change (e.g., color, width, font-size).
  • Value: This specifies the desired effect to apply to the property (e.g., red, 100px, 16pt).

Here’s a simple example of a CSS rule:

p {
color: blue;
font-size: 14px;
}

This rule will apply to all <p> (paragraph) elements in the HTML document, setting the text color to blue and the font size to 14 pixels.

Types of CSS

There are three main ways to apply CSS to HTML:

  1. Inline CSS: Styles are applied directly within an HTML tag using the style attribute.
<p style="color: blue; font-size: 14px;">This is a blue paragraph.</p>

2. Internal CSS: Styles are placed within a <style> element in the <head> section of the HTML document.

<head>
<style>
p {
color: blue;
font-size: 14px;
}
</style>
</head>

3. External CSS: Styles are written in a separate file with a .css extension and linked to the HTML document using a <link> element in the <head> section.

<!-- Inside the HTML head section -->
<head>
<link rel="stylesheet" href="styles.css">
</head>

Linking CSS to HTML

Using external CSS is a best practice because it separates content from style, making your code cleaner and more maintainable. Here’s how to link an external CSS file to an HTML document:

  1. Write your CSS in a separate file and save it with a .css extension (e.g., styles.css).
  2. In the HTML file, link to the CSS file within the <head> section using the <link> tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Page Title</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>This paragraph will be styled according to the rules defined in styles.css.</p>
</body>
</html>

Replace styles.css with the path to your CSS file. The href attribute specifies the path to the CSS file, which can be a relative or absolute path.

Conclusion

CSS is a powerful tool for web designers and developers, allowing for the customization of web pages in an almost limitless number of ways. By mastering CSS syntax and learning to link CSS to HTML documents, you can control the appearance of web pages, making them responsive and appealing to users. Remember, practice is key to becoming proficient in CSS, so start experimenting with your own styles and see how they affect your HTML.

--

--

Enes Akkal

Frontend Developer & Software Engineering Master Student At Boğaziçi University