CSS (Cascading Style Sheets) is a language designed to simplify the process of
making web pages presentable.
It allows you to apply styles to HTML documents by prescribing:
- Colors
- Fonts
- Spacing
- Positioning
Main advantages:
• Separation of content (HTML) and styling (CSS)
• Reuse of styles across multiple pages without rewriting
• HTML uses tags; CSS uses rule sets
• CSS styles are applied using selectors
----------------------------------------
Why CSS?
----------------------------------------
• Saves Time:
Write CSS once and reuse it across multiple HTML pages.
• Easy Maintenance:
Change the style globally with a single modification.
• Search Engine Friendly:
Clean coding technique improves readability for search engines.
• Superior Styles:
Offers a wider array of attributes compared to HTML.
• Offline Browsing:
CSS can store web applications locally using cache, allowing offline viewing.
----------------------------------------
CSS Syntax
----------------------------------------
CSS consists of style rules interpreted by the browser and applied to elements.
A rule set includes:
- Selector: Targets specific HTML elements
- Declaration Block: Contains one or more declarations
Each declaration:
- Property: The CSS attribute (e.g., color, font-size)
- Value: The value of that property
----------------------------------------
Example
----------------------------------------
// HTML Element
Hello World
// CSS Style
h1 {
color: blue;
font-size: 12px;
}
In this example:
• The selector is "h1"
• The declarations set the text color to blue and font size to 12px
Another example:
p {
color: blue;
text-align: center;
}
This rule applies to all tags:
• Text color: Blue
• Text alignment: Center
Notes:
- CSS declarations end with a semicolon (;)
- Declaration blocks are wrapped in curly braces {}
----------------------------------------
Ways to Apply CSS
----------------------------------------
1. Inline CSS:
- Inside the HTML element using the style attribute
p style="color: red;">Text p
2. Internal CSS:
- Within
3. External CSS:
- Using a separate .css file linked via
----------------------------------------
What is Cascading?
----------------------------------------
Cascading defines how conflicting CSS rules are resolved.
Priority order (highest to lowest):
1. Inline styles
2. Internal styles (within tag)
3. External stylesheets
Additional factors:
• Specificity of selectors
• Order of appearance
•
Understanding cascading allows for layered, flexible styling.
----------------------------------------
Advantages of CSS
----------------------------------------
✔ Simplifies web design and maintenance
✔ Enhances performance and user experience
✔ Supports responsive design across all devices