CSS controls the presentation of HTML. Learn the rule format, selectors, declarations, colours and typography, then connect a separate stylesheet to an HTML document.
A CSS rule contains a selector and a declaration block. The selector identifies the elements to style. Each declaration contains a property and a value, separated by a colon and ending with a semicolon.
For example, p { color: navy; } selects every paragraph and changes its text colour. CSS can control colour, font, size, spacing, borders, layout and many other visual properties.
An element selector such as h1 targets every matching heading. A class selector begins with a dot, such as .card, and can be reused on several elements. An ID selector begins with # and should identify one unique element in a page.
Prefer classes for reusable styling. Avoid using IDs for ordinary design rules because they are harder to override due to their higher specificity.
Keep larger stylesheets in a separate file. Add <link rel="stylesheet" href="style.css"> inside the document head. The browser loads that file and applies its rules to the HTML page.
CSS follows the cascade: when multiple declarations target the same property, origin, importance, specificity and source order decide which declaration wins. Start with simple selectors and avoid fighting the cascade with repeated overrides.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Practice</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 class="page-title">Code Campus</h1>
<p class="intro">Learning HTML and CSS step by step.</p>
</body>
</html>
body {
font-family: Arial, sans-serif;
background-color: #f3f4f6;
color: #20243a;
}
.page-title {
color: #5b55d9;
}
.intro {
font-size: 18px;
}
Create a page with a heading and paragraph. Link style.css, change the page background, set a font family and give the heading a different colour.
/* style.css */
body { background-color: #f3f4f6; font-family: Arial, sans-serif; }
h1 { color: #5b55d9; }