Introduction to CSS
What is CSS?
CSS stands for Cascading Style Sheets.
It is used to control the look and feel of a webpage.
“Cascading” refers to how CSS applies styles in order of importance.
How CSS Works
• HTML handles the structure of the webpage.
• CSS defines the appearance of the webpage.
• CSS can also work with JavaScript to dynamically update styles.
Why Use CSS?
• Allows applying specific designs to specific HTML elements.
• Lets you separate content (HTML) from style (CSS).
Ways to Insert CSS
There are 3 main methods:
- Inline CSS
- Internal (Embedded) CSS
- External CSS
Inline CSS
• Applies a unique style directly to a single HTML element.
• Uses the style attribute.
Syntax:
<p style="property:value;">Text</p>Output:
This is an example of inline styling
Internal / Embedded CSS
• Written inside the <style> element within the <head> section.
• Useful when only one page needs unique styling.
Syntax:
<style>selector { property: value; }
</style>
Output:
This is my first paragraph
This is my second paragraph
External CSS
• All styling rules are placed in a separate .css file.
• The external CSS file is linked inside the <head> using <link>.
Syntax:
<link rel="stylesheet" href="style.css">Output:
(HTML uses an external CSS file, so nothing visual to display here.)
CSS Syntax
CSS is made of style rules the browser reads and applies to HTML elements.
A CSS rule has two main parts:
- Selector - the HTML element to style
- Declaration Block - contains one or more declarations
Syntax:
selector {
property: value;
property: value;
}
• Each declaration ends with a semicolon ;
• The block is wrapped in curly braces { }
Output Example:
This paragraph is styled using CSS syntax.