Course Content
Project file
0/1
Complete Web Designing Course for Beginners

Lesson 4.5 – Positioning Elements

🎯 Controlling the Exact Position of Elements on a Webpage


🎯 Learning Objectives

After completing this lesson, students will be able to:

βœ… Understand CSS Positioning

βœ… Use Static Positioning

βœ… Use Relative Positioning

βœ… Use Absolute Positioning

βœ… Use Fixed Positioning

βœ… Use Sticky Positioning

βœ… Control Element Placement

βœ… Build Professional Website Layouts


πŸ“– Introduction

Imagine a website containing:

  • Logo
  • Navigation Menu
  • Login Button
  • Advertisement Banner
  • Floating Contact Button

How do websites place these elements exactly where they want?

The answer is:

πŸ“ CSS Positioning


πŸ€” What is Positioning?

Positioning controls:

Β 
Where an element appears
on a webpage.
Β 

CSS Position Property

Syntax:

position:value;
Β 

Common Position Values

position:static;
position:relative;
position:absolute;
position:fixed;
position:sticky;
Β 

🌟 Why Positioning is Important?

Positioning helps create:

βœ… Navigation Bars

βœ… Floating Buttons

βœ… Popups

βœ… Login Forms

βœ… Banners

βœ… Professional Layouts


🎯 Understanding Coordinates

Positioning uses:

Β 
top
Β 
Β 
bottom
Β 
Β 
left
Β 
Β 
right
Β 

Example:

top:20px;

Move element 20 pixels down.


Example:

left:50px;

Move element 50 pixels right.


1️⃣ Static Position

Default position.

Every element uses:

position:static;

unless changed.


Example

div{

position:static;

}

Result

Element stays in normal document flow.


HTML

<div>

HTML

</div>

CSS

div{

position:static;

}

No visible change.


🌟 Static Position Characteristics

βœ… Default value

βœ… Follows normal page flow

βœ… Top, Left, Right, Bottom do not work


Example

div{

position:static;

top:100px;

}

Result:

Β 
No movement
Β 

2️⃣ Relative Position

Relative means:

Move element relative to its original position.


Syntax

position:relative;

Example

div{

position:relative;

left:50px;

}

Result

Element moves 50px right.


Visual Example

Original:

Β 
HTML
Β 

After:

Β 
     HTML
Β 

Example

HTML

<div>

HTML

</div>

CSS

div{

position:relative;

left:100px;

}

Result

Element shifts right.


Relative Position Characteristics

βœ… Original space remains reserved

βœ… Element moves visually

βœ… Useful for small adjustments


Example

position:relative;

top:20px;
Β 

Moves element downward.


Example

position:relative;

right:30px;

Moves element left.


3️⃣ Absolute Position

Absolute positioning removes element from normal flow.


Syntax

position:absolute;
Β 

Example

div{

position:absolute;

top:50px;

left:100px;

}

Result

Element appears at exact coordinates.


Visual Example

Β 
Screen

-------------------

HTML

-------------------
Β 

Placed exactly.


Important

Absolute positioning uses the nearest positioned parent.


Example

<div class="parent">

<div class="child">

HTML

</div>

</div>

CSS

.parent{

position:relative;

width:400px;

height:200px;

border:2px solid blue;

}

.child{

position:absolute;

top:20px;

left:30px;

}

Result

Child appears inside parent at specified location.


Absolute Position Characteristics

βœ… Precise placement

βœ… Removed from normal flow

βœ… Useful for icons and overlays


4️⃣ Fixed Position

Fixed elements stay visible even while scrolling.


Syntax

position:fixed;

Example

button{

position:fixed;

bottom:20px;

right:20px;

}

Result

Button remains visible.


Real World Example

WhatsApp Floating Button:

Β 
πŸ’¬
Β 

Always visible.


Example

HTML

<button>

Contact Us

</button>

CSS

button{

position:fixed;

bottom:20px;

right:20px;

}

Result

Floating contact button.


Fixed Position Characteristics

βœ… Stays on screen

βœ… Ignores scrolling

βœ… Great for action buttons


5️⃣ Sticky Position

Sticky combines:

