CSS Notes
CSS Notes
CSS Notes
CSS (Cascading Style Sheets) -
Introduction
CSS (Cascading Style Sheets) -
Introduction
What is CSS?
CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation and
layout of HTML documents.
Types of CSS
1. Inline CSS (applies styles directly within an element)
<p style="color: blue; font-size: 20px;">This is inline
CSS</p>
2. Internal CSS (written inside a <style> tag in the HTML <head>)
<style>
p {
color: blue;
font-size: 20px;
}
</style>
css
p {
color: blue;
font-size: 20px;
}
CSS Selectors
● Element Selector: p { color: red; }
● Class Selector: .title { font-weight: bold; }
● ID Selector: #main { background-color: yellow; }
● Universal Selector: * { margin: 0; }
● Group Selector: h1, h2, p { font-family: Arial; }
● Attribute Selector: input[type="text"] { border: 1px solid gray; }
CSS Properties
● Text & Font Styling
color, font-size, font-family, font-weight, text-align,
text-decoration
● Box Model
margin, padding, border, width, height
● Backgrounds
background-color, background-image, background-size
● Positioning
position, display, float, flexbox, grid
1. Basic Selectors
css
* {
margin: 0;
padding: 0;
}
css
p {
color: blue;
}
css
.title {
font-size: 20px;
color: red;
}
Usage in HTML:
html
css
#main {
background-color: yellow;
}
Usage in HTML:
html
<div id="main">Main Content</div>
css
h1, h2, p {
font-family: Arial, sans-serif;
}
css
div p {
color: green;
}
Applies to <p> inside <div>.
css
div > p {
font-weight: bold;
}
css
h1 + p {
color: blue;
}
css
h1 ~ p {
font-style: italic;
}
3. Attribute Selectors
(a) Elements with a Specific Attribute
css
input[type="text"] {
border: 1px solid gray;
}
(b) Attribute Contains (*=)
css
a[href*="example"] {
color: red;
}
css
a[href^="https"] {
color: green;
}
css
img[src$=".png"] {
border-radius: 10px;
}
CSS Comments
CSS Comments
What are CSS Comments?
CSS comments are used to add explanations or notes within the stylesheet. They help
developers understand the code better and make it easier to maintain.
Example:
css
/*
This is a multi-line comment
It can span multiple lines
*/
h1 {
font-size: 24px;
}
/*
h2 {
color: red;
}
*/
css
p {
color: red;
}
css
p {
color: #ff5733; /* Red-Orange */
}
h1 {
color: #f06; /* Equivalent to #ff0066 */
}
(c) RGB Colors
RGB defines a color using Red, Green, and Blue values (0-255).
css
p {
color: rgb(255, 87, 51); /* Red-Orange */
}
css
p {
color: rgba(255, 87, 51, 0.5); /* 50% transparent */
}
HSL defines colors using hue (0-360°), saturation (%), and lightness (%).
css
p {
color: hsl(15, 100%, 50%); /* Red-Orange */
}
p {
color: hsla(15, 100%, 50%, 0.5); /* 50% transparent */
}
2. Background Colors
You can apply colors to the background of elements.
css
body {
background-color: lightgray;
}
3. Border Colors
css
div {
border: 2px solid rgb(0, 128, 255); /* Blue border */
}
4. Opacity Property
● The opacity property makes an element semi-transparent.
css
div {
background-color: blue;
opacity: 0.5; /* 50% transparent */
}
5. Gradient Colors
(a) Linear Gradient
css
div {
background: linear-gradient(to right, red, yellow);
}
css
body {
background-color: lightblue;
}
css
body {
background-image: url("background.jpg");
}
✅ Tips:
● Use an absolute URL (https://example.com/image.jpg) or relative URL
(images/bg.jpg).
● If the image does not load, the background-color (if defined) will be visible.
css
body {
background-image: url("pattern.png");
background-repeat: repeat; /* Default (Repeats both X &
Y) */
background-repeat: repeat-x; /* Repeats horizontally */
background-repeat: repeat-y; /* Repeats vertically */
background-repeat: no-repeat; /* No repeating */
}
css
body {
background-image: url("image.jpg");
background-size: cover; /* Covers entire area */
background-size: contain; /* Fits inside the container */
background-size: 100px 50px; /* Custom width & height */
}
css
body {
background-image: url("image.jpg");
background-position: top left;
background-position: center center;
background-position: 50% 50%;
}
6. Background Attachment (background-attachment)
Defines whether the background image scrolls with the page or stays fixed.
css
body {
background-image: url("image.jpg");
background-attachment: fixed; /* Background stays fixed */
background-attachment: scroll; /* Background moves with
the page */
}
div {
background: linear-gradient(to right, red, yellow);
}
div {
background: radial-gradient(circle, red, yellow);
}
css
body {
background: url("image.jpg") no-repeat center center /
cover fixed;
}
📌 Format:
background: [image] [repeat] [position] / [size] [attachment];
CSS Borders 🖼️
CSS Borders 🖼️
CSS borders allow you to define a frame around an element.
1. Border Syntax
The border property is a shorthand for:
Example:
css
div {
border: 2px solid red;
}
✅ Equivalent to:
css
div {
border-width: 2px;
border-style: solid;
border-color: red;
}
Style Example
Name
solid border: 3px solid
black;
Example:
css
p {
border: 4px dotted blue;
}
css
div {
border-width: 5px;
border-style: solid;
}
div {
border-top-width: 5px;
border-right-width: 10px;
border-bottom-width: 15px;
border-left-width: 20px;
}
css
h1 {
border: 3px solid rgb(0, 128, 255);
}
div {
border-top-color: red;
border-right-color: blue;
border-bottom-color: green;
border-left-color: yellow;
}
div {
border: 3px solid black;
border-radius: 10px;
}
✅ Perfect Circle:
css
div {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: lightblue;
}
div {
border-radius: 10px 20px 30px 40px; /* top-left,
top-right, bottom-right, bottom-left */
}
6. Individual Borders
You can set different styles, colors, and widths for each side.
css
div {
border-top: 5px solid red;
border-right: 3px dashed blue;
border-bottom: 4px double green;
border-left: 2px dotted orange;
}
7. Border Shorthand
You can define all properties in one line.
css
div {
border: 3px solid red;
}
div {
border: 5px dashed blue;
border-radius: 15px;
}
CSS Height & Width 📏
CSS Height & Width 📏
CSS provides properties to control the height and width of elements.
1. Width (width)
The width property sets the horizontal size of an element.
Example:
css
div {
width: 200px;
background-color: lightblue;
}
●
2. Height (height)
The height property sets the vertical size of an element.
Example:
css
div {
height: 100px;
background-color: lightgreen;
}
●
3. Full-Screen Element
You can make an element cover the full screen:
css
div {
width: 100vw; /* Full viewport width */
height: 100vh; /* Full viewport height */
background-color: gray;
}
4. Overflow (overflow)
Controls content that exceeds the height/width.
Property Description
div {
width: 200px;
height: 100px;
overflow: auto;
}
Property Description
div {
width: 300px;
height: 150px;
padding: 20px;
box-sizing: border-box;
}
CSS Margin & Padding 📏
CSS Margin & Padding 📏
Margins and padding are used for spacing in CSS.
1. Margin (margin)
The margin property controls the space outside an element.
Example:
css
div {
margin: 20px;
background-color: lightblue;
}
margin-top: 10px;
margin-right: 15px;
margin-bottom: 20px;
margin-left: 25px;
✅ Auto Centering:
css
div {
width: 50%;
margin: auto;
}
2. Padding (padding)
The padding property controls the space inside an element, between content and border.
Example:
css
div {
padding: 20px;
background-color: lightgreen;
}
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
css
div {
margin: 20px;
padding: 15px;
background-color: lightgray;
}
🔹 Default (content-box)
css
div {
width: 200px;
padding: 20px; /* Total width = 200px + 20px + 20px =
240px */
}
div {
width: 200px;
padding: 20px;
box-sizing: border-box; /* Keeps width = 200px */
}
CSS Box Model Components
CSS Box Model Components
Every HTML element is a rectangular box, and the box model consists of four main parts:
1. Content
○ The actual text, image, or other elements inside the box.
○ width and height define the size of the content area.
2. Padding
○ The space between the content and the border.
○ padding: 10px; adds 10px of space on all sides.
○ Can have different values for top, right, bottom, and left.
3. Border
○ A line around the padding (if any) and content.
○ border: 2px solid black; creates a solid black border of 2px.
○ Can be styled (solid, dashed, double, etc.).
4. Margin
○ The space outside the border, separating it from other elements.
○ margin: 20px; adds 20px of space around the element.
○ Can also be set individually for top, right, bottom, and left.
Box Sizing
Example Code
css
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid blue;
margin: 15px;
box-sizing: border-box;
}
CSS Text Properties
CSS Text Properties
3. Text Spacing
4. Text Shadow
5. Font Styling
Example Code
css
p {
color: #333;
text-align: justify;
font-family: 'Verdana', sans-serif;
font-size: 18px;
line-height: 1.6;
text-shadow: 1px 1px 3px gray;
}
CSS Links Styling - Quick Notes
CSS Links Styling - Quick Notes
Links (<a> tags) have different states that can be styled using CSS:
1. Link States
css
a:link { color: blue; } /* Unvisited link */
a:visited { color: purple; } /* Visited link */
a:hover { color: red; } /* When the mouse hovers over */
a:active { color: green; } /* When clicked */
2. Removing Underline
css
a {
text-decoration: none; /* Removes underline */
}
a:hover {
background: darkblue;
}
CSS Fonts - Quick Notes
CSS Fonts - Quick Notes
1. Font Family
p {
font-family: Arial, sans-serif;
}
2. Font Size
p {
font-size: 16px; /* Fixed size */
font-size: 1.2em; /* Relative size */
font-size: 120%; /* Percentage */
}
● Recommended Units:
○ px (pixels) → Fixed size
○ em / rem → Relative to parent/root element
○ % → Percentage of parent
3. Font Weight
Controls boldness.
p {
font-weight: normal; /* Default */
font-weight: bold; /* Bold */
font-weight: 300; /* Thin */
font-weight: 700; /* Extra Bold */
}
4. Font Style
css
p {
font-style: normal; /* Default */
font-style: italic; /* Italic text */
font-style: oblique; /* Slanted text */
}
5. Font Variant
css
p {
font-variant: small-caps;
}
p {
font: italic bold 20px/1.5 "Arial", sans-serif;
}
/* font-style | font-weight | font-size/line-height |
font-family */
CSS Units - Quick Notes
CSS Units - Quick Notes
CSS units define measurements like width, height, font size, margins, etc. They are
categorized into absolute and relative units.
Unit Description
cm Centimeters
mm Millimeters
🔹 Example:
div {
width: 200px;
height: 100px;
}
Unit Description
vmi Smaller of vw or vh
n
vma Larger of vw or vh
x
🔹 Example:
p {
font-size: 1.5em; /* 1.5 times parent font size */
}
.container {
width: 50%; /* Half of the parent element */
}
.hero {
width: 100vw; /* Full viewport width */
height: 100vh; /* Full viewport height */
}
🔹 Example:
.grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 3 columns, middle one
is twice as big */
}
Best Practices for Choosing Units
The cursor property changes the appearance of the mouse pointer when hovering over an
element.
1. Default Cursors
Cursor Description Example
🔹 Example:
button {
cursor: pointer; /* Changes to a hand icon */
}
2. Resize Cursors
Cursor Description Example
🔹 Example:
.draggable {
cursor: grab;
}
.draggable:active {
cursor: grabbing;
}
.custom-cursor {
cursor: url('cursor.png'), auto;
}
● The second value (auto) is a fallback in case the image fails to load.
Best Practices
The !important rule overrides all other CSS rules, including inline styles and styles with
higher specificity.
p {
color: red !important; /* This will override all other
color rules */
}
Even if another rule has higher specificity, !important will take precedence:
p {
color: blue; /* This will be ignored */
}
html
CopyEdit
<p style="color: green;">Hello</p>
p {
color: red !important; /* Will override inline color */
}
If two rules have !important, the one with higher specificity wins:
Best Practice
🚀 Use !important only as a last resort. Try restructuring your CSS before using it.
CSS box-shadow - Quick Notes
CSS box-shadow - Quick Notes
1. Basic Syntax
2. Basic Example
.box {
width: 200px;
height: 100px;
background: lightblue;
box-shadow: 5px 5px 10px gray;
}
🔹 This adds a gray shadow that is 5px right, 5px down, with a 10px blur.
5. Multiple Shadows
.box:hover {
box-shadow: none;
}
Best Practices
1. Basic Syntax
element {
opacity: value;
}
.box {
width: 200px;
height: 100px;
background: blue;
opacity: 0.5; /* 50% transparent */
}
3. Hover Effect
button {
background: green;
opacity: 0.7;
transition: opacity 0.3s;
}
button:hover {
opacity: 1; /* Fully visible on hover */
}
🔹 opacity affects the entire element, including text and child elements.
.parent {
background: red;
opacity: 0.5;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
animation: fadeIn 2s ease-in-out;
}
Best Practices
✔ Use opacity for smooth transitions & overlays.
✔ Use rgba() for transparent backgrounds (without affecting children).
✔ Combine with transition for smooth hover effects.
CSS filter Property - Quick Notes
CSS filter Property - Quick Notes
The filter property applies visual effects like blur, brightness, contrast, grayscale, etc.,
to an element (commonly used for images).
1. Basic Syntax
element {
filter: effect(value);
}
img {
filter: grayscale(100%) brightness(120%);
}
4. Hover Effect
img {
filter: grayscale(100%);
transition: filter 0.5s;
}
img:hover {
filter: none; /* Removes filter on hover */
}
.bg {
background: url('image.jpg');
filter: blur(10px);
}
⚠ Note: filter only works on elements, not background images directly. A workaround is
using pseudo-elements (::before).
.blur-box {
background: rgba(255, 255, 255, 0.3);
backdrop-filter: blur(10px);
}
Best Practices
Gradients in CSS allow you to create smooth transitions between multiple colors. There are
three main types:
1. Linear Gradient
A linear gradient transitions between colors along a straight line.
Basic Syntax:
div {
background: linear-gradient(red, blue);
}
div {
background: linear-gradient(to right, red, blue);
}
div {
background: linear-gradient(to bottom right, red, yellow,
blue);
}
Using Angles
2. Radial Gradient
A radial gradient spreads outward from a central point.
Basic Syntax:
div {
background: radial-gradient(circle, red, blue);
}
CopyEdit
background: radial-gradient(circle at top left, red, blue);
The overflow property controls what happens when content exceeds the size of its
container.
1. Basic Syntax
element {
overflow: value;
}
.box {
width: 200px;
height: 100px;
overflow: visible;
}
🔹 overflow: hidden
Hides content that overflows the container.
.box {
width: 200px;
height: 100px;
overflow: hidden;
}
🔹 Useful for clipping content (e.g., images, animations).
🔹 overflow: scroll
Always adds a scrollbar, even if not needed.
.box {
width: 200px;
height: 100px;
overflow: scroll;
}
🔹 overflow: auto
Adds a scrollbar only when needed.
.box {
width: 200px;
height: 100px;
overflow: auto;
}
Property Description
CopyEdit
overflow Controls horizontal overflow
.box {
-x
width: 200px;
overflow Controls vertical overflow height: 100px;
-y overflow-x: scroll;
overflow-y: hidden;
}
.box {
overflow: clip;
}
.text-box {
width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
1. Basic Syntax
element {
resize: value;
overflow: auto; /* Required for resizing to work */
}
✅ Example:
.resizable-box {
width: 200px;
height: 100px;
border: 2px solid black;
resize: both;
overflow: auto;
}
<div class="resizable-box">
Resize me!
</div>
.vertical {
resize: vertical;
overflow: auto;
}
3. Resizable Textarea
textarea {
width: 300px;
height: 100px;
resize: both; /* Can resize in any direction */
}
📌 Disable Resizing
textarea {
resize: none;
}
4. Controlling Maximum & Minimum Resize
You can set min-width, max-width, min-height, and max-height to control the
resizing limits.
✅ Example:
.resizable-box {
width: 200px;
height: 100px;
resize: both;
overflow: auto;
min-width: 150px;
max-width: 400px;
min-height: 80px;
max-height: 250px;
}
CSS Lists 📝
CSS Lists 📝
CSS provides various styling options for lists, including bullets, numbering, spacing, and
custom designs.
Value Description
none ❌ No bullets
✅ Example
ul {
list-style-type: square;
}
Value Description
decimal 1, 2, 3, … (default)
lower-alpha a, b, c, d, …
upper-alpha A, B, C, D, …
✅ Example
ol {
list-style-type: upper-roman;
}
ul, ol {
list-style: none;
}
✅ Example
ul {
list-style-image: url('bullet.png');
}
Value Description
✅ Example
ul {
list-style-position: inside;
}
ul {
list-style: square inside;
}
🔹 Equivalent to:
ul {
list-style-type: square;
list-style-position: inside;
}
CSS Tables 📊
CSS Tables 📊
CSS provides various ways to style HTML tables for better readability and appearance.
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 10px;
text-align: left;
}
✅ Explanation:
● width: 100%; → Makes the table take full width.
● border-collapse: collapse; → Merges borders for a clean look.
● padding: 10px; → Adds space inside cells.
● text-align: left; → Aligns text in cells.
✅ Example Table:
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>25</td>
</tr>
</table>
2. Table Borders (border)
You can customize border style, width, and color.
✅ Example:
table, th, td {
border: 2px solid blue;
}
✅ Example:
table {
border-collapse: separate;
border-spacing: 10px;
}
th {
background-color: #4CAF50;
color: white;
padding: 15px;
}
5. Striped Rows (nth-child)
Alternating row colors improve readability.
✅ Example:
tr:nth-child(even) {
background-color: #f2f2f2;
}
tr:hover {
background-color: yellow;
}
✅ Example:
th {
position: sticky;
top: 0;
background-color: #333;
color: white;
}
✅ Example:
.table-container {
overflow-x: auto;
}
<div class="table-container">
<table>
<!-- Table content here -->
</table>
</div>
CSS Functions 🎨
CSS Functions 🎨
CSS functions help perform calculations, manipulate colors, and dynamically adjust styles.
✅ Example:
div {
width: calc(100% - 50px); /* Subtracts 50px from full width */
height: calc(50vh + 20px); /* Half the viewport height plus 20px
*/
}
🔹 Operations Allowed: +, -, *, /
✅ Example:
:root {
--primary-color: blue;
--padding-size: 10px;
}
div {
background-color: var(--primary-color);
padding: var(--padding-size);
}
3.url() – Load External Resources 🌐
Loads images and other assets.
✅ Example:
div {
background-image: url('image.jpg');
}
✅ Example:
div {
width: min(50vw, 500px); /* Takes the smaller value */
height: max(200px, 10vh); /* Takes the larger value */
}
CSS Box Sizing 📦
CSS Box Sizing 📦
The box-sizing property controls how the width and height of an element are calculated. It
helps prevent unexpected layout shifts due to padding and borders.
✅ Example:
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
}
💡 Actual size = 200px (content) + 40px (padding) + 10px (border) = 250px total width
✅ Example:
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box;
}
✅ Best Practice:
* {
box-sizing: border-box;
}
✅ Example:
p {
color: red;
}
span {
color: initial; /* Resets to browser's default (usually black)
*/
}
💡 Result:
● <p> text = Red
● <span> text = Default black (not inherited)
✅ Example:
div {
color: blue;
}
p {
color: inherit; /* Takes color from <div> */
}
💡 Result:
● <p> inside <div> will be blue.
✅ Example:
p {
color: unset; /* Will inherit from parent */
border: unset; /* Will reset to default (no border) */
}
CSS object-fit Property 🖼️
CSS object-fit Property 🖼️
The object-fit property controls how an <img> or <video> fits inside its container,
maintaining aspect ratio without distortion.
1. Syntax 📌
img {
object-fit: value;
}
2. Values of object-fit 🎨
🔹 fill (Default) – Stretches to Fit
✅ The image stretches to fill the container (can distort).
img {
object-fit: fill;
}
💡 Useful when you want to shrink images but not enlarge them.
3. Example – object-fit in Action 🎬
<img src="image.jpg" style="width: 200px; height: 200px; object-fit:
cover;">
4. Bonus: object-position
Used with object-fit to adjust the image’s alignment inside the container.
✅ Example:
img {
object-fit: cover;
object-position: top; /* Aligns image to the top */
}
CSS Pseudo-classes 🎭
CSS Pseudo-classes 🎭
A pseudo-class is a keyword added to a selector that defines a special state of an
element.
1. Syntax 📌
selector:pseudo-class {
property: value;
}
✅ Example:
button:hover {
background-color: blue;
}
2. Common Pseudo-classes 🎨
🔹 User Interaction Pseudo-classes 🖱️
Pseudo-clas Description
s
button:active {
transform: scale(0.95);
}
🔹 Structural Pseudo-classes 📏
Pseudo-class Description
✅ Example:
p:first-child {
font-weight: bold;
}
div:nth-child(2) {
background-color: yellow;
}
🔹 Form Pseudo-classes 📝
Pseudo-class Description
:checked Selects checked checkboxes or radio
buttons.
✅ Example:
input:focus {
border: 2px solid blue;
}
input:required {
border: 2px solid red;
}
💡 All <p> elements except those with class special will turn gray.
3. Combining Pseudo-classes 🔗
✅ Example:
button:hover:active {
background-color: red;
}
4. Pseudo-class vs Pseudo-element 🚀
Feature Pseudo-class Pseudo-element
(:) (::)
1. Syntax 📌
selector::pseudo-element {
property: value;
}
✅ Example:
p::first-letter {
font-size: 2em;
color: red;
}
2. Common Pseudo-elements 🎭
🔹 ::before – Insert Content Before an Element
✅ Adds content before an element (useful for icons, decorations, etc.).
h1::before {
content: " 🔥 ";
}
3. Pseudo-element vs Pseudo-class 🚀
Feature Pseudo-element Pseudo-class
(::) (:)
button::after {
content: " ✅";
}
1. display Property 🖥️
The display property determines how an element is rendered in the layout.
✅ Example:
p {
display: none; /* Hides the paragraph completely */
}
2. visibility Property 👀
The visibility property controls whether an element is visible but still takes up space.
🔹 visibility Values
Value Description
✅ Example:
p {
visibility: hidden;
}
Element ✅ Yes ❌ No
removed?
.box2 {
visibility: hidden;
}
💡 box1 is completely removed, while box2 is invisible but still takes up space.
4. Bonus: opacity: 0 vs visibility: hidden 🎭
● opacity: 0; hides the element but keeps it interactive.
● visibility: hidden; makes the element invisible and non-interactive.
✅ Example:
.hidden {
opacity: 0;
}
8. Positioning Example 🚀
.container {
position: relative;
width: 300px;
height: 300px;
}
.box {
position: absolute;
top: 50px;
left: 50px;
background: red;
width: 100px;
height: 100px;
}
1. float Property 📌
The float property moves an element to the left or right, allowing text or other elements to
wrap around it.
🔹 float Values
Value Description
2. clear Property 🚫
The clear property is used to stop elements from wrapping around floated elements.
🔹 clear Values
Value Description
.box {
width: 100px;
height: 100px;
margin: 10px;
}
.left {
float: left;
background-color: lightblue;
}
.right {
float: right;
background-color: lightcoral;
}
✅ HTML
<div class="container">
<div class="box left">Left Box</div>
<div class="box right">Right Box</div>
</div>
1. transform Property 🚀
The transform property is used to apply 2D transformations to elements.
✅ Syntax:
selector {
transform: function(value);
}
2. Types of 2D Transforms 🛠️
🔹 1. translate(x, y) (Move)
Moves an element horizontally (x) and vertically (y).
✅ Example:
.box {
transform: translate(50px, 20px); /* Moves right 50px, down 20px
*/
}
💡 Shortcut:
● translateX(50px); → Moves right 50px
● translateY(-20px); → Moves up 20px
🔹 2. rotate(angle) (Rotate)
Rotates an element clockwise or counterclockwise.
✅ Example:
.box {
transform: rotate(45deg); /* Rotates 45 degrees clockwise */
}
🔹 3. scale(x, y) (Resize)
Resizes an element by scaling width (x) and height (y).
✅ Example:
.box {
transform: scale(1.5, 2); /* 1.5x width, 2x height */
}
💡 Shortcut:
● scaleX(2); → Doubles the width
● scaleY(0.5); → Shrinks height to 50%
🔹 4. skew(x, y) (Slant)
Tilts an element along the X or Y axis.
✅ Example:
.box {
transform: skew(20deg, 10deg); /* Skews 20° on X, 10° on Y */
}
💡 Shortcut:
● skewX(30deg); → Skews only along the X-axis
● skewY(15deg); → Skews only along the Y-axis
✅ Example:
.box {
transform: translate(50px, 20px) rotate(45deg) scale(1.2);
}
✅ Example:
.box {
transform-origin: top left; /* Rotates from the top-left corner
*/
transform: rotate(45deg);
}
✅ HTML
<div class="box"></div>
✅ Example:
.container {
perspective: 600px; /* Adjusts depth effect */
}
2. 3D Transform Functions 🛠️
🔹 1. translate3d(x, y, z) (Move in 3D Space)
Moves an element along the X, Y, and Z axes.
✅ Example:
.box {
transform: translate3d(50px, 20px, 100px);
}
💡 Shortcuts:
● translateX(50px); → Moves right 50px
● translateY(-20px); → Moves up 20px
● translateZ(100px); → Moves closer (toward the viewer)
✅ Example:
.box {
transform: rotate3d(1, 1, 0, 45deg); /* Rotates diagonally */
}
💡 Shortcuts:
● rotateX(45deg); → Rotates around the X-axis
● rotateY(60deg); → Rotates around the Y-axis
● rotateZ(90deg); → Rotates around the Z-axis
✅ Example:
.box {
transform: scale3d(1.5, 1.2, 1);
}
💡 Shortcuts:
● scaleX(2); → Doubles the width
● scaleY(1.5); → Increases height by 50%
● scaleZ(0.8); → Reduces depth
3. transform-style: preserve-3d 🎭
By default, child elements flatten into 2D.
To keep 3D depth, use preserve-3d.
✅ Example:
.parent {
transform-style: preserve-3d;
}
✅ Example:
.box {
backface-visibility: hidden;
}
.card {
width: 200px;
height: 200px;
position: relative;
transform-style: preserve-3d;
transition: transform 0.6s;
}
.card:hover {
transform: rotateY(180deg);
}
.front, .back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
}
.back {
background: coral;
transform: rotateY(180deg);
}
✅ HTML
<div class="container">
<div class="card">
<div class="front">Front</div>
<div class="back">Back</div>
</div>
</div>
💡 Hover to flip the card!
✅ Syntax:
selector {
transition: property duration timing-function delay;
}
🔹 Parameters:
Property Description
duration The time (in seconds or milliseconds) the transition takes (e.g., 2s,
500ms).
.box:hover {
background: blue;
transform: scale(1.2);
}
✅ HTML
<div class="box"></div>
✅ Example:
.box {
transition: width 2s ease-in-out;
}
4. Delaying Transitions ⏱️
Use transition-delay to start the animation after a delay.
✅ Example:
.box {
transition: background 1s ease-in 0.5s;
}
.box:hover {
background: green;
transform: rotate(20deg) scale(1.2);
}
✅ HTML
<div class="box"></div>
✅ Syntax:
@keyframes animationName {
from {
/* Initial state */
}
to {
/* Final state */
}
}
💡 OR using percentages:
@keyframes animationName {
0% { /* Start state */ }
50% { /* Mid state */ }
100% { /* End state */ }
}
✅ Syntax:
selector {
animation: name duration timing-function delay iteration-count
direction;
}
🔹 Parameters:
Property Description
.box {
width: 100px;
height: 100px;
background: coral;
animation: moveBox 2s ease-in-out infinite alternate;
}
✅ HTML
<div class="box"></div>
.box {
animation: colorChange 3s linear infinite;
}
Value Effect
✅ Example:
.box {
animation: moveBox 2s forwards;
}
✅ Example:
.box {
animation: moveBox 2s ease-in-out 1s;
}
.bouncing-box {
width: 100px;
height: 100px;
background: blue;
animation: bounce 1s infinite;
}
✅ HTML
<div class="bouncing-box"></div>
✅ Syntax:
@media print {
/* Print-specific styles here */
}
✅ CSS:
@media print {
body {
font-family: Arial, sans-serif; /* Set a readable font */
font-size: 12pt; /* Adjust font size */
}
header, footer {
display: none; /* Hide header and footer */
}
.no-print {
display: none; /* Hide elements with class 'no-print' */
}
}
💡 The content will only be visible when printed if it is not within the .no-print class.
Properties:
✅ Example:
@media print {
h2 {
page-break-before: always; /* Force a page break before each
heading */
}
.section {
page-break-inside: avoid; /* Prevent page break inside a
section */
}
footer {
page-break-after: always; /* Add a page break after the
footer */
}
}
✅ Example:
@media print {
.sidebar, .ads {
display: none; /* Hide elements not needed for print */
}
}
✅ Example:
@media print {
a {
color: black; /* Change links to black */
text-decoration: none; /* Remove underlines */
}
}
✅ Example:
@media print {
.container {
width: 100%; /* Set full width for better use of space */
margin: 0;
}
.two-column-layout {
display: block; /* Convert multi-column layout to single
column */
}
}
✅ Example:
@page {
size: A4; /* Set page size to A4 */
margin: 20mm; /* Set page margins */
}
@media print {
body {
margin: 0; /* Remove extra margin when printed */
}
}
/* Customize links */
a {
text-decoration: none;
color: black;
}
}
💡 Always make sure your printed pages are easy to read and look professional.
Responsive Web Design (RWD) 🌐📱
Responsive Web Design (RWD) 🌐📱
Responsive Web Design (RWD) is the approach of creating web pages that adapt to various
screen sizes and devices. This ensures that the user experience remains consistent and
optimal on mobile, tablet, and desktop screens.
Key Principles:
1. Fluid Grid Layouts - Use percentages instead of fixed pixel values for layout widths,
so the design adapts to the viewport.
2. Media Queries - Apply CSS styles based on the characteristics of the device (e.g.,
screen width).
3. Flexible Images - Images should resize within their containers using max-width:
100%.
Example:
.container {
width: 80%; /* Width adjusts to 80% of the viewport */
margin: 0 auto;
}
Syntax:
4. Fluid Images 🖼️
To make images responsive, you can set max-width: 100% to ensure images never
exceed their container's width.
Example:
img {
max-width: 100%;
height: auto; /* Maintain aspect ratio */
}
Example:
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
💡 width=device-width makes the page width match the device's screen width.
Example:
.container {
display: flex;
flex-wrap: wrap; /* Allow items to wrap */
}
.item {
flex: 1 1 200px; /* Flex items grow, shrink, and have a base
width */
}
Example:
.container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); /*
Auto-fill with minimum width */
gap: 20px;
}
💡 The grid adapts automatically based on the available space and screen size.
Example:
/* Mobile-first styles */
.container {
width: 100%;
padding: 10px;
}
9. Responsive Typography 📏
Responsive typography adjusts text sizes based on the screen width. You can use vw
(viewport width) for scalable typography.
Example:
h1 {
font-size: 5vw; /* Font size adjusts based on viewport width */
}
💡 The font size scales dynamically with the screen size!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Responsive Web Design</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.header {
background-color: #333;
color: white;
padding: 10px;
text-align: center;
}
.container {
display: flex;
flex-wrap: wrap;
padding: 20px;
}
.item {
flex: 1 1 200px;
margin: 10px;
background-color: lightgray;
padding: 20px;
text-align: center;
}
💡 This page will stack the items vertically on screens smaller than 768px wide!
Viewport Meta Tag 🌐📱
Viewport Meta Tag 🌐📱
The viewport meta tag is a key component in responsive web design, ensuring that a web
page adapts appropriately to different screen sizes, especially on mobile devices. It controls
the layout and scaling of the page in the browser’s viewport.
2. Syntax 📜
The basic syntax for the viewport meta tag is as follows:
Attributes:
● width=device-width: This sets the width of the page to be equal to the device's
screen width. It ensures that the content fits within the screen's width, regardless of
the device.
● initial-scale=1.0: This sets the initial zoom level when the page is first loaded. A
value of 1.0 means the page is not zoomed in or out.
● maximum-scale=1.0 (optional): Prevents users from zooming in beyond the set
scale.
● minimum-scale=1.0 (optional): Prevents users from zooming out beyond the set
scale.
● user-scalable=no (optional): Disables zooming on the page.
💡 This makes the page adjust to the width of the device’s screen and ensures it
loads without any initial zoom.
4. Common Variations 📐
1. Setting the Maximum and Minimum Zoom
To prevent zooming in or out on mobile devices, you can specify the maximum and minimum
zoom levels.
💡 This ensures the page is fixed at its default zoom level and cannot be zoomed in or
out.
If you want to completely disable zooming on your site, you can use user-scalable=no.
💡 This can improve layout consistency but might affect accessibility for users who
rely on zooming.
💡 This should always be the first line in the <head> section of your HTML document
to ensure proper scaling on all devices.
📄
8. Example: Full HTML Example with Viewport Meta Tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Responsive Page</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
h1 {
font-size: 2em;
color: #333;
}
p {
font-size: 1em;
line-height: 1.6;
}
</style>
</head>
<body>
<h1>Responsive Web Design</h1>
<p>This is an example of a responsive web page using the
viewport meta tag. Resize the browser window to see the layout
adjust.</p>
</body>
</html>
💡 This example ensures that the page layout adapts to different screen sizes.
Media Queries 📱💻
Media Queries 📱💻
Media Queries are a critical part of responsive web design. They allow you to apply different
CSS styles depending on the device's screen size, resolution, or other characteristics like
orientation and display type.
You can apply styles depending on the width and height of the viewport.
@media screen and (max-width: 768px) {
/* Styles for devices with screen width 768px or smaller */
}
● max-width: Applies styles when the viewport is smaller than the specified width.
● min-width: Applies styles when the viewport is larger than the specified width.
● max-height / min-height: Similar to width, but for screen height.
2. Orientation:
This condition is useful for targeting high-resolution screens like Retina displays.
Media queries can also apply styles for different output devices, such as printing.
@media print {
/* Styles for printing */
}
4. Examples of Common Media Queries 🖥️📱
1. Mobile-First Approach (Max-width Media Queries)
Mobile-first design involves writing the default styles for small screens and adding media
queries to scale for larger screens.
💡 This approach ensures the design looks good on mobile by default, and only
adjusts for larger screens.
💡 This query applies specific styles depending on both the width and orientation of
tablets.
💡 This applies when the screen size is wide enough for large desktops.
Example:
Example:
Desktop-First
h1 {
font-size: 2em;
color: #333;
}
p {
font-size: 1em;
line-height: 1.6;
}
💡 This layout changes the font sizes for small, medium, and large screens.
CSS Specificity 🎯
CSS Specificity 🎯
CSS specificity is a system used to determine which CSS rules are applied to an element
when multiple rules could apply. The more specific a selector is, the higher its specificity
and the more priority it has in applying styles.
1. What is Specificity? 🤔
Specificity defines the "weight" of a selector. If multiple CSS selectors match the same
element, the one with the highest specificity will take precedence and apply its styles.
(a, b, c, d)
Where:
● a = the number of inline styles (style attribute within the HTML element).
● b = the number of ID selectors (#id).
● c = the number of class selectors (.class), attributes selectors ([type="text"]),
and pseudo-classes (:hover).
● d = the number of type selectors (element names like div, p, etc.) and
pseudo-elements (::before, ::after).
Specificity Rules:
2. ID Selector
#main {
color: blue;
}
3. Class Selector
.content {
color: green;
}
p {
color: yellow;
}
CSS:
p {
color: yellow;
}
#main {
color: red;
}
.text {
color: green;
}
Specificity:
Result: The #main selector will take precedence and apply color: red because it has the
highest specificity.
CSS:
.content {
color: green;
}
p {
color: yellow;
}
Specificity:
Result: The .content selector will apply color: green because it has higher specificity
than the p selector.
CSS:
.text {
color: red;
}
.content {
color: blue;
}
Specificity:
Result: Since both classes have the same specificity, the last declared rule in the CSS file
will apply. In this case, color: blue will be applied because .content comes last.
5. Specificity Hierarchy 🔝
1. Inline styles: (1, 0, 0, 0)
2. ID selectors: (0, 1, 0, 0)
3. Class selectors, attribute selectors, pseudo-classes: (0, 0, 1, 0)
4. Element selectors, pseudo-elements: (0, 0, 0, 1)
6. Practical Tip: Avoid Over-Specificity ⚠️
Sometimes, it’s easy to fall into the trap of over-specifying selectors, which can make future
edits more complex. A good practice is to keep selectors as simple as possible to
maintain flexibility and reusability.
7. Overriding Specificity
If two selectors have the same specificity, the one that appears last in the CSS file will be
applied. If you need to override a style, you can:
Example:
#main {
color: blue !important;
}
.text {
color: red;
}
Result: The color: blue will be applied, even though .text has higher specificity,
because the !important rule takes precedence.
8. Summary of Specificity 📚
● Inline styles always win.
● ID selectors are very specific.
● Class, attribute, and pseudo-class selectors have medium specificity.
● Element selectors (tags like div, h1, p) are the least specific.
● Specificity values are cumulative: for example, #main .content p would have a
higher specificity than #main p due to the combination of ID, class, and element.
CSS Multiple Columns 📑
CSS Multiple Columns 📑
The CSS Multiple Columns feature allows you to split content into multiple columns, similar
to newspaper or magazine layouts. It enhances the readability of text-heavy content by
breaking it into narrower columns.
Syntax:
selector {
columns: <column-count> <column-width>;
}
Example:
.container {
columns: 3 200px;
}
This rule creates a layout with 3 columns, each having a width of 200px.
Example:
.container {
column-count: 3; /* Number of columns */
column-width: 200px; /* Width of each column */
}
If there is not enough space to fit the specified width, the browser will adjust the number of
columns accordingly.
Syntax:
selector {
column-gap: <value>;
}
● <value>: Specifies the gap between the columns (can be in px, em, rem, etc.).
Example:
.container {
column-count: 3;
column-gap: 30px;
}
selector {
column-rule: <width> <style> <color>;
}
Example:
.container {
column-count: 3;
column-rule: 2px solid #000;
}
.container {
column-count: 3;
column-gap: 20px;
column-fill: auto; /* Default behavior */
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Multi-Column Layout</title>
<style>
.container {
column-count: 3;
column-gap: 20px;
column-rule: 2px solid #000;
}
</style>
</head>
<body>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Donec a orci at nunc tempor tincidunt. Morbi eget bibendum lorem.
Sed tincidunt placerat turpis, in facilisis libero lacinia non.</p>
<p>Nullam ultricies, ligula nec varius lacinia, turpis velit
aliquet felis, vel euismod purus est sed nunc. Donec ac erat sit
amet est feugiat ultricies.</p>
<p>Maecenas vel purus libero. Nulla id orci eget mi
scelerisque vehicula. Donec id orci in enim mollis bibendum ac nec
dui. Proin vel suscipit justo. Integer condimentum nisi eget felis
tristique bibendum.</p>
</div>
</body>
</html>
By combining multiple columns with other layout techniques like flexbox or grid, you can
create more complex and responsive layouts.
2. Responsive Multi-Column Layouts
You can use media queries to adjust the number of columns depending on the screen size.
For instance, on mobile devices, you may want a single column, and on larger screens, you
can have multiple columns.
.container {
column-count: 1;
column-gap: 20px;
}
8. Conclusion 🎯
The CSS Multiple Columns feature is an easy way to create a multi-column layout, perfect
for content like articles, blogs, and news sites. With properties like column-count,
column-gap, and column-rule, you can easily adjust the number of columns, the
spacing between them, and even add rules between the columns.