CSS_Notes
CSS_Notes
1. What is CSS ?
CSS stands for Cascading Style Sheets.
It’s used to style HTML elements — control colors, fonts, spacing, layout, and more.
Example:
<p style="color: blue;">This is a blue paragraph.</p>
3. CSS Syntax
selector {
property: value;
}
Example:
h1 {
color: red;
font-size: 24px;
}
Selector: Which element to style (e.g., h1)
Property: What to style (e.g., color)
Value: The setting for the property (e.g., red)
Example:
🎯 Quick Comparison
5. Selector forms
1. Element Selector / Type Selector
2. ID Selector
3. Class Selector
4. Universal Selector
5. Group Selector
6. Descendant Selector
7. Child Selector
1. Element Selector
Definition: Selects all elements of a specific type (tag name). \
Example Program :
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: blue;
}
</style>
</head>
<body>
<p>This paragraph will appear in blue color.</p>
</body>
</html>
2. ID Selector
Definition: Selects a single unique element by its ID.
Example Program :
<!DOCTYPE html>
<html>
<head>
<style>
#main-header {
color: green;
}
</style>
</head>
<body>
<h1 id="main-header">This is a green header</h1>
</body>
</html>
3. Class Selector
Definition: Selects elements with a specific class name.
Example Program :
<!DOCTYPE html>
<html>
<head>
<style>
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p class="highlight">This paragraph has a yellow background.</p>
<p>This paragraph has normal background.</p>
</body>
</html>
4. Universal Selector
Definition: Selects all elements in the document.
Example Program :
<!DOCTYPE html>
<html>
<head>
<style>
*{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph with no margin or padding.</p>
</body>
</html>
5. Group Selector
Example Program:
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
color: purple;
}
</style>
</head>
<body>
<h1>Heading 1 in purple</h1>
<h2>Heading 2 in purple</h2>
<p>Paragraph in purple</p>
</body>
</html>
6. Descendant Selector
Definition: Selects elements inside another element (at any depth).
Example Program :
<!DOCTYPE html>
<html>
<head>
<style>
div p {
color: red;
}
</style>
</head>
<body>
<div>
<p>This paragraph is inside div and red.</p>
</div>
<p>This paragraph is outside div and normal color.</p>
</body>
</html>
7. Child Selector
Definition: Selects only direct child elements of a parent.
Example Program:
<!DOCTYPE html>
<html>
<head>
<style>
div > p {
font-weight: bold;
}
</style>
</head>
<body>
<div>
<p>This is a bold paragraph (direct child).</p>
<section>
<p>This is a normal paragraph (not direct child).</p>
</section>
</div>
</body>
</html>
6. Box Model
What is the CSS Box Model?
Every HTML element is treated like a box, and it’s made up of 4 layers around the content.
4. Margin – Space outside the border, separating the element from others.
Each element’s content, padding, border and margin properties can be adjusted. All the time, padding, border
and margin properties are applied to each element implicitly or explicitly.
Example Program:
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 300px;
padding: 20px;
border: 5px solid blue;
margin: 30px;
background-color: lightgray;
}
</style>
</head>
<body>
<div class="box">
This is a box demonstrating the CSS Box Model.<br> Content → Padding → Border →
Margin
</div>
</body>
</html>
HTML elements are categorized as block-level elements and inline elements.
Block-level elements by default start on a new line and occupies full page width. Width and height properties
are respected to block-level elements. Using display property, block-level elements can be converted to block-
inline, inline, etc., elements, Few block-level elements are <div>, <p>, <pre>, <h1>,<h2>,<ul>,<ol>.
Inline elements by default occupies only required width based on content size. Width and height properties are
not respected/applicable to inline elements. Using display property, inline elements can be converted to block,
block-inline, etc., elements. Few inline elements are <a>, <span>, <img>, <i>.
1. Block-level elements by default start on a new line and occupies full page width. But, Inline elements by
default occupies only required width based on content size.
2. Width and height properties are applicable to block-level element but not to inline elements.
3. Inline elements can be placed inside the block-level element, but block-level elements can not be
placed inside the inline element.
[Link]-top
[Link]-bottom
[Link]-top
[Link]-bottom
Following example shows box model (on right side top) diagram for left side specified code.
7. CSS Text
1. Text Color
p{
color: blue;
}
2. Text Alignment
h1 {
text-align: center;
}
p{
text-align: justify;
}
3. Text Decoration
a{
text-decoration: none; /* removes underline */
}
h2 {
text-decoration: underline;
}
p{
text-decoration: line-through;
}
4. Text Transformation
h1 {
text-transform: uppercase;
}
p{
text-transform: capitalize;
}
5. Text Spacing
h1 {
text-shadow: 2px 2px 5px gray; /* horizontal, vertical, blur, color */
}
Example Program (All in One)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: darkblue;
text-align: center;
text-transform: uppercase;
text-shadow: 2px 2px 5px gray;
}
h2 {
text-decoration: underline;
text-align: left;
color: darkgreen;
}
p{
color: purple;
text-align: justify;
letter-spacing: 2px;
word-spacing: 5px;
line-height: 1.6;
text-indent: 40px;
}
</style>
</head>
<body>
<h1>CSS Text Example</h1>
<h2>Subheading Styled with CSS</h2>
<p>
This is a paragraph to demonstrate different CSS text properties.
Notice the spacing, alignment, indentation, and decoration applied
to the content using CSS.
</p>
</body>
</html>
8. CSS Font Properties
Fonts make text readable and attractive. CSS gives us control over type, size, style, and weight.
1. font-family
p{
font-family: Arial, sans-serif;
}
2. font-size
h1 {
font-size: 36px;
}
p{
font-size: 18px;
}
3. font-style
p{
font-style: italic;
}
4. font-weight
h2 {
font-weight: bold;
}
p{
font-weight: 300;
}
5. font-variant
p{
font-variant: small-caps;
}
Example Program (All Font Properties)
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
font-family: "Times New Roman", serif;
font-size: 36px;
font-style: italic;
font-weight: bold;
text-align: center;
}
h2 {
font-family: Verdana, sans-serif;
font-size: 28px;
font-weight: 600;
color: darkblue;
}
p{
font-family: Arial, sans-serif;
font-size: 18px;
font-style: normal;
font-variant: small-caps;
line-height: 1.6;
}
</style>
</head>
<body>
<h1>CSS Font Example</h1>
<h2>Subheading Styled with Fonts</h2>
<p>
This is a paragraph demonstrating different CSS font properties.
Notice the font family, size, weight, and variant applied to the content.
</p>
</body>
</html>
9. CSS List Properties
1. list-style-type
ol {
list-style-type: upper-roman;
}
2. list-style-position
• Controls whether the bullet/number appears inside or outside the text block.
ul {
list-style-position: inside;
}
3. list-style-image
ul {
list-style-image: url("[Link]");
}
Example Program (CSS Lists)
<html>
<head>
<style>
/* Unordered List */
ul {
list-style-type: square;
list-style-position: inside;
color: darkblue;
font-size: 18px;
}
/* Ordered List */
ol {
list-style-type: upper-roman;
list-style-position: outside;
color: darkgreen;
font-size: 18px;
}
</style>
</head>
<body>
<h2>Unordered List Example</h2>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>JavaScript</li>
</ul>
<h2>Ordered List Example</h2>
<ol>
<li>Introduction</li>
<li>Chapters</li>
<li>Conclusion</li>
</ol>
</body>
</html>
relative
• The element is positioned relative to its normal position.
• You can use top, left, right, and bottom to move it from the default spot.
position: relative;
top: 20px; /* Moves element 20px down */
left: 30px; /* Moves element 30px to the right */
absolute
• The element is positioned relative to its nearest positioned ancestor (non-static).
• If no ancestor has a position, it is relative to the document body.
position: absolute;
top: 50px;
left: 100px;
fixed
• The element is positioned relative to the browser window (viewport).
• Stays fixed when the page scrolls.
position: fixed;
top: 10px;
right: 10px;
Example Program – All Position Types
<!DOCTYPE html>
<html>
<head>
<style>
.static-box {
position: static;
background-color: lightblue;
padding: 10px;
margin-bottom: 20px;
}
.relative-box {
position: relative;
top: 20px;
left: 30px;
background-color: lightgreen;
padding: 10px;
margin-bottom: 20px;
}
.absolute-box {
position: absolute;
top: 150px;
left: 50px;
background-color: lightcoral;
padding: 10px;
}
.fixed-box {
position: fixed;
top: 10px;
right: 10px;
background-color: lightgoldenrodyellow;
padding: 10px;
}
</style>
</head>
<body>
<div class="static-box">
Static Position (Normal Flow)
</div>
<div class="relative-box">
Relative Position (Moved from normal position)
</div>
<div class="absolute-box">
Absolute Position (Based on document or nearest positioned parent)
</div>
<div class="fixed-box">
Fixed Position (Stays at top-right even when scrolling)
</div>
</body>
</html>
➤ What is it?
Syntax:
selector {
border-radius: value;
}
• value → Can be in pixels (px) or percentage (%).
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightblue;
border: 2px solid blue;
border-radius: 15px; /* Rounded corners */
}
</style>
</head>
<body>
<h2>CSS Rounded Corners Example</h2>
<div class="box">
This box has rounded corners.
</div>
</body>
</html>
.custom-box {
width: 200px;
height: 100px;
background-color: lightgreen;
border: 2px solid green;
border-radius: 10px 20px 30px 40px;
}
ORDER ---> top-left top-right bottom-right bottom-left
Value Result
CSS allows us to add shadow effects to both text and boxes (elements) using two main properties:
1. box-shadow
➤ Syntax:
selector {
box-shadow: horizontal-offset vertical-offset blur-radius color;
}
➤ Example:
div {
width: 200px;
height: 100px;
background-color: lightblue;
box-shadow: 5px 5px 10px gray;
}
The box gets a shadow 5px to the right, 5px down, with a blur of 10px.
<html>
<head>
<style>
.box {
width: 200px;
height: 100px;
background-color: lightgreen;
box-shadow: 10px 10px 15px rgba(0,0,0,0.5);
margin: 20px;
}
</style>
</head>
<body>
<h2>CSS Box Shadow Example</h2>
<div class="box">
This box has a shadow effect.
</div>
</body>
</html>
3. text-shadow
Adds shadow to text.
➤ Syntax:
selector {
text-shadow: horizontal-offset vertical-offset blur-radius color;
}
➤ Example:
h1 {
text-shadow: 2px 2px 5px gray;
}
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
text-shadow: 3px 3px 5px black;
color: orange;
}
</style>
</head>
<body>
<h1>Shadowed Text Example</h1>
</body>
</html>
Summary – Shadow Effects
Property Purpose
2D Transforms let us move, rotate, scale, or skew HTML elements in 2-dimensional space.
div {
transform: translate(50px, 30px); /* Moves right by 50px and down by 30px */
}
2. rotate() – Rotate Element
div {
transform: rotate(45deg); /* Rotates by 45 degrees */
}
3. scale() – Resize Element
div {
transform: scale(1.5); /* Increases size by 1.5x */
}
4. skew() – Skew Element
div {
transform: skew(20deg, 10deg); /* Skews horizontally by 20°, vertically by 10° */
}
Example Program (All Transforms Together)
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px;
height: 100px;
background-color: lightcoral;
margin: 20px;
text-align: center;
line-height: 100px;
color: white;
}
/* Translate Example */
.translate {
transform: translate(50px, 30px);
}
/* Rotate Example */
.rotate {
transform: rotate(45deg);
}
/* Scale Example */
.scale {
transform: scale(1.5);
}
/* Skew Example */
.skew {
transform: skew(20deg, 10deg);
}
</style>
</head>
<body>
<h2>CSS 2D Transform Examples</h2>
<div class="box translate">Translate</div>
<div class="box rotate">Rotate</div>
<div class="box scale">Scale</div>
<div class="box skew">Skew</div>
</body>
</html>
3D Transforms let us move, rotate, or scale HTML elements in 3-dimensional space (along X, Y, and Z axes).
div {
transform: rotateX(45deg);
}
Rotates the element around the horizontal (X) axis.
div {
transform: rotateY(45deg);
}
Rotates the element around the vertical (Y) axis.
div {
transform: rotateZ(45deg);
}
Rotates the element around the depth (Z) axis (similar to 2D rotation).
4. perspective
• Sets the distance between the viewer and the 3D object → important for realistic 3D effect.
.container {
perspective: 500px;
}
<!DOCTYPE html>
<html>
<head>
<style>
.container {
perspective: 600px;
}
.box {
width: 150px;
height: 150px;
background-color: lightcoral;
margin: 50px;
text-align: center;
line-height: 150px;
color: white;
font-size: 20px;
}
/* Rotate X */
.box1 {
transform: rotateX(45deg);
}
/* Rotate Y */
.box2 {
transform: rotateY(45deg);
}
/* Rotate Z */
.box3 {
transform: rotateZ(45deg);
}
</style>
</head>
<body>
<h2>CSS 3D Transforms Example</h2>
<div class="container">
<div class="box box1">RotateX</div>
<div class="box box2">RotateY</div>
<div class="box box3">RotateZ</div>
</div>
</body>
</html>
✅ Summary Table
Transform Effect
rotateX(45deg) Tilts element forward or backward
CSS Transitions allow properties of an element to change smoothly over time instead of changing instantly.
✅ Basic Syntax
selector {
transition: property duration timing-function delay;
}
• property → Which CSS property to animate (e.g., color, width, background-color, etc.).
• duration → Time the transition takes (e.g., 1s, 0.5s).
• timing-function → Speed curve (ease, linear, ease-in, ease-out).
• delay → Time before the transition starts (optional).
<!DOCTYPE html>
<html>
<head>
<style>
button {
background-color: lightblue;
padding: 15px 30px;
font-size: 18px;
border: none;
cursor: pointer;
button:hover {
background-color: lightgreen;
}
</style>
</head>
<body>
<h2>CSS Transition Example</h2>
<button>Hover Me!</button>
</body>
</html>
When you hover the button, the background color changes smoothly from lightblue to lightgreen in 0.5
seconds.
Example 2 – Smooth Width Change
div {
width: 150px;
height: 100px;
background-color: tomato;
transition: width 1s ease;
}
div:hover {
width: 300px;
}
<div></div>
On hover, the box’s width grows from 150px to 300px smoothly over 1 second.
✅ Quick Summary
1. Key Concepts
• @keyframes → Defines the animation steps (from → to or percentage steps).
• animation-name → Name of the keyframes animation.
• animation-duration → How long the animation takes (e.g., 2s).
• animation-iteration-count → Number of times the animation runs (e.g., infinite for continuous
animation).
• animation-timing-function → Speed curve (ease, linear, etc.).
Example 1 – Simple Animation (Move a Box)
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes slide {
from { left: 0px; }
to { left: 300px; }
}
.box {
width: 100px;
height: 100px;
background-color: orange;
position: relative;
animation-name: slide;
animation-duration: 3s;
animation-iteration-count: infinite;
}
</style>
</head>
<body>
<h2>CSS Animation Example</h2>
<div class="box"></div>
</body>
</html>
The box will slide from left to right repeatedly.
Example 2 – Changing Color Animation
@keyframes colorChange {
0% { background-color: red; }
50% { background-color: yellow; }
100% { background-color: green; }
}
.box {
width: 150px;
height: 150px;
animation: colorChange 5s infinite;
}
<div class="box"></div>
The box will change colors smoothly every 5 seconds.
Example:
.box {
animation: slide 3s linear 1s infinite alternate;
}
Summary Table
Property Purpose
Property Purpose