CSS Intro
CSS Intro
CSS Intro
• HTML provides the structure and content of your webpage (headings, paragraphs,
images, etc.).
• CSS styles the appearance of that content (colors, fonts, layouts, animations).
• Maintainability: Easily change styles across multiple pages without altering content.
• Clarity: Code becomes more organized and readable.
• Reusability: Create styles once and apply them to various elements.
• Before CSS, styling was directly embedded in HTML using tags like <font> and <center>.
• This made pages inflexible and difficult to maintain.
• CSS arose in the mid-1990s to separate style from structure.
• While CSS is the core styling language, preprocessors like SASS and LESS offer additional
features:
• Variables: Store values and reuse them throughout your styles.
• Nesting: Indent code to create more readable, hierarchical structures.
• Mixins: Define reusable blocks of styles.
• Functions: Perform calculations and manipulations within styles.
These preprocessors compile into regular CSS before being used by the browser.
a) Inline Styles:
• Use the style attribute within HTML elements.
• Example: <h1 style="color: red">This is a red heading</h1>
• Pros: Quick and easy for small changes.
• Cons: Not maintainable for large websites, clutters HTML code.
b) Internal Styles:
• Define styles within a <style> tag in the <head> section of your HTML.
• Example:
<head>
<style>
h1 {
color: blue;
}
p {
font-size: 16px;
}
</style>
</head>
• Pros: More organized than inline styles, applies to all elements on the page.
• Cons: Still embedded in HTML, not reusable across multiple pages.
c) External Styles:
• Create a separate .css file and link it to your HTML using the <link> tag in the <head>
section.
• Example:
<head>
<link rel="stylesheet" href="style.css">
</head>
• Pros: Most recommended approach, highly reusable, styles kept separate from HTML.
• Cons: Requires creating an extra file, initial setup might be less intuitive.
5. CSS Syntax:
Common Properties:
• color: Text color.
• font-family: Font type.
• font-size: Text size.
• background-color: Background color.
• margin: Spacing around elements.
• padding: Spacing inside elements.
6. CSS Selectors:
Basic Selectors:
Combined Selectors:
You can combine selectors using commas, spaces, and child combinators to create more specific
targets: