CSS (Cascading Style Sheets) is used to style and design HTML web pages. It controls how elements like text, images, tables, and layouts appear on the screen.
With CSS you can change:
<style> h1 { color: blue; } </style> <h1>Welcome to CSS</h1>
CSS syntax defines how CSS rules are written to style HTML elements.
| Part | Meaning |
|---|---|
| Selector | HTML element to style |
| Property | Style type (color, size) |
| Value | Style value |
/* Selector { Property: Value; } */ p { color: red; font-size: 18px; }
<!DOCTYPE html> <html> <head> <style> p { color: red; } </style> </head> <body> <p>Styled Paragraph</p> </body> </html>
There are 3 ways to apply CSS in HTML. Each has a specific purpose and scope.
Written directly inside the HTML tag.
Written inside the <head> section.
Written in a separate .css file.
Written inside the HTML element using the style attribute.
Rules:
<h1 style="color:red;"> Hello World </h1> <p style="font-size:20px;"> This is a paragraph. </p>
Written inside the <style> tag in the <head> section.
Rules:
<head> <style> h1 { color: green; } p { font-size: 18px; } </style> </head>
h1 { color: blue; } p { font-size: 20px; }
<link rel="stylesheet" href="style.css">
Rules:
Comments are ignored by the browser. They help developers understand the code.
Rules:
/* Change heading color */ h1 { color: red; } /* Style paragraph size */ p { font-size: 20px; }