Β 
Relative

+

Fixed
Β 

Syntax

position:sticky;

Example

header{

position:sticky;

top:0;

}

Result

Header sticks to top when scrolling.


Real World Example

Navigation Bars

Β 
Home About Contact
Β 

Remain visible during scrolling.


Example

HTML

<header>

NextGen Dev Hub

</header>

CSS

header{

position:sticky;

top:0;

background-color:blue;

color:white;

}

Result

Header stays at top.


🌟 Position Comparison Table

Position Moves Element Keeps Space Scrolls
Static ❌ βœ… βœ…
Relative βœ… βœ… βœ…
Absolute βœ… ❌ βœ…
Fixed βœ… ❌ ❌
Sticky βœ… βœ… Partial

🎯 Practical Example

HTML

<div class="box">

HTML

</div>

CSS

.box{

position:relative;

left:100px;

background-color:lightblue;

padding:20px;

}

Result

Box moves right.


πŸ’» Complete Practical Program

HTML

<!DOCTYPE html>

<html>

<head>

<link rel="stylesheet" href="style.css">

</head>

<body>

<header>

NextGen Dev Hub

</header>

<div class="box">

Learning CSS Positioning

</div>

</body>

</html>

CSS

header{

position:sticky;

top:0;

background-color:blue;

color:white;

padding:15px;

}

.box{

width:300px;

height:150px;

background-color:lightyellow;

border:2px solid orange;

position:relative;

left:50px;

top:20px;

padding:20px;

}

🎯 Expected Output

βœ… Sticky Header

βœ… Positioned Content Box

βœ… Professional Layout


🏫 Real World Examples

Navigation Bar

position:sticky;

Floating Chat Button

position:fixed;

Notification Badge

position:absolute;

Small Adjustments

position:relative;

Normal Content

position:static;

⚠️ Common Beginner Mistakes


Mistake 1

Using Top Without Position

Wrong:

top:20px;

No effect.


Correct:

position:relative;

top:20px;

Mistake 2

Overusing Absolute Position

Can break layouts.

Use carefully.


Mistake 3

Forgetting Parent Position

For absolute positioning:

.parent{

position:relative;
}

is often required.


🌟 Best Practices

βœ… Use relative for small adjustments.

βœ… Use absolute for precise placement.

βœ… Use fixed for floating buttons.

βœ… Use sticky for navigation bars.

βœ… Avoid excessive positioning.


πŸ† Mini Project – Floating Contact Button

Create:

Β 
Contact Us
Β 

Button.

Apply:

position:fixed;

bottom:20px;

right:20px;

πŸ“ Practical Activity

Create:

Sticky Header

Β 
My College Website
Β 

Floating Button

Β 
Help
Β 

Positioned Box

Β 
Learning CSS Position
Β 

Apply different positioning methods.


πŸ“‹ Assignment

Assignment 4.5

  1. What is CSS Positioning?
  2. What is Static Position?
  3. What is Relative Position?
  4. What is Absolute Position?
  5. What is Fixed Position?
  6. What is Sticky Position?
  7. Create a floating contact button.

🧠 Quiz

Q1. What is the default position value?

A. relative

B. absolute

C. static

D. fixed

βœ… Answer: C


Q2. Which position keeps an element fixed during scrolling?

A. static

B. relative

C. fixed

D. sticky

βœ… Answer: C


Q3. Which position is commonly used for notification badges?

A. static

B. absolute

C. sticky

D. block

βœ… Answer: B


Q4. Which position is commonly used for sticky navigation bars?

A. fixed

B. absolute

C. sticky

D. static

βœ… Answer: C


Q5. Which position moves an element relative to its original location?

A. relative

B. absolute

C. fixed

D. sticky

βœ… Answer: A


πŸ“Œ Lesson Summary

βœ… Positioning controls where elements appear.

βœ… Static is the default position.

βœ… Relative moves elements from their original position.

βœ… Absolute places elements at exact coordinates.

βœ… Fixed keeps elements visible while scrolling.

βœ… Sticky creates headers and menus that stay visible.

βœ… Positioning is essential for modern website layouts.

Scroll to Top