CSS Notes -

CSS and its Syntax
CSS stands for Cascading Style Sheets.
It controls the visual presentation (style, layout, color, fonts, spacing, etc.) of HTML elements.
Why Use CSS?
Without CSS:
<h1>Hello</h1>
With CSS:
<h1 style="color: blue; font-family: Arial;">Hello</h1>
3 Ways to Add CSS
Method | How to Use | Example |
Inline | Inside the HTML tag | <p style="color:red;">Text</p> |
Internal | Inside a <style> tag in <head> | <style>p { color: red; }</style> |
External โ | Linked .css file | <link rel="stylesheet" href="styles.css"> |
CSS Syntax
selector {
property: value;
}
โ Example:
h1 {
color: blue;
font-size: 24px;
}
This means make all <h1>
elements blue and 24px in size.
CSS Selectors
Selectors target which elements to style.
๐น 1. Universal Selector (*
)
Targets all elements.
* {
margin: 0;
padding: 0;
}
๐น 2. Element Selector
Targets a specific HTML tag.
p {
color: green;
}
๐น 3. Class Selector (.
)
Targets elements with a specific class.
<p class="highlight">Hello</p>
.highlight {
background-color: yellow;
}
๐น 4. ID Selector (#
)
Targets an element with a unique ID.
<h1 id="main-title">Welcome</h1>
#main-title {
font-size: 30px;
}
๐น 5. Group Selector
Style multiple elements at once.
h1, h2, p {
font-family: Arial;
}
๐น 6. Descendant Selector
Select elements inside other elements.
article p {
color: gray;
}
Applies to all <p>
inside <article>
.
๐น 7. Child Selector (>
)
Selects direct children only.
ul > li {
list-style-type: square;
}
Specificity (Which Style Wins?)
Inline style
Internal style
External style
CSS Comment
CSS comments are lines of text that are ignored by the browser. They're used to explain code, leave notes, or temporarily disable CSS rules during development.
โ Syntax:
/* This is a CSS comment */
Comments start with
/*
and end with*/
.They can span multiple lines.
๐ Examples:
/* This sets the background color of the body */
body {
background-color: #f0f0f0;
}
/*
This section styles the navigation bar.
It includes padding and font color.
*/
nav {
padding: 10px;
color: white;
}
CSS Colors
โ 1. Color Names
CSS supports 147 predefined color names, like:
color: red;
color: blue;
color: lightgreen;
color: darkslategray;
Easy to use, but limited flexibility.
โ 2. HEX (Hexadecimal) Color
HEX is a base-16 value that defines red, green, and blue (RGB) using 2 hex digits each.
color: #RRGGBB;
#FF0000
โ red#00FF00
โ green#0000FF
โ blue#FFFFFF
โ white#000000
โ black
Short form also works: #FFF
(same as #FFFFFF
)
โ 3. RGB (Red, Green, Blue)
Each color is defined using integer values (0โ255) for red, green, and blue:
color: rgb(255, 0, 0); /* Red */
color: rgb(0, 255, 0); /* Green */
color: rgb(0, 0, 255); /* Blue */
โ 4. RGBA (RGB + Alpha Transparency)
Same as rgb()
, but adds alpha (opacity) as a value from 0
(transparent) to 1
(opaque):
color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
CSS Background
The background
property in CSS is used to set the background effects for elements. It can control background color, images, repeat behavior, positioning, and more.
โ
1. background-color
Sets the background color of an element.
background-color: lightblue;
โ
2. background-image
Sets a background image using a URL.
background-image: url("image.jpg");
๐ Can be used with gradients too:
background-image: linear-gradient(to right, red, yellow);
โ
3. background-repeat
Controls if/how the image repeats.
background-repeat: repeat; /* Default */
background-repeat: no-repeat; /* Show once */
background-repeat: repeat-x; /* Horizontal only */
background-repeat: repeat-y; /* Vertical only */
โ
4. background-position
Sets the starting position of the background image.
background-position: center;
background-position: top right;
background-position: 50% 50%;
โ
5. background-attachment
Defines whether the background scrolls with the page.
background-attachment: scroll; /* Default */
background-attachment: fixed; /* Stays in place */
โ
6. background
(Shorthand)
You can combine all background properties in a single line:
background: url("bg.jpg") no-repeat center/cover fixed;
Format:
background: [color] [image] [repeat] [position]/[size] [attachment];
CSS Units
CSS Units โ Absolute & Relative
โ Absolute Units
These units are fixed and do not scale with screen size or parent elements. Best for print or very controlled layouts.
Unit | Description | Example |
px | Pixels (screen dots) | width: 100px; |
pt | Points (1pt = 1/72 inch) | font-size: 12pt; |
cm | Centimeters | margin: 2cm; |
mm | Millimeters | padding: 10mm; |
in | Inches (1in = 2.54cm) | width: 1in; |
pc | Picas (1pc = 12pt) | font-size: 1pc; |
Use Case: Print styles, very specific layouts.
โ Relative Units
These units scale depending on parent elements or the viewport. Best for responsive web design.
Unit | Relative To | Example |
% | Parent element | width: 50%; |
em | Font size of the parent | padding: 2em; |
rem | Font size of the root (<html> ) | font-size: 1.5rem; |
vw | 1% of the viewport width | width: 80vw; |
vh | 1% of the viewport height | height: 50vh; |
vmin | 1% of the smaller of vw or vh | font-size: 5vmin; |
vmax | 1% of the larger of vw or vh | padding: 3vmax; |
ch | Width of the character "0" | width: 40ch; |
ex | x-height (height of lowercase "x") | line-height: 2ex; |
Use Case: Responsive layouts, scaling text, flexible designs.
โ Quick Comparison:
Scenario | Use Unit |
Responsive layout | % , vw , vh , rem |
Font size (scalable) | em , rem |
Fixed-size elements | px |
Print layout | cm , pt , in |
Example:
html {
font-size: 16px; /* 1rem = 16px */
}
.container {
width: 80vw;
padding: 2em;
font-size: 1.2rem;
}
CSS Fonts
The font
properties in CSS let you control the style, size, weight, family, and more for the text on your web page.
โ
1. font-family
Specifies the typeface (the font). Always provide fallback fonts.
font-family: 'Roboto', Arial, sans-serif;
Custom font:
'Roboto'
(loaded via @font-face or Google Fonts)Fallback:
Arial
Generic family:
sans-serif
๐ฏ Common generic families:
serif
โ like Times New Romansans-serif
โ like Arialmonospace
โ like Couriercursive
,fantasy
,system-ui
โ
2. font-size
Defines the size of the text.
font-size: 16px;
font-size: 1.2em;
font-size: 2rem;
Relative units (em
, rem
, %
) make your text responsive.
โ
3. font-weight
Controls the boldness of the font.
font-weight: normal; /* 400 */
font-weight: bold; /* 700 */
font-weight: 100โ900; /* Numeric scale */
โ
4. font-style
Defines the style (italic or normal).
font-style: normal;
font-style: italic;
font-style: oblique;
โ
5. font-variant
Used for small caps:
font-variant: small-caps;
CSS Text
These properties help you control the appearance and layout of text, including alignment, spacing, decoration, and transformation.
โ
1. color
Sets the text color.
color: red;
color: #333;
color: rgb(255, 0, 0);
โ
2. text-align
Aligns the text horizontally.
text-align: left; /* Default */
text-align: right;
text-align: center;
text-align: justify;
โ
3. text-decoration
Adds decorations like underline, line-through, etc.
text-decoration: none;
text-decoration: underline;
text-decoration: line-through;
text-decoration: overline;
Combine with color:
text-decoration: underline red;
โ
4. text-transform
Changes the case of the text.
text-transform: none;
text-transform: uppercase;
text-transform: lowercase;
text-transform: capitalize;
โ
5. letter-spacing
Adjusts space between characters.
letter-spacing: 2px;
โ
6. word-spacing
Adjusts space between words.
word-spacing: 5px;
โ
7. line-height
Controls space between lines of text.
line-height: 1.5;
โ 8. Text Shadow
Adds shadow to text.
text-shadow: 2px 2px 5px gray;
What is box-sizing
in CSS?
The box-sizing
property defines how the total width and height of an element is calculated โ whether or not it includes padding and border.
By Default (content-box
):
box-sizing: content-box;
Only the content is counted in the width and height.
Padding and border are added outside the defined width/height.
Example:
width: 300px;
padding: 20px;
border: 10px solid black;
Total width = 300 + 20*2 + 10*2 = 360px
Total height = same calculation
โ
box-sizing: border-box;
box-sizing: border-box;
The width and height include content + padding + border.
No need to manually calculate extra space.
Example:
width: 300px;
padding: 20px;
border: 10px solid black;
Here, total width stays 300px.
Content area is automatically shrunk to fit the padding and border.
๐ Why Use border-box
?
Keeps layout simple and predictable โ
Prevents overflow and alignment issues โ
Widely used in modern CSS frameworks โ
Best Practice (Set Globally):
* {
box-sizing: border-box;
}
This sets all elements (including pseudo-elements) to calculate width and height in a consistent, easier-to-manage way.
Box Model
The CSS box model is essentially a box that wraps around every HTML element. It consists of: content, padding, borders and margins.
In CSS, the term "box model" is used when talking about design and layout.
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
๐น 1. Content Area
- Controls the actual content's dimensions.
Property | Description | Example |
width | Sets the width of the content | width: 300px; |
height | Sets the height of the content | height: 150px; |
min-width , max-width | Set limits on width | max-width: 500px; |
min-height , max-height | Set limits on height | min-height: 100px; |
overflow | Controls content overflow | overflow: hidden; |
display | Affects box behavior | display: block; |
๐น 2. Padding Area
- Space between content and border.
Property | Description | Example |
padding | Shorthand for all sides | padding: 20px; |
padding-top | Top padding | padding-top: 10px; |
padding-right | Right padding | padding-right: 15px; |
padding-bottom | Bottom padding | padding-bottom: 10px; |
padding-left | Left padding | padding-left: 15px; |
๐น 3. Border Area
- The visible edge surrounding padding and content.
Property | Description | Example |
border | Shorthand for width, style, color | border: 1px solid black; |
border-width | Controls thickness | border-width: 2px; |
border-style | Controls style (solid, dashed, etc.) | border-style: dashed; |
border-color | Sets the border color | border-color: red; |
border-top , border-right , border-bottom , border-left | Individual borders | border-top: 2px dotted blue; |
border-radius | Rounds corners | border-radius: 10px; |
๐น 4. Margin Area
- Space outside the border, between this element and others.
Property | Description | Example |
margin | Shorthand for all sides | margin: 10px; |
margin-top | Top margin | margin-top: 5px; |
margin-right | Right margin | margin-right: 10px; |
margin-bottom | Bottom margin | margin-bottom: 5px; |
margin-left | Left margin | margin-left: 10px; |
margin: auto | Centers an element (used with width ) | margin: auto; |
๐น 5. Box Behavior / Control
- Properties that influence how the box behaves.
Property | Description | Example |
box-sizing | Defines how width/height are calculated | box-sizing: border-box; |
display | Block, inline, flex, grid, etc. | display: flex; |
visibility | Show/hide the box | visibility: hidden; |
overflow | What happens if content overflows | overflow: scroll; |
outline | A line outside the border (doesnโt affect box size) | outline: 2px dotted red; |
โ Box Model Shorthand Example
.box {
width: 200px;
height: 150px;
padding: 20px;
margin: 10px;
border: 2px solid black;
box-sizing: border-box;
}
Display in CSS
The display
property specifies the display behavior (the type of rendering box) of an element.
โ
block
Takes up 100% width of the parent (whole line).
Always starts on a new line.
You can set
width
andheight
.
Example elements: <div>
, <p>
, <h1>
p {
display: block;
}
Makes the paragraph take full width and start on a new line.
โ
inline
Takes up only as much width as needed.
Sits side by side with other inline elements.
You cannot set
width
orheight
.
Example elements: <span>
, <a>
, <strong>
a {
display: inline;
}
Makes the anchor behave like text โ no line break, no width/height control.
โ
inline-block
Behaves like
inline
(sits next to others).But you can set
width
andheight
.
Best of both inline
and block
.
button {
display: inline-block;
width: 150px;
height: 40px;
}
Button stays inline with others but can have a set width and height.
Common Use Cases
Task | Use this display |
Stack items vertically | block |
Place items side-by-side | inline or inline-block |
Custom buttons or links with padding & width | inline-block |
CSS Gradients
Gradients let you transition between two or more colors smoothly โ and they're generated by the browser, so no image files needed!
โ 1. Linear Gradient
Color changes in a straight line (top to bottom by default).
background: linear-gradient(direction, color1, color2, ...);
Examples:
background: linear-gradient(to right, red, blue);
/* From left to right */
background: linear-gradient(to bottom right, #ff7e5f, #feb47b);
/* Diagonal gradient */
background: linear-gradient(45deg, #00c6ff, #0072ff);
/* Using degree */
โ 2. Radial Gradient
Color radiates outward from a central point (like a circle or ellipse).
background: radial-gradient(shape size at position, color1, color2, ...);
Examples:
background: radial-gradient(circle, red, yellow, green);
background: radial-gradient(ellipse at center, #ff9a9e, #fad0c4);
Box Shadow & Drop Shadow
box-shadow
(for elements)
Adds shadow around the border of an element โ often used to create depth or elevation.
โ Syntax:
box-shadow: offset-x offset-y blur-radius spread-radius color;
offset-x
โ Horizontal position (right if positive, left if negative)offset-y
โ Vertical position (down if positive, up if negative)blur-radius
(optional) โ Soften the edgesspread-radius
(optional) โ Expand or shrink the shadowcolor
โ Shadow color
Example:
box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);
Multiple Shadows:
You can stack shadows with commas:
box-shadow: 2px 2px 4px red, -2px -2px 4px blue;
Inset Shadow:
To apply shadow inside the element:
box-shadow: inset 2px 2px 5px rgba(0,0,0,0.5);
filter: drop-shadow()
(for images, SVGs, elements)
drop-shadow()
is part of the filter
property and applies a shadow to the visual content (not the box), useful especially for transparent images, text, or SVGs.
โ Syntax:
filter: drop-shadow(offset-x offset-y blur-radius color);
No spread-radius
, but it respects transparent areas of PNGs or SVGs!
Example:
filter: drop-shadow(4px 4px 6px rgba(0,0,0,0.5));
Key Differences
Feature | box-shadow | drop-shadow() |
Used on | Any HTML element | Elements, images, SVGs |
Applies to | The box itself (border area) | The rendered image or content |
Spread control | Yes | No |
Inset option | Yes (inset ) | No |
Transparent images | No proper shadow | โ Perfect for transparent PNG/SVG |
Multiple shadows | โ With commas | โ Only with multiple filters |
List in CSS
Used to style HTML lists (<ul>
, <ol>
, <li>
).
โ
1. list-style-type
Defines the type of marker (bullet or number) for list items.
ul {
list-style-type: circle;
}
Common values:
disc
(default bullet)circle
square
decimal
(numbers for<ol>
)none
(no bullet)
โ
2. list-style-position
Sets where the marker appears โ inside or outside the list item's content.
ul {
list-style-position: inside;
}
outside
(default) โ bullet is outside the content boxinside
โ bullet is inside the content box
โ
3. list-style-image
Replaces the default marker with a custom image.
ul {
list-style-image: url('bullet.png');
}
Anchor Link States (Pseudo-classes)
Anchor tags (<a>
) have 4 main states, often remembered as LVHA:
โ
1. :link
- Unvisited link (default state)
a:link {
color: blue;
}
โ
2. :visited
- Link that the user has already clicked
a:visited {
color: purple;
}
โ
3. :hover
- When the user hovers over the link
a:hover {
text-decoration: underline;
color: red;
}
โ
4. :active
- When the link is being clicked (mouse down)
a:active {
color: orange;
}
Order Matters!
Always use this order:
a:link
a:visited
a:hover
a:active
(LVHA โ Link, Visited, Hover, Active)
Otherwise, some states might not work as expected.
Pseudo Elements in CSS
1. ::before
- Inserts content before the elementโs actual content.
h1::before {
content: "๐ฅ ";
}
2. ::after
- Inserts content after the elementโs content.
h1::after {
content: " โจ";
}
You must use content:
with ::before
and ::after
.
3. ::first-letter
- Styles the first letter of a block of text.
p::first-letter {
font-size: 2rem;
color: red;
}
4. ::first-line
- Styles the first line of text in a block (depends on screen width).
p::first-line {
font-weight: bold;
}
Position in CSS
The position
property defines how an element is positioned in the document flow.
1. static
(default)
Normal document flow
You canโt use
top
,right
,bottom
,left
Used when no special positioning is needed.
Most HTML elements are
static
by default.
๐ Use when: You donโt need to move the element manually.
div {
position: static;
}
2. relative
Element stays in its normal position
You can move it with
top
,left
, etc. relative to itselfUse when you want to move an element slightly from its normal position.
Can also be used as a reference for
absolute
children.
๐ Use when:
You want to nudge an element without breaking layout.
You want to position a tooltip or badge over a parent element.
Tip: Combine with z-index
to bring things forward or behind.
div {
position: relative;
top: 10px;
left: 20px;
}
3. absolute
Removed from normal flow
Positioned relative to the nearest positioned ancestor
If no positioned ancestor โ relative to
<html>
orbody
Use when you want to place something at a precise position.
It removes the element from the normal layout.
๐ Use when:
Creating dropdown menus.
Positioning tooltips or popups.
Designing modal dialogs or floating boxes.
๐ก Needs a parent with position: relative
or it will stick to the <body>
.
div {
position: absolute;
top: 0;
left: 0;
}
4. fixed
Stays in one place even when scrolling
Positioned relative to the viewport
Use to pin an element to a specific spot on the screen โ even when scrolling.
๐ Use when:
You want a floating button (like a chat icon).
Creating sticky footers/headers that never move.
Displaying persistent alerts or banners.
div {
position: fixed;
bottom: 10px;
right: 10px;
}
5. sticky
Switches between
relative
andfixed
based on scrollSticks to a position when a threshold is reached
Use to make elements stick temporarily while scrolling, then release.
๐ Use when:
Creating sticky headers or section titles.
Sidebar menus that scroll with the content up to a point.
๐ก Needs a top
, left
, or bottom
value and scrollable parent.
div {
position: sticky;
top: 0;
}
CSS z-index
Property
Controls the stacking order of elements
Higher
z-index
= placed on topWorks only with positioned elements (
relative
,absolute
,fixed
,sticky
)Use when elements overlap and you want to control which one appears on top.
๐ Use when:
Creating modals, popups, or tooltips.
Fixing dropdown menus behind other elements.
Overlapping images or buttons.
๐ข Higher z-index
means it sits on top.
div {
position: absolute;
z-index: 10;
}
Example:
.box1 {
z-index: 1;
}
.box2 {
z-index: 2;
}
box2
will appear above box1
.
Summary Table
Position Type | In Normal Flow? | Can Use Top/Left? | Relative To |
static | โ | โ | N/A |
relative | โ | โ | Itself |
absolute | โ | โ | Nearest ancestor with position |
fixed | โ | โ | Viewport |
sticky | โ (until sticky) | โ | Scroll position |
Flex Box -
Flexbox (Flexible Box) is a one-dimensional layout system that helps you align and distribute space among items in a single directionโeither row or column.
Basic Terminology
Flex Container: The parent element with
display: flex
.Flex Items: The child elements inside the flex container.
Enabling Flex
<div class="flex-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.flex-container {
display: flex;
background-color: #ddd;
}
.item {
background-color: #4CAF50;
color: white;
padding: 20px;
margin: 5px;
}
Main Axis vs Cross Axis
Main Axis = direction of flex (default: row โ horizontal)
Cross Axis = perpendicular to main axis
Common Properties on Container
1. flex-direction
Controls main axis direction.
.flex-container {
display: flex;
flex-direction: row; /* default */
}
Other options:
row-reverse
column
column-reverse
2. justify-content
Aligns items on the main axis.
.flex-container {
justify-content: center;
}
Other options:
flex-start
flex-end
space-between
space-around
space-evenly
3. align-items
Aligns items on the cross axis.
cssCopyEdit.flex-container {
align-items: center;
}
Options:
flex-start
flex-end
stretch
baseline
4. flex-wrap
Makes items wrap to next line.
.flex-container {
flex-wrap: wrap;
}
Properties on Flex Items
1. flex
Shorthand for flex-grow
, flex-shrink
, flex-basis
.
.item {
flex: 1;
}
โ Makes all items share available space equally.
2. order
Controls display order.
.item {
order: 2;
}
Key Flexbox Use Cases
โ
Navigation bars
โ
Centering elements
โ
Equal height columns
GRID
CSS Grid is a two-dimensional layout system for building complex layouts with rows and columns.
Basic Terminology
Grid Container: Parent with
display: grid
Grid Items: Children of grid container
Enabling Grid
<div class="grid-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
.grid-container {
display: grid;
grid-template-columns: 100px 100px;
grid-template-rows: 100px 100px;
gap: 10px;
}
.item {
background-color: #2196F3;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
โ Result: 2x2 grid with 10px gaps.
โ
Define Columns with grid-template-columns
grid-template-columns: 1fr 2fr;
fr
= fraction of available space
โ Define Rows
grid-template-rows: 100px 200px;
โ
gap
Space between grid cells.
gap: 20px;
โ Placing Items
.item1 {
grid-column: 1 / 3;
}
โ Makes item span 2 columns.
Key Grid Use Cases
โ
Page layouts
โ
Photo galleries
โ
Complex, two-dimensional designs
CSS Animation
Animation in CSS allows elements to move, change color, grow/shrink, rotate, and more โ without JavaScript.
Two Ways to Animate in CSS
Transitions โ Simple changes between two states (hover, focus, etc.)
Animations โ More control, keyframes, multiple steps
CSS TRANSITIONS
A transition is used to animate properties smoothly when they change (e.g., width
, background
, transform
, etc.)
Example: Button Hover
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s;
}
div:hover {
width: 300px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Common timing functions: ease
, linear
, ease-in
, ease-out
linear - Same speed throughout
ease - Slow start โ Fast middle โ Slow end
ease-in - Slow start โ Fast end
ease-out - Slow end
CSS ANIMATIONS
CSS @keyframes
define how an element will animate step-by-step.
Example: Bouncing Ball
<!DOCTYPE html>
<html>
<head>
<style>
.ball {
width: 50px;
height: 50px;
background-color: red;
border-radius: 50%;
position: relative;
animation: bounce 1s infinite;
}
@keyframes bounce {
0% { top: 0; }
50% { top: 100px; }
100% { top: 0; }
}
</style>
</head>
<body>
<div class="ball"></div>
</body>
</html>
animation-name
,animation-duration
,animation-iteration-count
,animation-timing-function
infinite
= keeps repeating
Animation Properties
Property | Description |
animation-name | Name of the @keyframes |
animation-duration | Time it takes for one cycle |
animation-timing-function | Speed curve (ease , linear , etc.) |
animation-delay | Wait time before starting |
animation-iteration-count | How many times it runs |
animation-direction | Normal, reverse, alternate |
animation-fill-mode | forwards , backwards , etc. |
Example: Fading Text In and Out
<!DOCTYPE html>
<html>
<head>
<style>
.fade {
font-size: 24px;
animation: fadeinout 2s infinite;
}
@keyframes fadeinout {
0% { opacity: 0; }
50% { opacity: 1; }
100% { opacity: 0; }
}
</style>
</head>
<body>
<p class="fade">I fade in and out!</p>
</body>
</html>
Subscribe to my newsletter
Read articles from Koustav Moitra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
