CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents. It controls the layout, colors, fonts, spacing, and overall visual design of web pages, allowing you to create visually attractive and consistent user interfaces.
With CSS, you separate content (HTML) from presentation (design), making your code cleaner, easier to maintain, and more reusable across multiple pages.
CSS relies on a few fundamental building blocks to target and style elements:
p, .btn, #header).color, font-size, margin).red, 16px, center).color: blue;).A standard CSS rule follows a strict structure: a selector followed by a declaration block.
/* The Selector targets the element */
selector {
property: value; /* Property: Value pair */
}
Here:
selector targets the HTML element(s).property describes what visual feature you want to style.value sets how that feature should look.;, and all declarations are wrapped in {}.Below is a simple CSS snippet that styles the page background, headings, and paragraphs:
/* 1. Target the entire body */
body {
background-color: #f0f0f0; /* Light grey background */
font-family: Arial, sans-serif;
}
/* 2. Target main headings */
h1 {
color: #007bff; /* Brand Blue */
text-align: center;
}
/* 3. Target paragraphs */
p {
color: #333; /* Dark grey text */
font-size: 16px;
line-height: 1.5;
}
This is a sample styled box with CSS!
In this example:
body gets a light gray background and a clean sans-serif font.h1 text becomes blue and centered, making the page title stand out.p text is set to a comfortable size and line height for better readability.Some frequently used properties you'll use in almost every project:
| Property | Description | Example Value |
|---|---|---|
color |
Text color | #333, red |
background-color |
Background color of element | #fff, blue |
font-family |
Font typeface | Arial, sans-serif |
font-size |
Size of text | 16px, 1rem |
text-align |
Horizontal alignment of text | left, center |
margin |
Space outside element | 10px, 1em, auto |
padding |
Space inside element | 5px, 1rem, 0 |
<h1> elements to have a blue color and center alignment.