Before we start writing CSS code, let’s first understand what CSS actually is and why we need it. Let’s start with Introduction to CSS.
What is CSS?
CSS stands for Cascading Style Sheets. CSS is used to design and style HTML elements on a web page. For example, HTML creates a heading:
<h1>Hello World</h1>
CSS can change its color, size, and appearance:
h1 {
color: blue;
font-size: 40px;
}
After applying CSS, the heading will appear blue and larger.
Why Do We Use CSS?
CSS helps us make websites look better. With CSS, you can:
- Change text colors
- Change font sizes
- Add backgrounds
- Add borders
- Create layouts
- Make websites responsive
- Add animations and effects
Without CSS, a website usually looks very simple and plain.
How CSS Works?
CSS works by selecting an HTML element and applying styles to it.
CSS Syntax
selector {
property: value;
}
For example:
p {
color: red;
}
Here:
pis the selectorcoloris the propertyredis the value
This code changes the color of all <p> elements to red.
Your First CSS Example
Create an HTML file and add the following code:
<!DOCTYPE html>
<html>
<head>
<title>My First CSS</title>
<style>
h1 {
color: blue;
}
p {
color: green;
}
</style>
</head>
<body>
<h1>Welcome to CSS</h1>
<p>I am learning CSS.</p>
</body>
</html>
Result
- The heading will appear blue.
- The paragraph will appear green.
Quick Recap
- CSS stands for Cascading Style Sheets.
- CSS is used to style HTML elements.
- CSS uses selectors, properties, and values.
- CSS can change colors, fonts, layouts, and much more.
Frequently Asked Questions (FAQs)
Q1. What is CSS?
CSS stands for Cascading Style Sheets. It is used to style and design HTML elements on a web page.
Q2. Why is CSS used in web development?
CSS is used to improve the appearance of websites by changing colors, fonts, sizes, backgrounds, layouts, and other visual elements.
Q3. What is the difference between HTML and CSS?
HTML is used to create the structure and content of a web page, while CSS is used to style and design that content.
Q4. Can CSS be used without HTML?
CSS is mainly used with HTML to style web page elements. HTML creates the structure, while CSS controls its appearance.
Q5. What can I do with CSS?
With CSS, you can change colors, fonts, sizes, backgrounds, borders, layouts, animations, and make websites responsive.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
