Introduction to CSS
Introduction to CSS
CSS (Cascading Style Sheets) is a styling language used to describe the look and formatting of HTML
documents. With CSS, we can control the layout, colors, fonts, spacing, and more, making our
websites attractive and user-friendly.
2. Properties and Values: Each CSS rule consists of a property (like color or font-size) and a
value.
3. CSS Syntax: Basic CSS syntax involves a selector, followed by curly braces {}, containing
property-value pairs.
CSS Syntax
selector {
property: value;
For example:
h1 {
color: blue;
font-size: 24px;
1. Selectors
p{
color: green;
Class Selector: Targets elements with a specific class attribute (use .className).
.highlight {
background-color: yellow;
#main-heading {
font-size: 30px;
h1 {
color: red;
p{
font-size: 16px;
body {
background-color: #f0f0f0;
Every HTML element is a rectangular box and has margins, borders, padding, and content.
margin: 20px;
.container {
padding: 15px;
.container {
4. Text Styling
Text Alignment:
h1 {
text-align: center;
Text Decoration:
a{
text-decoration: none;
1. Inline CSS: Add CSS directly to an HTML element using the style attribute.
2. Internal CSS: Use the <style> tag within the HTML <head>.
3. External CSS: Link a CSS file using <link rel="stylesheet" href="style.css"> in the
HTML <head>.
Conclusion
CSS is essential in web design, allowing for customization and enhancing user experience.
Understanding CSS basics is the first step toward creating attractive, functional websites. Keep
experimenting with different styles to see how they impact your webpage’s appearance.
Inline CSS
Inline CSS allows you to apply styles directly to an HTML element using the style attribute, which is
placed inside the opening tag of the element. Here are some examples that illustrate how to use
inline CSS.
To change the color of a heading directly, you can use inline CSS like this:
This example applies the color blue only to this specific <h1> element.
The background-color property only affects this paragraph, leaving other paragraphs unaffected.
<p style="font-size: 20px; font-family: Arial, sans-serif;">This paragraph has custom font size and
style.</p>
Here, the font size is set to 20 pixels, and the font family is set to Arial.
</div>
This div element has a solid black border and padding that creates space inside the border.
The text-align: center; rule centers the text within the element.
</div>
Here:
This changes the link color to green and removes the underline.
You can apply multiple styles at once by separating each property-value pair with a semicolon:
</p>
This paragraph has white text on a dark blue background, with padding and bold text.
Inline CSS is useful for quick, single-use styling, but remember that using too much inline CSS can
make the code harder to maintain. For larger projects, consider using internal or external CSS for a
more organized approach.
Internal CSS
Internal CSS is added within the <style> element inside the <head> section of the HTML document.
This method allows you to apply styles to multiple elements on the same page without needing an
external CSS file. Here are some examples to demonstrate how internal CSS works.
To use internal CSS, place the CSS code inside a <style> tag in the HTML <head>, like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
h1 {
color: darkblue;
text-align: center;
p{
font-size: 18px;
color: #555;
.container {
background-color: #f4f4f4;
padding: 20px;
margin: 10px 0;
a{
color: green;
text-decoration: none;
a:hover {
color: darkgreen;
text-decoration: underline;
</style>
</head>
<body>
<p>This is a paragraph styled with internal CSS. Internal CSS applies styles to multiple elements on
a single page.</p>
<div class="container">
</div>
</body>
</html>
1. Headings:
o h1 elements are styled to have a dark blue color and centered alignment.
2. Paragraphs:
o p elements have a font size of 18 pixels and a light grey text color (#555).
3. Containers:
o The .container class adds styles to any <div class="container"> element, giving it a
light grey background, padding, a border, and some vertical spacing.
4. Links:
o When the user hovers over a link, the color changes to dark green, and an underline
appears.
By using internal CSS, you can keep your styles organized within the same HTML file, making it easy
to manage while learning CSS or working on small projects.
External CSS
External CSS is a method of separating your CSS code into a separate file, typically with
a .css extension. This approach keeps your HTML clean and organizes the styling logic in a reusable
and maintainable way.
1. Reusability: One CSS file can be linked to multiple HTML pages, ensuring consistent styling
across all pages.
2. Separation of Concerns: Keeps the content (HTML) and presentation (CSS) separate, making
the code cleaner and easier to maintain.
3. Efficiency: The browser caches the CSS file, reducing load time for pages that use the same
stylesheet.
1. Create a CSS File: Save the CSS code in a file with a .css extension (e.g., styles.css).
2. Link the CSS File: Use the <link> element inside the <head> section of your HTML file to link
the external CSS file.
body {
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
/* Styling for the heading */
h1 {
color: darkblue;
text-align: center;
padding: 20px;
background-color: #e0e0ff;
p{
font-size: 18px;
color: #333;
line-height: 1.6;
margin: 15px;
.container {
width: 80%;
margin: 0 auto;
background-color: #ffffff;
border-radius: 8px;
padding: 20px;
a{
color: darkgreen;
text-decoration: none;
a:hover {
text-decoration: underline;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<p>External CSS allows you to apply consistent styles across multiple web pages. It also makes
your code cleaner and easier to maintain.</p>
<p>With an external CSS file, you can define styles once and reuse them across multiple HTML
documents.</p>
</div>
</body>
</html>
project-folder/
│
o Create a file named index.html and paste the HTML code above.
o Create another file named styles.css and paste the CSS code above.
3. Open the index.html file in your browser to see the styled webpage.
Key Notes
html
project-folder/
├── css/
│ └── styles.css
├── index.html
html
Using external CSS is highly recommended for building professional websites, as it ensures clean,
modular, and scalable code.
Example of a colorful Webpage with HTML and CSS
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colorful Sample Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
</nav>
</header>
<main class="container">
<section id="home">
<h2>Home</h2>
<p>Welcome to my colorful and creative website. Explore and enjoy the vibrant design!</p>
</section>
<section id="about">
<h2>About</h2>
<p>This page is a demonstration of how to use HTML and CSS to build a beautiful and responsive
webpage.</p>
</section>
<section id="services">
<h2>Services</h2>
<ul>
<li>Web Design</li>
<li>Content Creation</li>
<li>SEO Optimization</li>
<li>Student Training</li>
</ul>
</section>
<section id="contact">
<h2>Contact</h2>
<p>Email: <a href="mailto:[email protected]">[email protected]</a></p>
<p>Phone: <a href="tel:+919847390760">+91 9847390760</a></p>
<p>Follow me on <a href="https://www.linkedin.com" target="_blank">LinkedIn</a></p>
</section>
</main>
<footer>
<p>Copyright © 2025 mySite. All rights reserved.</p>
</footer>
</body>
</html>
style.css
/* General Reset */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
body {
color: #333;
line-height: 1.6;
/* Header */
header {
color: white;
padding: 30px 0;
text-align: center;
header h1 {
font-size: 2rem;
nav ul {
margin-top: 15px;
display: flex;
justify-content: center;
list-style: none;
gap: 20px;
nav a {
color: white;
font-weight: bold;
text-decoration: none;
border-radius: 20px;
nav a:hover {
}
/* Main Container */
.container {
max-width: 900px;
padding: 30px;
background-color: #ffffffcc;
border-radius: 15px;
/* Sections */
section {
margin-bottom: 30px;
h2 {
color: #e91e63;
margin-bottom: 10px;
ul {
padding-left: 20px;
a{
color: #00796b;
text-decoration: none;
a:hover {
text-decoration: underline;
/* Footer */
footer {
background-color: #ff7043;
color: white;
text-align: center;
padding: 15px 0;
margin-top: 40px;
HTML + CSS webpage design for an online book store. It includes: ( University Question)
A page title
Main heading
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #f9f9f9;
margin: 0;
padding: 0;
}
header {
background-color: #4CAF50;
color: white;
padding: 20px 0;
text-align: center;
h1 {
margin: 0;
.content {
padding: 20px;
h2 {
color: #333;
ul {
list-style-type: none;
padding-left: 0;
li {
background-color: #e0f7fa;
margin: 10px 0;
padding: 12px;
border-radius: 5px;
font-size: 18px;
li:hover {
background-color: #b2ebf2;
</style>
</head>
<body>
<header>
</header>
<div class="content">
<h2>Book Categories</h2>
<ul>
<li>Fiction</li>
<li>Non-Fiction</li>
<li>Biographies</li>
<li>Children's Books</li>
<li>Self Help</li>
</ul>
</div>
</body>
</html>