CSS
CSS
CSS
Introduction to CSS, CSS Syntax, Location of Styles, Selectors, The Cascade: How Styles
CSS is a language used to describe the presentation of HTML documents. It controls the layout,
colors, fonts, and overall visual style of web pages, allowing for the separation of content (HTML)
from design.
Introduction to CSS
CSS enhances the appearance of web pages by defining how HTML elements should be displayed. It
allows developers to apply consistent styling across multiple pages, making web design more
efficient and maintainable.
CSS Syntax
Declaration Block: Contains one or more declarations, enclosed in curly braces {}.
Declaration: A pair of property and value separated by a colon (:), specifying the styling for
the selected element.
Example:
css
Copy code
p{
color: blue;
font-size: 16px;
Location of Styles
html
Copy code
2. Internal CSS: Placed within a <style> tag inside the <head> of an HTML document.
html
Copy code
<style>
p{
color: blue;
</style>
3. External CSS: Linked to an HTML document via a separate .css file, using the <link> tag.
html
Copy code
Selectors
Selectors define which HTML elements the CSS rules apply to:
css
Copy code
p { color: blue; }
Class Selector: Targets elements with a specific class, defined with a . prefix.
css
Copy code
ID Selector: Targets a unique element with a specific ID, defined with a # prefix.
css
Copy code
css
Copy code
The cascade refers to the order in which CSS rules are applied when there are conflicts:
Inheritance: Some properties are inherited by child elements from their parent elements
(e.g., color, font-family).
Specificity: The more specific a selector, the higher its priority. IDs have more specificity
than classes, and classes have more specificity than elements.
Source Order: When selectors have the same specificity, the last one in the source code
takes precedence.
Important: The !important keyword can override any other rule, but should be used
sparingly.
Example:
css
Copy code
p { color: blue; }
The box model describes how elements are displayed and spaced on a web page. It consists of:
Content: The actual content inside the element (e.g., text, images).
Margin: Space outside the border, separating the element from others.
Example:
css
Copy code
div {
width: 100px;
padding: 10px;
margin: 20px;
The total width of the div would be 100px (content) + 10px (padding) + 5px (border) + 20px
(margin).
css
Copy code
p { color: blue; }
css
Copy code
css
Copy code
h1 { font-size: 24px; }
css
Copy code
h1 { text-align: center; }
Text Decoration: Adds decoration like underlines, overlines, or strikethroughs.
css
Copy code
a { text-decoration: none; }
Summary
CSS is a powerful tool for controlling the presentation of web pages. Understanding its syntax, how
styles are applied, and key concepts like the box model and cascade allows developers to create
visually appealing and well-structured web designs. By mastering selectors and text styling, you can
fine-tune the appearance of your web content for a better user experience.