CSS separates the look of a page from its content. The same HTML can be given a completely different appearance just by changing the style sheet.
What is CSS
CSS (Cascading Style Sheets) is a language that describes how HTML elements are displayed: colours, fonts, sizes, spacing, borders, backgrounds and layout. A CSS rule has a selector, which chooses the elements, and a declaration block in braces, made of property: value; pairs: h1 { color: navy; font-size: 28px; }. CSS was proposed by Håkon Wium Lie in 1994; CSS3 split it into modules that added rounded corners, shadows, gradients, transitions, animations, Flexbox, Grid and media queries.
Definition: CSS. Cascading Style Sheets, a style-sheet language that describes the presentation of HTML documents, such as colour, font, spacing and layout, separately from their content.
Advantages of CSS
CSS separates content from presentation, so a page is easier to read and maintain. One external style sheet can style a whole website, giving consistency and saving work. Pages load faster, since the style sheet is downloaded once and cached. Different styles can be given for screens, phones and printers.
Types of CSS
Inline CSS is written in the style attribute of an element: <p style="color: red;">. It affects only that element and has the highest priority, but mixing style with content is hard to maintain. Internal (embedded) CSS is written in a <style> element in the <head> of the page, and applies to that page only. External CSS is written in a separate .css file and linked from each page with <link rel="stylesheet" href="style.css"> in the head. It is the best method for real websites.
The Cascade
When several rules apply to the same element, the cascade decides which wins. Inline styles beat internal and external ones; among rules, the more specific selector wins (id beats class beats element), and if specificity is equal, the rule that comes later wins. !important overrides the normal order. Some properties, such as color and font, are inherited by child elements.
Table 2.1: Types of CSS
| Type | Where written | Scope |
|---|---|---|
| Inline | style attribute | One element |
| Internal | <style> in head | One page |
| External | .css file linked | Whole website |
Example 2.1: Three ways to colour text
Problem: Make the heading navy using an external file, the paragraphs grey with internal CSS, and one word red with inline CSS.
Solution:
- In style.css, write h1 { color: navy; } and link it in the head.
- In a <style> element, write p { color: #555; }.
- On one <span>, write style="color: red;".
Result: All three methods work together on the same page, and the inline style wins for the red word.
<!-- style.css contains: h1 { color: navy; font-family: Arial; } -->
<head>
<link rel="stylesheet" href="style.css">
<style>
p { color: #555555; line-height: 1.5; }
</style>
</head>
<body>
<h1>Supermarket Offers</h1>
<p>Fresh fruit at
<span style="color: red;">20% off</span> today.</p>
</body>
Common mistakes
- Forgetting the semicolon between declarations.
- Linking an external style sheet without rel="stylesheet".
- Styling every element inline, which makes the site hard to change.
Key takeaways
- A CSS rule is selector { property: value; }.
- Inline, internal and external CSS differ in where they are written and their scope.
- External CSS styles a whole site from one file.
- The cascade gives priority to inline, then specificity, then order.
Practice questions
- What does CSS stand for?
- How is an external style sheet linked?
- Which type of CSS has the highest priority?
Answers to the practice questions
- Cascading Style Sheets.
- With <link rel="stylesheet" href="file.css"> in the head.
- Inline CSS.
Sources
- Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit II
