CSS Introduction (Casestudy)
CSS Introduction (Casestudy)
CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a
document written in HTML or XML. CSS defines how elements are displayed on the screen,
paper, or other media. It allows for the separation of content (HTML) from design (CSS),
improving flexibility and control over the layout and appearance of web pages.
Types of CSS
1. Inline Styles
Inline styles are applied directly to an HTML element using the style attribute.
It has the highest specificity among the three types of CSS.
Example:
✅ Advantages:
Embedded styles are defined within a <style> tag inside the <head> section of an
HTML document.
Used to apply styles to a single document.
Example:
<head>
<style>
p {
color: blue;
font-size: 18px;
}
</style>
</head>
✅ Advantages:
External styles are stored in a separate .css file and linked using the <link> tag.
Recommended for consistent styling across multiple pages.
Example:
<head>
<link rel="stylesheet" href="styles.css">
</head>
✅ Advantages:
Conflicting Styles
When multiple styles target the same element, CSS follows a hierarchy of specificity:
p {
color: blue !important;
}
div {
position: relative;
top: 20px;
left: 30px;
}
2. Absolute Positioning
An element is positioned relative to its nearest positioned ancestor (or the document
body if none exists).
Example:
div {
position: absolute;
top: 50px;
left: 50px;
}
Backgrounds
CSS allows you to style the background of elements using:
o Background color
o Background images
o Background repeat and position
body {
background-color: lightgray;
background-image: url('background.jpg');
background-repeat: no-repeat;
background-position: center;
}
Box Model
Every HTML element is treated as a box with the following parts:
Example:
div {
width: 300px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
Text Flow
CSS controls text alignment and direction using properties like:
o text-align
o direction
o white-space Example:
p {
text-align: center;
direction: rtl;
}
Media Types
CSS can apply different styles depending on the media type:
Example:
@media print {
body {
font-size: 12pt;
}
}
Media Queries
Media queries allow you to apply styles based on screen size or device type. Example:
Text Shadows
Adds shadow effects to text.
h1 {
text-shadow: 2px 2px 5px gray;
}
Rounded Corners
Creates rounded corners using the border-radius property.
div {
border-radius: 10px;
}
Linear Gradient
Creates a linear color transition.
div {
background: linear-gradient(to right, red, yellow);
}
Radial Gradient
Creates a circular color transition.
div {
background: radial-gradient(circle, red, yellow);
}
Image Borders
Applies an image as a border.
div {
border: 10px solid;
border-image: url('border.png') 30 stretch;
}
Animation
CSS @keyframes define animations. Example:
@keyframes slide {
from { left: 0px; }
to { left: 200px; }
}
div {
position: relative;
animation: slide 2s;
}
Selectors
1. Universal Selector (*) – Selects all elements
2. Type Selector (h1) – Selects all elements of a type
3. Class Selector (.class) – Selects all elements with a class
4. ID Selector (#id) – Selects a specific element
5. Attribute Selector ([type="text"]) – Selects elements with an attribute
Example:
p {
color: red;
}
#heading {
color: blue;
}
.class-name {
font-size: 18px;
}
div {
width: 100px;
transition: width 2s;
}
div:hover {
width: 300px;
}
2. Transformations
div {
transform: rotate(45deg);
}
1. Conflicting Styles
When multiple styles target the same element, CSS resolves conflicts based on specificity
and inheritance.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
#title {
color: blue; /* ID selector */
}
.heading {
color: red; /* Class selector */
}
</style>
</head>
<body>
</body>
</html>
Explanation:
The element has both an ID selector (#title) and a class selector (.heading).
ID selectors have higher specificity than class selectors.
Therefore, the text color will be blue.
Output:
The heading will appear blue because ID selectors have higher specificity than class
selectors.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
.relative-box {
position: relative;
width: 200px;
height: 100px;
background-color: lightblue;
}
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
background-color: coral;
padding: 10px;
}
</style>
</head>
<body>
<div class="relative-box">
Relative Box
<div class="absolute-box">Absolute Box</div>
</div>
</body>
</html>
Explanation:
Output:
The "Absolute Box" appears inside the Relative Box at an offset of top: 20px and
left: 20px.
3. Backgrounds
You can set background color, images, and positioning using CSS.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
.background-box {
width: 300px;
height: 150px;
background-color: lightgray;
background-image: url(https://media.geeksforgeeks.org/wp-
content/cdn-uploads/20190417124305/250.png );
);
background-repeat: no-repeat;
background-position: center;
background-size: content;
}
</style>
</head>
<body>
<div class="background-box"></div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.background-box {
width: 300px;
height: 150px;
background-color: lightgray;
background-image: url(https://media.geeksforgeeks.org/wp-
content/cdn-uploads/20190417124305/250.png );
);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="background-box"></div>
</body>
</html>
Explanation:
Output:
A 300px by 150px box with a background image is shown, filling the entire box.
4. Box Model
CSS box model includes content, padding, border, and margin.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
.box-model {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 20px;
background-color: lightgreen;
}
</style>
</head>
<body>
</body>
</html>
Explanation:
Output:
A green box with a black border and padding appears.
5. Text Flow
CSS can control how text is aligned and displayed.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
.text-flow {
text-align: justify;
direction: ltr;
white-space: normal;
}
</style>
</head>
<body>
<p class="text-flow">
This is an example of text flow. The text will wrap and align according
to the CSS rules.
</p>
</body>
</html>
Explanation:
Output:
6. Media Queries
CSS can adapt styles based on screen size.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: white;
}
</body>
</html>
Explanation:
Output:
7. Text Shadows
You can add shadows to text using text-shadow.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 2px 2px 5px gray;
}
</style>
</head>
<body>
</body>
</html>
Explanation:
Output:
8. Rounded Corners
You can create rounded corners using border-radius.
HTML Example:
<!DOCTYPE html>
<html>
<head>
<style>
.rounded {
width: 150px;
height: 150px;
background-color: coral;
border-radius: 15px;
}
</style>
</head>
<body>
<div class="rounded"></div>
</body>
</html>
Output:
9. Linear Gradient
CSS supports color gradients.
HTML Example:
<div style="width:200px; height:100px; background: linear-gradient(to
right, red, yellow);"></div>
Output:
HTML Example:
<div style="width:200px; height:100px; background: radial-gradient(circle,
red, yellow);"></div>
Output:
11. Animation
CSS allows element animation.
HTML Example:
<style>
@keyframes slide {
from { left: 0; }
to { left: 100px; }
}
div {
position: relative;
animation: slide 2s infinite alternate;
}
</style>
<div>Animated Box</div>
Output:
HTML Example:
<style>
div {
transition: transform 0.5s;
}
div:hover {
transform: rotate(15deg);
}
</style>
<div>Hover Me!</div>
Output:
Box rotates when hovered.
Example2
Animation
The following example binds the "example" animation to the <div> element.
The animation will last for 4 seconds, and it will gradually change the
background-color of the <div> element from "red" to "yellow":
Case study for CSS
1. You are designing a responsive e-commerce website. On mobile devices (width less than
600px), the sidebar overlaps the product grid. On tablets (width between 600px and 900px),
the product cards appear too small. The layout works fine on desktops. Write CSS media
queries to solve these layout issues and explain how media queries help in building
responsive websites.
🖥️HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-
scale=1.0" />
<title>Responsive E-Commerce</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<!-- Sidebar -->
<div class="sidebar">
<h2>Sidebar</h2>
<ul>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
</ul>
</div>
</body>
</html>
🎨 CSS Code
/* General Styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.container {
display: flex;
gap: 20px;
padding: 20px;
}
/* Sidebar Styling */
.sidebar {
width: 250px;
background-color: #f4f4f4;
padding: 20px;
box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
}
.product-card {
background-color: #e0e0e0;
padding: 20px;
text-align: center;
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
transition: transform 0.3s ease;
}
.product-card:hover {
transform: scale(1.05);
}
.product-grid {
grid-template-columns: repeat(1, 1fr); /* Single column */
}
}
📌 Explanation of Code
✅ 1. General Styles
The container uses flexbox to arrange the sidebar and product grid side by side.
The gap property provides spacing between elements.
box-shadow, border-radius, and hover effects improve the overall styling.
.sidebar {
width: 100%;
margin-bottom: 20px;
}
.product-grid {
grid-template-columns: repeat(1, 1fr);
}
}
Explanation
.container is a CSS class selector targeting an element with the class name
container.
flex-direction: column changes the layout direction of a flex container to be
vertical:
➔ The child elements of .container will now be stacked vertically instead of
horizontally.
.sidebar is another CSS class selector targeting an element with the class name
sidebar.
width: 100% makes the sidebar take up the full width of the screen.
margin-bottom: 20px adds 20 pixels of space below the sidebar, creating a gap
between it and the next element.
Effect:
The sidebar will stretch to fill the screen width and leave a gap below it.
Effect:
Products in the grid will stack vertically into a single column.
✅ 3. Tablets – min-width: 600px and max-width: 900px
✅ min-width: 600px ➔ The styles apply when the screen size is 600 pixels or wider.
✅ max-width: 900px ➔ The styles apply when the screen size is 900 pixels or narrower.
👉 This means the styles will apply for devices like tablets (in portrait or landscape mode) or
small laptop screens.
👉 Solution:
✔️On desktops, product cards are displayed in three columns.
✔️Utilizes available screen space for better organization.
🌟 Output Description
1. On Desktop:
2. You are designing a login form for a website. When a user focuses on an input
field, it should slightly rotate and scale using transformations, and the Submit
button should smoothly change its background color on hover using
transitions. Write the CSS code using appropriate selectors, transitions, and
transformations to achieve this. Also, explain how these properties enhance
the interactivity and user experience of a web page.
🖥️HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-
scale=1.0" />
<title>Interactive Login Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="login-container">
<h2>Login</h2>
<form action="#">
<input type="text" class="input-field" placeholder="Username"
required />
<input type="password" class="input-field" placeholder="Password"
required />
<button type="submit" class="submit-btn">Login</button>
</form>
</div>
</body>
</html>
🎨 CSS Code
/* ================= General Styles ================= */
body {
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
.login-container {
background-color: #ffffff;
padding: 40px;
border-radius: 12px;
width: 320px;
text-align: center;
h2 {
color: #333;
margin-bottom: 20px;
font-size: 24px;
font-weight: 600;
.input-field {
width: 100%;
padding: 12px;
margin-bottom: 16px;
border-radius: 8px;
box-sizing: border-box;
font-size: 16px;
}
/* ✅ Transformation on Focus */
.input-field:focus {
outline: none;
transform: scale(1.03);
.input-field:hover {
border-color: #4CAF50;
Explanation
1. .input-field:focus
.input-field:focus {
outline: none;
transform: scale(1.03);
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.15);
border-color: #4CAF50; /* Highlight border color */
}
✅ Purpose:
The :focus pseudo-class is triggered when the user clicks or tabs into an input field
(when it gains focus).
This helps give the user visual feedback that the input is active and ready for typing.
✅ Step-by-Step Explanation:
Property Purpose
outline: none;
Removes the default browser outline around the input
field (to apply custom styling).
transform: scale(1.03);
Scales the input field by 3% to make it slightly larger
and more prominent.
box-shadow: 0px 4px 12px Adds a shadow effect to give the input field a raised,
rgba(0, 0, 0, 0.15); 3D look.
border-color: #4CAF50;
Changes the border color to green to highlight the
focused state.
➡️Example:
2. .input-field:hover
.input-field:hover {
border-color: #4CAF50;
}
✅ Purpose:
The :hover pseudo-class is triggered when the user moves the mouse pointer over the
input field.
It provides visual feedback to signal that the field is interactive.
✅ Step-by-Step Explanation:
Property Purpose
border-color: Changes the border color to green when the user hovers over the
#4CAF50; field.
➡️Example:
The green border color appears, signaling that the field is interactive and ready for
input.
.submit-btn {
width: 100%;
padding: 12px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 500;
cursor: pointer;
/* ✅ Transition on Hover */
.submit-btn:hover {
background-color: #45a049;
/* ✅ Animation on Click */
.submit-btn:active {
transform: scale(0.95);
.submit-btn:disabled {
background-color: #ccc;
cursor: not-allowed;
Explanation
1. .submit-btn:active
.submit-btn:active {
transform: scale(0.95);
}
✅ Purpose:
The :active pseudo-class is triggered when the user clicks and holds down the submit
button.
It gives the user immediate visual feedback that the button is being pressed.
✅ Step-by-Step Explanation:
Property Purpose
➡️Example:
2. .submit-btn:disabled
.submit-btn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
✅ Purpose:
The :disabled pseudo-class targets the button when it’s in a disabled state.
A disabled button usually means that the form is incomplete or not ready for submission.
✅ Step-by-Step Explanation:
Property Purpose
background-color:
#ccc;
Changes the background to light gray to signal that the button is inactive.
📌 Explanation of Code
✅ 1. General Styles
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
✔️Centers the form vertically and horizontally using flexbox.
✔️Sets a light gray background color to make the form stand out.
✅ 2. Login Container
.login-container {
background-color: #ffffff;
padding: 40px;
border-radius: 12px;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
width: 320px;
text-align: center;
}
✔️The login form is given a clean, modern look with rounded corners and a subtle shadow.
✔️The container width is fixed to 320px to maintain consistency across screen sizes.
✅ 3. Input Fields
.input-field {
width: 100%;
padding: 12px;
margin-bottom: 16px;
border: 1px solid #ccc;
border-radius: 8px;
box-sizing: border-box;
font-size: 16px;
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
✅ 4. Transformation on Focus
.input-field:focus {
outline: none;
transform: scale(1.05) rotate(2deg);
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.2);
}
✅ 5. Submit Button
.submit-btn {
width: 100%;
padding: 12px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
✅ 6. Transition on Hover
.submit-btn:hover {
background-color: #45a049;
}
✅ 7. Animation on Click
.submit-btn:active {
transform: scale(0.95);
}
On Focus:
On Hover:
On Click:
👉 Copy and run this code — you'll love how interactive it feels! 😎