Styling HTML with CSS
π§΅ Introduction to HTML and CSS
CSS (Cascading Style Sheets) is the language used to style HTML content. It controls layout, colors, fonts, spacing, animations, and responsiveness of your webpage. While HTML builds the structure, CSS handles the presentation π .
>>βHTML is the skeleton, CSS is the skin β together they form the body of the web.β πΈοΈ
π― 3 Ways to Apply CSS
You can apply CSS to your HTML using:
- Inline CSS β Directly inside HTML tags
- Internal CSS β Inside a <style> block in the HTML file
- External CSS β Linked via a separate .css file
π Inline CSS
Inline CSS Example
<p style="color: blue; font-size: 20px;">Styled Text</p>Note
β οΈ Use inline styles only for quick tests or small tweaks. Not recommended for full layouts.
π§ Internal CSS
Internal CSS Example
<head>
<style>
p {
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<p>This is styled with internal CSS</p>
</body>Note
π Internal CSS is useful for single-page documents or demos.
π External CSS
External CSS Example
<head>
<link rel="stylesheet" href="styles.css" />
</head>styles.css
body {
background-color: #f2f2f2;
}
h1 {
color: crimson;
text-align: center;
}Note
πΌ This is the most scalable and maintainable way to style websites.
π‘ CSS Syntax Overview
CSS Syntax
selector {
property: value;
}Example:
Code Snippet
p {
color: red;
font-size: 16px;
}π§ Common CSS Properties
| Property | Use |
|---|---|
| color | Text color |
| background-color | Element background |
| font-size | Size of the text |
| margin / padding | Spacing outside/inside an element |
| border | Adds a border |
π Real-World Example
Styled Card Example
<html>
<head>
<link rel="stylesheet" href="card.css" />
</head>
<body>
<div class="card">
<h2>Card Title</h2>
<p>This is a styled card.</p>
</div>
</body>
</html>card.css
.card {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}π Learn More
>>βDesign is not just what it looks like and feels like. Design is how it works.β β Steve Jobs π‘