Web Developement CSS
Web Developement CSS
texts, Fonts, borders and boxes, Margins, Padding Lists, CSS2, CSS3, Animations, Tool-Tips, Style
images, Variables, Flex Box, Media Queries, Wildcard Selectors (*, ^ and $) in CSS, Working with
Gradients, Pseudo Class, Pseudo elements, basic of frameworks like Bootstrap, Responsive web
design and Media Query, CSS variables
🧾 Introduction to CSS
o CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML
elements on a web page. While HTML provides the structure or content of a webpage, CSS
defines the look and feel—such as colors, fonts, layout, and spacing.
o CSS works by associating rules with HTML elements. These rules define how the content should
appear in terms of:
Fonts and text styling
Colors and backgrounds
Positioning and layout
Animations and transitions
o The term “Cascading” refers to the priority scheme used to determine which CSS rule applies
when multiple rules could apply to the same element. It allows styles to "cascade" down from
multiple sources, prioritizing inline, internal, and external styles accordingly.
✅ Advantages of CSS
Advantage Description
Separation of Concerns CSS separates content (HTML) from design, making both easier
to manage.
Improved Website One change in the CSS file updates styles across the entire site.
Maintainability
Reduces Code Duplication Common styles are written once and reused across
elements/pages.
Faster Page Loading External CSS files are cached by browsers, reducing load time.
Consistent Design Ensures a uniform look and feel across multiple web pages.
Enhanced Accessibility Styles can be adapted for screen readers, larger fonts, dark
modes, etc.
Supports Responsive Design Media queries help create designs that adapt to all devices.
❌ Disadvantages of CSS
Disadvantage Description
Browser Compatibility Some CSS features behave differently or are unsupported in older
Issues browsers.
Complexity with Large As a project grows, managing styles across many files can become
Projects challenging.
Global Scope by Default Styles apply globally unless carefully scoped, which may lead to
conflicts.
Requires Learning Curve Understanding selectors, specificity, and layout models (Flexbox, Grid)
can be tricky for beginners.
Debugging Difficulties Resolving conflicting styles or finding the source of applied styles can
be time-consuming.
🧾 Types of CSS
CSS can be applied to HTML documents in three main ways:
1. Inline CSS
2. Internal CSS
3. External CSS
🧪 Syntax Example:
✅ Advantages:
Point Description
🔹 Easy and Quick Great for small changes or testing.
🔹 Overrides Other Styles Has highest precedence in the cascade.
🔹 No Need for External Files Everything is inside a single HTML element.
❌ Disadvantages:
Point Description
🔻 Not Reusable You must repeat styles for every element.
🔻 Reduces Readability HTML becomes cluttered and hard to maintain.
🔻 Poor Separation of Mixes structure (HTML) with style (CSS), violating clean coding
Concerns principles.
📌 Use Case:
Quick one-time adjustments, e.g., highlighting a word or debugging.
2️⃣ Internal CSS
Internal CSS is defined inside the <style> tag within the <head> section of an HTML document. The
style rules apply to the entire HTML document.
Characteristics:
It is suitable for single-page websites or documents.
It allows centralized control over the styles for a page, improving organization compared to
inline CSS.
Since it's embedded within the HTML file, it increases the size of the page, which may affect
load time.
Internal CSS cannot be reused across multiple HTML pages without duplication.
It has medium precedence: higher than external styles, lower than inline styles.
Ideal Scenario:
Best suited for simple projects or prototypes where all styling is contained in one file, and external
file linking is unnecessary.
🧪 Syntax Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #f2f2f2;
}
h1 {
color: green;
text-align: center;
}
</style>
</head>
<body>
<h1>Internal CSS Example</h1>
</body>
</html>
✅ Advantages:
Point Description
🔹 Centralized for Single Page Styles are defined once for the whole page.
🔹 No Need for External Files Useful when hosting multiple files is not desired.
🔹 More Manageable than Inline Can target multiple elements without repeating.
❌ Disadvantages:
Point Description
🔻 Not Scalable Styles are restricted to just one page.
🔻 Repetition Across Pages You must copy-paste styles if the same design is needed elsewhere.
🔻 Slower Page Load HTML and styles are loaded together, increasing size.
📌 Use Case:
Single-page websites or internal applications where only one HTML file is used.
3️⃣ External CSS
External CSS involves writing all style rules in a separate .css file and linking it to the HTML document
using the <link> tag.
Characteristics:
It provides a clean separation of content and style, adhering to modern web development
best practices.
Styles can be reused across multiple HTML pages, ensuring a consistent look and reducing
redundancy.
The browser downloads and caches the CSS file, improving page load times on subsequent
visits.
It simplifies maintenance and scalability, as changes in one file reflect site-wide.
External CSS has lowest precedence among the three unless overridden or specified with !
important.
Ideal Scenario:
Used in professional web projects, multi-page websites, and applications where modularity,
reusability, and performance optimization are critical.
🧪 Syntax Example:
[Link]
h1 {
color: navy;
font-family: 'Arial', sans-serif;
}
p{
font-size: 16px;
color: gray;
}
[Link]
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>External CSS Example</h1>
<p>This paragraph is styled using external CSS.</p>
</body>
</html>
✅ Advantages:
Point Description
🔹 Best for Large Projects Styles are reusable across multiple pages.
🔹 Clean HTML Structure HTML stays neat and readable.
🔹 Better Performance CSS files are cached by browsers, speeding up page loads.
🔹 Easy Maintenance Style changes are done once in the .css file and affect all linked pages.
❌ Disadvantages:
Point Description
🔻 Requires Internet Access If CSS file is not available (e.g., due to broken link), styles won’t
load.
🔻 Slight Delay on First Load The browser has to fetch an additional file.
🔻 File Management Required Separate file must be created, maintained, and linked properly.
📌 Use Case:
Professional websites, multiple-page applications, or large projects where consistency and scalability
are required.
h1, h2, h3 {
font-family: Arial, sans-serif;
}
Applies the same font to all <h1>, <h2>, and <h3> elements.
🔑 Summary Table
.info {
color: blue;
font-weight: bold;
}
<h1 id="main-title">Welcome</h1>
#main-title {
text-align: center;
font-size: 32px;
}
Only the element with id="main-title" will receive the style.
📏 Best Practices
.box {
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
}
.highlight {
color: red;
font-weight: bold;
}
🧠 Best Practices
❌ Don’t:
Overuse <div> and <span> unnecessarily; use semantic tags like <header>, <section>,
<article> where appropriate.
Nest too many <div>s inside each other without structure—known as "div soup."
padding: 10px;
🔹 3️⃣ Border
A line surrounding the padding and content.
Can be styled using border-width, border-style, and border-color.
border: 2px solid black;
🔹 4️⃣ Margin
Space outside the element, separating it from other elements.
Affects the layout of nearby elements.
margin: 20px;
🧾Padding in CSS
Padding is the space inside an element, between the content and its border. It increases the
internal spacing, allowing the content to "breathe" within the box.
Think of padding as cushioning inside a box—it creates room around the content, but within the
border.
🎯 Key Characteristics
Padding affects only the internal space.
It does not overlap with other elements.
Padding can be applied to each side independently or shorthand.
✅ Visual Example
Imagine a text inside a box:
<div style="padding: 20px; background-color: lightblue;">
Hello, I am inside a padded box!
</div>
In this example:
padding: 20px; creates 20 pixels of space on all four sides of the text within the light blue
box.
This spacing gives the content a more readable and aesthetic appearance.
🧠 Practical Uses of Padding
Improve readability: Add spacing around text in cards or buttons.
Make clickable areas larger: Padding enlarges interactive zones without affecting layout.
Create visual balance: Even spacing inside boxes improves design aesthetics.
Separate content from borders: Prevent text from “touching” the edge.
✅ Visual Example
border-width: 3px;
2️⃣ border-style
Defines the visual appearance of the line.
Common values:
Value Description
None No border
Solid Solid straight line
Dashed Dashed lines
Dotted Dotted border
Double Two solid lines
Groove Carved appearance
Ridge Opposite of groove
Inset Sunken appearance
Outset Raised appearance
border-style: dashed;
3️⃣ border-color
Sets the color of the border.
border-color: blue;
🎨 Combined Examples
✅ Basic Full Border
border: 2px solid black;
✅ Only Top and Bottom Borders
border-top: 3px dashed orange;
border-bottom: 3px dashed orange;
✅ Colorful Border with Rounded Corners
border: 4px double blue;
border-radius: 8px;
✅ Different Widths and Colors
border-width: 2px 5px 3px 1px;
border-style: solid;
border-color: red green blue yellow;
🧠 Summary Table
Property Purpose
Border Shorthand for setting all border parts
border-width Controls thickness
border-style Controls line appearance
border-color Sets border color
border-top/right/... Side-specific borders
border-radius Rounds corners
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Box Model Example</title>
<style>
.card {
width: 300px;
margin: 40px auto; /* Centers the card with space from top */
padding: 20px; /* Internal space inside the card */
border: 2px solid #333; /* Visible solid border */
border-radius: 10px; /* Rounded corners */
background-color: #f9f9f9; /* Light background */
font-family: Arial, sans-serif;
}
.card h2 {
margin-bottom: 10px;
color: #222;
}
.card p {
margin: 0;
padding: 10px;
background-color: #e7f3ff;
border: 1px dashed #66aaff;
}
</style>
</head>
<body>
<div class="card">
<h2>John Doe</h2>
<p>This is a message box that uses margin, padding, and border effectively to create clean layout
spacing and visual styling.</p>
</div>
</body>
</html>
🔹 1. color
Sets the text color.
Can use named colors, HEX, RGB, RGBA, HSL, etc.
color: blue;
color: #333;
color: rgb(255, 0, 0);
🔹 2. text-align
Aligns text horizontally within its container.
Value Description
Left Aligns to the left (default)
Right Aligns to the right
center Centers the text
justify Aligns both left and right
text-align: center;
🔹 3. text-decoration
Adds decoration line(s) to text.
Value Effect
None Removes underline
underline Adds underline
overline Adds line above text
line- Strikes through text
through
text-decoration: underline;
🔹 4. text-transform
Controls capitalization of text.
Value Effect
none No change
uppercas Converts all text to UPPERCASE
e
lowercase Converts all text to lowercase
capitalize Capitalizes the first letter of each word
text-transform: capitalize;
🔹 5. letter-spacing
Controls the space between characters.
letter-spacing: 2px;
🔹 6. word-spacing
Controls the space between words.
word-spacing: 5px;
🔹 7. line-height
Sets the vertical spacing between lines of text.
🔹 8. text-indent
Indents the first line of a block of text.
text-indent: 40px;
🔹 9. white-space
Controls how white space inside an element is handled.
Value Effect
normal Collapses spaces and wraps text (default)
nowrap Prevents wrapping
pre Preserves whitespace and line breaks
pre-wrap Preserves whitespace, wraps when needed
pre-line Collapses whitespace, preserves line breaks
white-space: nowrap;
🔹 10. direction
Sets the text direction.
🧠 Summary Table
Property Purpose Common Values
Color Text color red, #333, rgb(0,0,0)
text-align Horizontal alignment left, right, center, justify
text-decoration Adds/removes lines underline, line-through, none
text-transform Changes case uppercase, lowercase, capitalize
letter-spacing Space between letters px, em
word-spacing Space between words px, em
line-height Line spacing 1.5, 24px, 120%
text-indent Indent first line 20px, 2em
white-space Handling of normal, nowrap, pre, pre-wrap
whitespace
Direction Text direction ltr, rtl
✅ Practical Example
<p style="
color: #444;
text-align: justify;
text-decoration: none;
text-transform: capitalize;
letter-spacing: 1px;
word-spacing: 3px;
line-height: 1.6;
text-indent: 30px;">
this paragraph is styled using various text properties in css to enhance readability and visual
appearance.
</p>
🔹 1️⃣ font-family
Specifies the typeface (font) to use for the text.
🧠 Syntax:
font-family: "Arial", "Helvetica", sans-serif;
Always provide fallbacks: If the first font isn't available, the next one is used.
Generic families include: serif, sans-serif, monospace, cursive, fantasy.
✅ Example:
p{
font-family: 'Georgia', serif;
}
🔹 2️⃣ font-size
Sets the size of the font.
📏 Units:
px → Pixels (fixed)
em → Relative to parent’s font-size
rem → Relative to root element
% → Relative to parent
vw, vh → Viewport-based sizes
✅ Examples:
h1 {
font-size: 32px;
}
p{
font-size: 1.2em;
}
🔹 3️⃣ font-weight
Sets the thickness or boldness of the text.
Values:
Keywor Numeric Equivalent
d
normal 400
bold 700
lighter Relative
bolder Relative
Numeric 100 to 900
✅ Examples:
h1 {
font-weight: bold;
}
p{
font-weight: 300;
}
🔹 4️⃣ font-style
Applies italic or oblique styling to the font.
Values:
normal – Default
italic – Italic version of the font
oblique – Slanted version (less common)
✅ Example:
em {
font-style: italic;
}
🔹 5️⃣ font-variant
Controls the use of small-caps.
Values:
normal – Default
small-caps – Converts lowercase to uppercase but smaller
✅ Example:
p{
font-variant: small-caps;
}
<style>
.quote {
font-family: 'Georgia', serif;
font-size: 20px;
font-weight: 400;
font-style: italic;
font-variant: small-caps;
line-height: 1.6;
}
</style>
<p class="quote">
"Simplicity is the ultimate sophistication."
</p>
🎯 1️⃣ background-color
Sets a solid color as the background of an element.
✅ Syntax:
background-color: lightblue;
🔹 Example:
<div style="background-color: #e0f7fa;">
This box has a background color.
</div>
2️⃣ background-image
Sets an image as the background.
✅ Syntax:
background-image: url("[Link]");
🔹 Example:
div {
background-image: url("[Link]");
}
🔁 3️⃣ background-repeat
Controls whether the background image repeats (tiles) and in which direction.
Value Behavior
repeat Repeats both x and y (default)
no-repeat Displays once only
repeat-x Repeats horizontally
repeat-y Repeats vertically
✅ Example:
div {
background-repeat: no-repeat;
}
📍 4️⃣ background-position
Specifies the starting position of the background image.
Keyword Values Meaning
top, right, bottom, left, center Relative to container sides
x% y% or xpx ypx Exact positioning in % or px
✅ Example:
div {
background-position: center center;
}
📏 5️⃣ background-size
Controls the scaling of the background image.
Value Description
Auto Default; keeps original size
Cover Scales to cover entire container (cropping possible)
Contain Scales to fit within container (no cropping)
width height Specific size in px or %
✅ Example:
div {
background-size: cover;
}
📌 6️⃣ background-attachment
Defines how background behaves during scrolling.
Value Behavior
scroll Scrolls with the page (default)
fixed Stays fixed on screen
local Scrolls with the element content
✅ Example:
div {
background-attachment: fixed;
}
div {
background: #f0f0f0 url("[Link]") no-repeat center center / cover fixed;
}
Property Purpose
background-color Sets background color
background-image Sets background image
background-repeat Controls tiling
background-position Sets position of image
background-size Controls image scaling
background-attachment Scroll or fixed positioning
Background Shorthand for all background settings
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Background and Border Example</title>
<style>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
margin: 0;
padding: 40px;
}
.box {
width: 500px;
margin: auto;
padding: 30px;
color: white;
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.7)),
url("[Link] no-repeat center center / cover;
border: 5px solid #007BFF;
border-radius: 15px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
background-attachment: fixed;
text-align: center;
}
.box h2 {
margin-top: 0;
font-size: 32px;
text-transform: uppercase;
letter-spacing: 2px;
}
.box p {
font-size: 18px;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="box">
<h2>Styled Box</h2>
<p>
This box demonstrates the use of CSS backgrounds and borders. The image is blended with a
gradient overlay, and the box has rounded corners, a solid border, and a drop shadow.
</p>
</div>
</body>
</html>
✅ Practical Example
<style>
.color-box {
width: 120px;
height: 120px;
display: inline-block;
margin: 10px;
}
.hex { background-color: #ff6347; } /* tomato */
.rgb { background-color: rgb(0, 128, 0); } /* green */
.rgba { background-color: rgba(0, 0, 255, 0.4); } /* transparent blue */
</style>
1️⃣ list-style-type
Defines the marker style (bullet or number) used in a list.
🔹 Common Values for <ul>:
Value Description
disc ● (default)
circle ○
square ■
none No marker
🔹 For <ol>:
Value Description
Decimal 1, 2, 3, ... (default)
lower-alpha a, b, c, ...
upper-alpha A, B, C, ...
lower-roman i, ii, iii, ...
upper- I, II, III, ...
roman
✅ Example:
ul {
list-style-type: square;
}
ol {
list-style-type: upper-roman;
}
2️⃣ list-style-position
Controls whether the marker appears inside or outside the list item box.
Value Description
outsid Marker is outside the content block (default)
e
inside Marker is inside the content block
✅ Example:
ul {
list-style-position: inside;
}
3️⃣ list-style-image
Replaces the default marker with a custom image.
✅ Example:
ul {
list-style-image: url("[Link]");
}
⚠️Best used for consistent icon bullets.
✅ Practical Example
<style>
.styled-list {
list-style-type: square;
list-style-position: inside;
font-family: Arial;
font-size: 16px;
padding: 10px;
background-color: #f9f9f9;
width: 300px;
border: 1px solid #ccc;
}
.styled-list li {
margin-bottom: 8px;
color: #444;
}
</style>
<ul class="styled-list">
<li>HTML & CSS</li>
<li>JavaScript</li>
<li>Responsive Design</li>
<li>SEO Basics</li>
</ul>
🧠 Summary Table
Property Function
list-style-type Sets bullet/number format
list-style- Positions bullet inside or outside
position
list-style-image Replaces bullet with an image
list-style Shorthand for all the above
🔹 1️⃣ block
📘 Behavior:
Takes full width of the container.
Always starts on a new line.
✅ Examples:
<div>, <p>, <h1> are block by default.
display: block;
🔍 Used For:
Structuring page sections
Headers, articles, footers
🔹 2️⃣ inline
📘 Behavior:
Sits on the same line with other inline elements.
Does not allow width/height, margin-top/bottom, or padding-top/bottom.
✅ Examples:
<span>, <a>, <strong> are inline by default.
display: inline;
🔍 Used For:
Styling parts of text
Inline links or labels
🔹 3️⃣ inline-block
📘 Behavior:
Behaves like inline (sits on the same line).
But also supports width, height, margin, padding like block.
display: inline-block;
🔍 Used For:
Horizontal menus
Buttons, icons, tooltips
Card-like elements aligned inline
📋 Side-by-Side Comparison
Feature block inline inline-block
Starts new line ✅ Yes ❌ No ❌ No
Respects width ✅ Yes ❌ No ✅ Yes
Respects height ✅ Yes ❌ No ✅ Yes
Aligns ❌ No ✅ Yes ✅ Yes
horizontally
.menu a {
display: inline-block;
color: white;
text-decoration: none;
padding: 10px 20px;
margin: 0 5px;
border-radius: 5px;
background-color: #007BFF;
}
.menu a:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="menu">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
</body>
</html>
🧠 Explanation:
Each <a> tag is set to inline-block → appears on the same line, but accepts padding, border,
and width like a block.
Allows for button-like horizontal menus.
You cannot do this with inline alone, since it ignores width and padding-top/bottom.
/* Visited link */
a:visited {
color: purple;
}
/* On mouse hover */
a:hover {
color: white;
background-color: blue;
padding: 5px 10px;
border-radius: 4px;
transition: 0.3s;
}
🧠 Summary Table
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Anchor Link Styling Demo</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
padding: 40px;
}
a{
display: inline-block;
margin: 10px;
padding: 10px 20px;
text-decoration: none;
font-size: 18px;
border-radius: 5px;
transition: background-color 0.3s, color 0.3s;
}
/* Unvisited link */
a:link {
color: #007BFF;
background-color: white;
border: 2px solid #007BFF;
}
/* Visited link */
a:visited {
color: #6f42c1;
border-color: #6f42c1;
}
/* Hover state */
a:hover {
color: white;
background-color: #007BFF;
}
</body>
</html>
🧠 What You'll See:
🎨 Default (link): Blue border and text.
✔️Visited: Purple text and border.
Hover: Turns white text on a blue background.
👇 Active: While clicking, it goes darker.
🔲 Focus: When tabbing, it gets an orange outline.
🧾 What is CSS2?
📘 CSS2 (Cascading Style Sheets Level 2) was published in 1998 by the W3C. It built upon CSS1 and
introduced foundational features that are still widely used today.
❌ Limitations:
No support for animations, transitions, or rounded corners
Limited layout options (no Flexbox or Grid)
Inconsistent browser support (especially IE6/7)
🧾 What is CSS3?
📘 CSS3 was introduced in the late 2000s and is still evolving. It broke CSS into modules to allow
easier updates and faster adoption by browsers.
Module/Feature Description
Selectors Level 3 Advanced selectors like :nth-child(), :not(), ::before, ::after
Box Model box-sizing, border-radius, box-shadow
Enhancements
Backgrounds & Borders Multiple backgrounds, background-size, gradients
Text Effects text-shadow, word-wrap, advanced text-decoration
Web Fonts @font-face allows custom fonts to be loaded
2D & 3D Transforms transform, rotate, scale, translate, perspective
Transitions & Animations Smooth changes using transition and keyframe @animation
Flexible Layouts Flexbox, Grid, media queries for responsive design
Variables CSS custom properties (e.g., --main-color) for reusable values
Media Queries Responsive design based on screen size, resolution, etc.
🧠 Summary
CSS2 is the foundation of styling: box model, positioning, basic selectors, display types.
CSS3 is all about modern layout, visual effects, and responsiveness.
Today’s web pages combine CSS2 and CSS3 — modern features layered on solid basics.
🧱 Flexbox Structure
✅ Flex Container
The parent element with:
display: flex;
✅ Flex Items
The direct child elements of the flex container — automatically take on flexible behavior.
📐 One-Dimensional Layout
Main Axis: Direction in which the flex items are placed.
o Set by flex-direction
o Can be horizontal (default) or vertical
Cross Axis: Perpendicular to the main axis.
📦 Basic Syntax
.container {
display: flex;
}
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
🧠 Key Benefits
Align elements vertically and horizontally
Make equal width or height columns
Easily reorder elements visually
Support for wrapping items automatically
Clean, minimal markup and CSS
✅ Example:
<div class="flex-container">
<div class="box">Item 1</div>
<div class="box">Item 2</div>
<div class="box">Item 3</div>
</div>
.flex-container {
display: flex;
background-color: #f2f2f2;
padding: 10px;
}
.box {
background-color: #007BFF;
color: white;
padding: 20px;
margin: 10px;
font-size: 18px;
}
📦 Flex Items
Only direct children of the flex container become flex items
They automatically align based on the main axis direction
You can control size, alignment, order, and spacing of each item individually
✅ Visual Illustration:
If flex-direction: row (default):
MAIN AXIS → →
+-----------------------------+
| [Item 1] [Item 2] [Item 3] |
| |
|^ |
|| |
| CROSS AXIS |
+-----------------------------+
If flex-direction: column:
CROSS AXIS →
↓
[Item 1]
↓
[Item 2]
↓
[Item 3]
↑ MAIN AXIS
.box {
background-color: #28a745;
color: white;
padding: 20px;
margin: 5px;
}
🧠 Summary
Concept Key Point
Flex Container Created using display: flex
Flex Items Direct children of the flex container
Main Axis Controlled by flex-direction (row, column, etc.)
Cross Axis Perpendicular to main axis; affects vertical/horizontal alignment
🔹 1. flex-direction
Controls the direction in which flex items are placed in the container.
.flex-container {
display: flex;
flex-direction: row; /* default */
}
Value Description
row Left to right (default)
row-reverse Right to left
column Top to bottom
column- Bottom to top
reverse
🔹 2. flex-wrap
By default, flex items try to fit in one line. flex-wrap allows them to wrap to next line if needed.
.flex-container {
flex-wrap: wrap;
}
Value Description
Nowrap All items in one line (default)
Wrap Wrap onto multiple lines
wrap-reverse Wrap in reverse direction
🔹 3. flex-flow
Shorthand for both flex-direction and flex-wrap.
.flex-container {
flex-flow: row wrap;
}
🔹 4. justify-content
Aligns items horizontally along the main axis.
.flex-container {
justify-content: space-between;
}
Value Description
flex-start Items start from beginning (default)
flex-end Items align to end of container
Center Items centered in container
space-between Equal space between items
space-around Equal space around items
space-evenly Equal space between and around
🔹 5. align-items
Aligns items vertically along the cross axis (single line).
.flex-container {
align-items: center;
}
Value Description
stretch (default) items stretch to fill cross axis
flex-start Top (or left in column mode)
flex-end Bottom (or right in column mode)
center Centered vertically
baseline Aligns text baselines
🔹 6. align-content
Only applies when items wrap to multiple lines.
.flex-container {
flex-wrap: wrap;
align-content: space-between;
}
Value Description
flex-start Lines packed to start of container
flex-end Lines packed to end
Center Lines centered
space-between Equal spacing between lines
space-around Equal spacing above/below lines
Stretch (default) lines stretch to fill space
.item {
background-color: #007BFF;
color: white;
padding: 20px;
margin: 10px;
width: 100px;
text-align: center;
border-radius: 5px;
}
</style>
<div class="flex-container">
<div class="item">Box 1</div>
<div class="item">Box 2</div>
<div class="item">Box 3</div>
<div class="item">Box 4</div>
</div>
🧾 Flex Item Properties
These are CSS properties you apply to individual flex items (i.e., the children of a flex container).
They control growth, shrinkage, ordering, and alignment of each item independently.
🔹 1️⃣ order
Default order = 0
Items with lower order appear first
.item1 { order: 2; }
.item2 { order: 1; }
.item3 { order: 3; }
➡️Even though .item2 is written second, it will be shown first.
🔹 2️⃣ flex-grow
Defines how much the item can grow relative to others.
If all items have flex-grow: 1, they will grow equally.
If one has flex-grow: 2, it gets twice as much space.
.item {
flex-grow: 1;
}
.[Link] {
flex-grow: 2;
}
🔹 3️⃣ flex-shrink
Defines how much the item can shrink when container space is limited.
If flex-shrink: 0, it won’t shrink at all.
.item {
flex-shrink: 1;
}
.[Link] {
flex-shrink: 0;
}
🔹 4️⃣ flex-basis
Sets the initial size of the flex item before grow or shrink happens.
Can be auto, a length (px, %, etc.), or 0.
.item {
flex-basis: 150px;
}
🔹 6️⃣ align-self
Overrides the cross-axis alignment (align-items) for just one item.
.flex-container {
align-items: center;
}
.[Link] {
align-self: flex-start;
}
.item {
background-color: #28a745;
color: white;
padding: 20px;
margin: 10px;
flex: 1 1 150px;
text-align: center;
border-radius: 5px;
}
.item:nth-child(2) {
flex-grow: 2;
background-color: #007bff;
}
.item:nth-child(3) {
align-self: flex-start;
background-color: #ffc107;
}
.item:nth-child(4) {
order: -1;
background-color: #dc3545;
}
</style>
<div class="flex-container">
<div class="item">Item 1</div>
<div class="item">Item 2<br>(flex-grow: 2)</div>
<div class="item">Item 3<br>(align-self: start)</div>
<div class="item">Item 4<br>(order: -1)</div>
</div>
🧠 Summary Table
Property Role
order Changes visual position in flex container
flex-grow Shares available space after initial sizing
flex-shrink Shrinks item if container is too small
flex-basis Sets starting size of item
flex Shorthand for grow, shrink, and basis
align-self Aligns item vertically (cross axis), overrides parent
.menu a {
color: white;
text-decoration: none;
margin-left: 20px;
}
.col {
flex: 1;
background: #ddd;
padding: 20px;
}
📌 Each .col takes equal space and stretches to the same height, automatically.
.inner {
background: #007bff;
color: white;
padding: 20px;
border-radius: 5px;
}
✅ This centers .inner perfectly in both directions.
.sidebar {
width: 250px;
background: #333;
color: white;
padding: 20px;
}
.main {
flex: 1;
background: #f9f9f9;
padding: 20px;
}
✅ Easily build a 2-column layout.
button {
padding: 10px 20px;
}
📱 Works well on small screens because wrap is enabled.
🧠 Tip:
Use these Flexbox patterns as building blocks for dashboards, cards, forms, and content grids.
🎨 CSS Styling:
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip .tooltip-text {
visibility: hidden;
width: 140px;
background-color: black;
color: #fff;
text-align: center;
padding: 8px;
border-radius: 5px;
/* Positioning */
position: absolute;
bottom: 125%; /* show above the text */
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
z-index: 1;
}
.tooltip:hover .tooltip-text {
visibility: visible;
opacity: 1;
}
✅ Output:
Hover me → when hovered → shows “This is a tooltip!” in a black box above the text.
🧪 Full Example
<style>
:root {
--primary-color: #4CAF50;
--secondary-color: #ffffff;
--padding: 15px;
--border-radius: 8px;
}
button {
background-color: var(--primary-color);
color: var(--secondary-color);
padding: var(--padding);
border: none;
border-radius: var(--border-radius);
font-size: 16px;
}
</style>
<button>Click Me</button>
✅ This button is styled using variables — easy to tweak!
.dark-theme .card {
--card-bg: #222;
}
📌 --card-bg is scoped to .card, and overridden in .dark-theme .card.
🧠 Best Practices
Tip Reason
Use :root for global Makes them available throughout your CSS
variables
Use semantic names --primary-color instead of --blue
Organize by section Group typography, colors, spacing variables
Use fallbacks For older browsers or missing definitions
Combine with media queries For responsive theming
🧠 Basic Syntax
@media media-type and (condition) {
/* CSS rules here */
}
Or simply:
@media (max-width: 768px) {
/* Apply these styles on smaller screens */
}
.column {
flex: 1;
}
/* Larger screens */
@media (min-width: 768px) {
.container {
font-size: 18px;
}
}
✅ Use Cases
Use Case Media Query Example
Hide element on @media (max-width: 480px)
phone
Change nav layout @media (max-width: 768px)
Adjust grid/card size @media (min-width: 1024px)
Detect landscape mode @media (orientation: landscape)
.nav {
display: flex;
justify-content: space-around;
}
<div class="header">
<h1>My Website</h1>
<div class="nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Contact</a>
</div>
</div>
✅ This layout becomes column-wise nav on small screens.
Wildcard Selectors?
These are attribute selectors and are very useful for partial matching of attribute values like class, id,
href, src, alt, etc.
✅ Syntax
[attribute*="value"] { ... } /* contains */
[attribute^="value"] { ... } /* starts with */
[attribute$="value"] { ... } /* ends with */
These are used inside square brackets [ ], targeting HTML attributes.
✨ Summary
Selector Meaning Example Matches
[attr*="x"] Contains x [class*="nav"] main-nav, top-navigation
[attr^="x"] Starts with x [href^="https"] [Link]
[attr$="x"] Ends with x [src$=".jpg"] [Link]
🔹 1. Linear Gradient
📌 Syntax:
background: linear-gradient(direction, color1, color2, ...);
📌 Example:
div {
background: linear-gradient(to right, #ff7e5f, #feb47b);
}
🔀 Directions:
to right
to left
to top
to bottom
45deg, 90deg, etc.
💡 With Multiple Colors:
background: linear-gradient(to bottom, red, yellow, green);
🔹 2. Radial Gradient
📌 Syntax:
background: radial-gradient(shape size at position, color1, color2, ...);
📌 Example:
div {
background: radial-gradient(circle, #6a11cb, #2575fc);
}
circle or ellipse
closest-side, farthest-corner, etc.
💡 Example With Positioning:
background: radial-gradient(circle at top left, red, yellow);
.linear {
background: linear-gradient(135deg, #ff416c, #ff4b2b);
}
.radial {
background: radial-gradient(circle, #43cea2, #185a9d);
}
.conic {
background: conic-gradient(from 90deg, red, orange, yellow, green, blue, violet, red);
}
</style>
📌 Summary Table
Gradient Type Syntax Example
Linear linear-gradient(to right, red, yellow)
Radial radial-gradient(circle, red, yellow)
Conic conic-gradient(from 0deg, red, yellow, green)