CSS (Cascading Style Sheets) is the language that makes websites beautiful. While HTML provides the structure, CSS is what controls colors, fonts, spacing, and layout. This guide will teach you the foundations of CSS so you can design modern, responsive websites.
What is CSS?
CSS is a stylesheet language used to control the presentation of HTML elements.
- Cascading: Rules are applied in order of importance (last rule wins).
- Style: Defines how elements look (fonts, colors, sizes).
- Sheets: Styles are reusable and stored in separate files.
👉 Think of HTML as the skeleton of a webpage, and CSS as its clothing and design.
Ways to Add CSS
- Inline CSS: Inside the
style
attribute. - Internal CSS: Inside a
<style>
block in the head. - External CSS: In a separate
.css
file linked to HTML.
<!-- External CSS Example -->
<link rel="stylesheet" href="style.css">
CSS Syntax
selector {
property: value;
}
Example:
p {
color: blue;
font-size: 16px;
}
Selectors You Should Know
p
→ Selects all paragraphs.className
→ Selects elements with a class#idName
→ Selects an element with an IDdiv p
→ Selects paragraphs inside divs*
→ Selects all elements
Working with Colors
You can set colors using names, HEX, RGB, or HSL values.
h1 {
color: red; /* Name */
color: #ff0000; /* HEX */
color: rgb(255,0,0); /* RGB */
color: hsl(0,100%,50%); /* HSL */
}
Styling Text
p {
font-family: Arial, sans-serif;
font-size: 18px;
font-weight: bold;
text-align: center;
text-decoration: underline;
}
The CSS Box Model
Every element in CSS is treated as a box:
- Content → Text or image inside.
- Padding → Space between content and border.
- Border → Outline around the element.
- Margin → Space outside the border.
div {
margin: 20px;
padding: 10px;
border: 2px solid black;
}
Layout Techniques
Modern CSS provides powerful layout systems: Flexbox and Grid.
Flexbox Example:
.container {
display: flex;
justify-content: space-between;
}
Grid Example:
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
}
Responsive Design
Use media queries to make websites mobile-friendly.
@media (max-width: 600px) {
body {
font-size: 14px;
}
}
Best Practices
- Use external CSS for cleaner code.
- Keep selectors simple and organized.
- Use semantic HTML with CSS for better accessibility.
- Prefer relative units (
em
,rem
,%
) over fixed pixels. - Comment your code to improve readability.
Learning Path After CSS
- JavaScript → Add interactivity to pages.
- Responsive Design → Master fluid, mobile-first layouts.
- Frameworks → Learn Bootstrap or Tailwind CSS.
Conclusion
CSS brings life and personality to websites. With it, you can control colors, fonts, layouts, and create visually stunning designs.
👉 Start small by styling your personal webpage, then move on to larger projects. Combine CSS with HTML and JavaScript to become a complete front-end developer.
Happy coding and styling! 🎨✨
No comments:
Post a Comment