9: CSS Positioning
Chapter 8-ൽ display property ഉപയോഗിച്ച് elements page-ൽ എങ്ങനെ കാണപ്പെടണം എന്ന് പഠിച്ചു. ഇനി ഒരു element page-ൽ കൃത്യമായി എവിടെ സ്ഥാനം പിടിക്കണം എന്ന് നിയന്ത്രിക്കാൻ CSS Positioning പഠിക്കാം.
Positioning ഉപയോഗിച്ച് ഒരു element-നെ അതിന്റെ സാധാരണ സ്ഥാനത്ത് നിന്ന് മാറ്റാനും, മറ്റൊരു element-നെ അടിസ്ഥാനമാക്കി സ്ഥാപിക്കാനും, browser window-നോട് fixed ആയി നിർത്താനും കഴിയും.
1. position property
ഒരു element എങ്ങനെ position ചെയ്യണം എന്ന് നിർവചിക്കാൻ position property ഉപയോഗിക്കുന്നു. ഇതിന്റെ പ്രധാന values static, relative, absolute, fixed, sticky എന്നിവയാണ്.
| Value | പ്രധാന ആശയം |
|---|---|
| static | Default positioning; normal document flow |
| relative | Normal position-നെ അടിസ്ഥാനമാക്കി element മാറ്റാം |
| absolute | Positioned ancestor-നെ അടിസ്ഥാനമാക്കി സ്ഥാപിക്കാം |
| fixed | Browser viewport-നോട് fixed ആയി നിൽക്കും |
| sticky | ഒരു പരിധിവരെ normal flow-ൽ, പിന്നീട് sticky ആയി പ്രവർത്തിക്കും |
2. position: static
static ആണ് elements-ന്റെ സാധാരണ default position. Element normal document flow അനുസരിച്ച് page-ൽ സ്ഥാനം പിടിക്കും.
.box {
position: static;
}സാധാരണയായി position: static; പ്രത്യേകം എഴുതേണ്ട ആവശ്യമില്ല, കാരണം ഇത് default behavior ആണ്.
3. top, right, bottom, left
Positioned element-ന്റെ സ്ഥാനം മാറ്റാൻ top, right, bottom, left properties ഉപയോഗിക്കാം. ഇവയുടെ effect position value-നെ ആശ്രയിച്ചിരിക്കും.
.box {
position: relative;
top: 20px;
left: 30px;
}ഇവിടെ element-ന്റെ സാധാരണ സ്ഥാനത്തിൽ നിന്ന് 20px താഴേക്കും 30px വലത്തേക്കും മാറ്റുന്നു.
4. position: relative
relative positioning ഉപയോഗിക്കുമ്പോൾ element ആദ്യം normal document flow-ൽ സ്വന്തം സ്ഥാനം നിലനിർത്തുന്നു. അതിന്റെ ശേഷം top, right, bottom, left ഉപയോഗിച്ച് അതിന്റെ visual position മാറ്റാം.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 150px;
padding: 20px;
background-color: lightblue;
position: relative;
left: 30px;
}
</style>
</head>
<body>
<div class="box">Relative Box</div>
<p>Another paragraph.</p>
</body>
</html>Box visual ആയി 30px വലത്തേക്ക് മാറിയാലും document flow-ൽ അതിന്റെ original space നിലനിൽക്കും.
5. Relative Positioning എപ്പോൾ ഉപയോഗിക്കാം?
- ഒരു element-നെ അതിന്റെ സാധാരണ സ്ഥാനത്തിൽ നിന്ന് ചെറിയ അളവിൽ മാറ്റാൻ.
- ഒരു parent element-നെ absolute child-ന്റെ positioning reference ആക്കാൻ.
- Badge, icon തുടങ്ങിയ ചെറിയ positioned elements-ന്റെ container തയ്യാറാക്കാൻ.
6. position: absolute
absolute positioning ഉപയോഗിക്കുമ്പോൾ element normal document flow-ൽ നിന്ന് പുറത്തേക്ക് പോകുന്നു. സാധാരണയായി അതിന്റെ ഏറ്റവും അടുത്ത positioned ancestor-നെ അടിസ്ഥാനമാക്കിയാണ് സ്ഥാനം കണക്കാക്കുന്നത്.
<!DOCTYPE html>
<html>
<head>
<style>
.parent {
position: relative;
width: 300px;
height: 200px;
background-color: lightblue;
}
.child {
position: absolute;
top: 20px;
right: 20px;
padding: 10px;
background-color: white;
}
</style>
</head>
<body>
<div class="parent">
<div class="child">Badge</div>
</div>
</body>
</html>ഇവിടെ parent-ന് position: relative; നൽകിയിട്ടുണ്ട്. അതിനാൽ absolute child-ന് parent-നെ positioning reference ആയി ഉപയോഗിക്കാം.
7. Parent relative, Child absolute Pattern
CSS-ൽ വളരെ സാധാരണമായി കാണുന്ന ഒരു pattern ആണ് parent-ന് position: relative; നൽകുകയും അതിന്റെ ഉള്ളിലെ പ്രത്യേക element-ന് position: absolute; നൽകുകയും ചെയ്യുന്നത്.
.card {
position: relative;
}
.badge {
position: absolute;
top: 10px;
right: 10px;
}ഇതിലൂടെ badge card-ന്റെ top-right ഭാഗത്ത് സ്ഥാപിക്കാം. Cards, notifications, labels, icons തുടങ്ങിയ UI elements-ൽ ഈ pattern വളരെ ഉപയോഗപ്രദമാണ്.
8. absolute element-ന്റെ reference
ഒരു absolute element-ന് അടുത്ത positioned ancestor ഇല്ലെങ്കിൽ browser മറ്റൊരു positioning context ഉപയോഗിക്കും. അതിനാൽ absolute child-ന് വ്യക്തമായ reference ആവശ്യമുണ്ടെങ്കിൽ parent-ന് position: relative; നൽകുന്നത് സാധാരണ practice ആണ്.
.container {
position: relative;
}
.item {
position: absolute;
top: 0;
left: 0;
}9. position: fixed
fixed positioning ഉപയോഗിക്കുമ്പോൾ element browser viewport-നോട് ബന്ധപ്പെട്ട് നിൽക്കും. Page scroll ചെയ്താലും അത് അതേ viewport position-ൽ തുടരാം.
.help-button {
position: fixed;
right: 20px;
bottom: 20px;
padding: 12px 18px;
background-color: lightblue;
}ഇത്തരത്തിലുള്ള positioning floating help button, back-to-top button, fixed notification തുടങ്ങിയ UI elements-ൽ ഉപയോഗിക്കാം.
10. Fixed Header Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
padding-top: 60px;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
padding: 18px;
background-color: lightblue;
box-sizing: border-box;
}
</style>
</head>
<body>
<header>My Website</header>
<main>
<h1>Welcome</h1>
<p>
Scroll the page to see the fixed header remain in place.
</p>
<p style="height: 1000px;">
Page content continues here.
</p>
</main>
</body>
</html>Fixed header സാധാരണ document flow-ൽ നിന്ന് പുറത്താകുന്നതിനാൽ content header-ന്റെ പിന്നിൽ പോകാതിരിക്കാൻ body-ക്ക് padding-top നൽകുന്നത് ഇവിടെ ഉപയോഗിച്ചിരിക്കുന്നു.
11. position: sticky
sticky positioning normal document flow-ൽ ആരംഭിക്കുകയും ഒരു specified offset എത്തിയ ശേഷം sticky ആയി പ്രവർത്തിക്കുകയും ചെയ്യുന്നു. ഇത് സാധാരണയായി scrolling സമയത്ത് ഒരു heading അല്ലെങ്കിൽ navigation section കാണിച്ചുകൊണ്ടിരിക്കാൻ ഉപയോഗിക്കുന്നു.
.menu {
position: sticky;
top: 0;
padding: 15px;
background-color: lightblue;
}ഇവിടെ top: 0; നൽകിയതിനാൽ scrolling സമയത്ത് element viewport-ന്റെ മുകളിലെ ഭാഗത്ത് sticky ആയി നിൽക്കാൻ ശ്രമിക്കും.
12. Sticky Practical Example
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
}
.section-title {
position: sticky;
top: 0;
padding: 15px;
background-color: lightblue;
}
.content {
height: 500px;
padding: 20px;
}
</style>
</head>
<body>
<h2 class="section-title">HTML Course</h2>
<div class="content">
<p>Course content goes here.</p>
</div>
<h2 class="section-title">CSS Course</h2>
<div class="content">
<p>More course content goes here.</p>
</div>
</body>
</html>Sticky behavior മനസ്സിലാക്കാൻ browser-ൽ page scroll ചെയ്ത് നോക്കുക. Sticky element-ന്റെ behavior അതിന്റെ containing area, scrolling context എന്നിവയെയും ആശ്രയിക്കാം.
13. Positioning Values താരതമ്യം ചെയ്യാം
| Position | Normal Flow? | സാധാരണ ഉപയോഗം |
|---|---|---|
| static | അതെ | Default layout |
| relative | അതെ | Original position reference ചെയ്ത് ചെറിയ മാറ്റങ്ങൾ |
| absolute | ഇല്ല | Parent-ന്റെ ഉള്ളിൽ precise positioning |
| fixed | ഇല്ല | Viewport-നോട് fixed UI |
| sticky | സാഹചര്യത്തിന് അനുസരിച്ച് | Scrolling സമയത്തെ sticky elements |
14. z-index
രണ്ട് positioned elements ഒരേ സ്ഥലത്ത് overlap ചെയ്യുമ്പോൾ ഏത് element മുകളിൽ കാണണം എന്ന് നിയന്ത്രിക്കാൻ z-index ഉപയോഗിക്കാം.
.box-one {
position: absolute;
z-index: 1;
}
.box-two {
position: absolute;
z-index: 2;
}സാധാരണയായി വലിയ z-index value ഉള്ള element stacking order-ൽ മുകളിലേക്ക് വരും. എന്നാൽ z-index-ന്റെ ഫലം stacking context പോലുള്ള കാര്യങ്ങളെയും ആശ്രയിക്കാം.
15. z-index Practical Example
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 300px;
height: 200px;
}
.box-one,
.box-two {
position: absolute;
width: 150px;
height: 100px;
padding: 10px;
}
.box-one {
top: 20px;
left: 20px;
background-color: lightblue;
z-index: 1;
}
.box-two {
top: 60px;
left: 80px;
background-color: lightgreen;
z-index: 2;
}
</style>
</head>
<body>
<div class="container">
<div class="box-one">Box One</div>
<div class="box-two">Box Two</div>
</div>
</body>
</html>ഇവിടെ രണ്ട് boxes overlap ചെയ്യുന്നു. Box Two-ന് z-index: 2; ഉള്ളതിനാൽ അത് Box One-ന് മുകളിലായി കാണപ്പെടും.
16. Positioning ഉപയോഗിച്ച് Badge
ഒരു card-ന്റെ corner-ൽ New, Sale, Popular തുടങ്ങിയ ചെറിയ labels സ്ഥാപിക്കുന്നത് absolute positioning-ന്റെ നല്ല practical example ആണ്.
<!DOCTYPE html>
<html>
<head>
<style>
.card {
position: relative;
width: 300px;
padding: 25px;
border: 2px solid #333;
box-sizing: border-box;
}
.badge {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
background-color: lightgreen;
font-weight: bold;
}
</style>
</head>
<body>
<div class="card">
<div class="badge">NEW</div>
<h2>CSS Course</h2>
<p>Learn CSS step by step.</p>
</div>
</body>
</html>ഇവിടെ card ആണ് positioning context. Badge card-ന്റെ top-right ഭാഗത്ത് absolute ആയി സ്ഥാപിച്ചിരിക്കുന്നു.
17. Positioning ഉപയോഗിക്കുമ്പോൾ ശ്രദ്ധിക്കേണ്ട കാര്യങ്ങൾ
- സാധാരണ layout നിർമ്മിക്കാൻ positioning മാത്രം ആശ്രയിക്കരുത്.
- Absolute positioning ആവശ്യമായ UI elements-ന് മാത്രം ഉപയോഗിക്കുക.
- Absolute child-ന് വ്യക്തമായ reference വേണമെങ്കിൽ parent-ന് position: relative; നൽകുക.
- Fixed elements content മറയ്ക്കുന്നില്ലെന്ന് ഉറപ്പാക്കുക.
- Sticky elements ഉപയോഗിക്കുമ്പോൾ scrolling behavior പരിശോധിക്കുക.
- z-index values അനാവശ്യമായി വളരെ വലിയ numbers ആക്കി ഉപയോഗിക്കേണ്ടതില്ല.
- Responsive screens-ലും positioning ശരിയായി പ്രവർത്തിക്കുന്നുണ്ടോ എന്ന് പരിശോധിക്കുക.
18. Positioning vs Layout Systems
Positioning പ്രധാനമായും ഒരു element-നെ പ്രത്യേക സ്ഥലത്ത് സ്ഥാപിക്കാനുള്ള tool ആണ്. എന്നാൽ page-ന്റെ മുഴുവൻ layout നിർമ്മിക്കാൻ Flexbox, Grid പോലുള്ള layout systems കൂടുതൽ അനുയോജ്യമാണ്.
അതിനാൽ എല്ലാ elements-നും absolute positioning ഉപയോഗിക്കുന്നത് നല്ല practice അല്ല. അടുത്ത അധ്യായം മുതൽ Flexbox പഠിക്കുമ്പോൾ elements എങ്ങനെ row, column രീതിയിൽ ക്രമീകരിക്കാം എന്ന് കൂടുതൽ ശക്തമായി മനസ്സിലാക്കാം.
Common Beginner Mistakes
- Normal layout-ന് absolute positioning അമിതമായി ഉപയോഗിക്കുക.
- Absolute child-ന്റെ parent-ന് position: relative; നൽകേണ്ടത് മറക്കുക.
- Fixed header content-നെ മറയ്ക്കുന്നത് ശ്രദ്ധിക്കാതിരിക്കുക.
- z-index വലിയ number നൽകിയാൽ എല്ലാ സാഹചര്യങ്ങളിലും element മുകളിൽ വരുമെന്ന് കരുതുക.
- top, left എന്നിവ ഉപയോഗിക്കുമ്പോൾ element-ന്റെ positioning context മനസ്സിലാക്കാതിരിക്കുക.
- Sticky positioning പ്രവർത്തിക്കാത്തപ്പോൾ containing area, offset എന്നിവ പരിശോധിക്കാതിരിക്കുക.
Practice Exercises
- ഒരു box-ന് position: relative; നൽകി 30px വലത്തേക്ക് മാറ്റുക.
- ഒരു parent container-നും absolute child-നും ഉള്ള example നിർമ്മിക്കുക.
- ഒരു card-ന്റെ top-right corner-ൽ NEW badge സ്ഥാപിക്കുക.
- ഒരു floating help button viewport-ന്റെ bottom-right ഭാഗത്ത് fixed ആയി സ്ഥാപിക്കുക.
- ഒരു heading-ന് sticky positioning നൽകി page scroll ചെയ്ത് പരിശോധിക്കുക.
- രണ്ട് overlapping boxes ഉണ്ടാക്കി z-index ഉപയോഗിച്ച് ഏതാണ് മുകളിൽ കാണേണ്ടതെന്ന് നിയന്ത്രിക്കുക.
- Relative, absolute, fixed, sticky എന്നിവ ഓരോന്നും ഉപയോഗിക്കുന്ന ചെറിയ examples നിർമ്മിച്ച് അവയുടെ behavior താരതമ്യം ചെയ്യുക.
Chapter Summary
- position property element-ന്റെ positioning behavior നിയന്ത്രിക്കുന്നു.
- static ആണ് സാധാരണ default positioning.
- relative element-ന്റെ normal position നിലനിർത്തിക്കൊണ്ട് അതിനെ മാറ്റാൻ സഹായിക്കുന്നു.
- absolute element-നെ positioned ancestor-നെ അടിസ്ഥാനമാക്കി സ്ഥാപിക്കാം.
- fixed element viewport-നോട് fixed ആയി നിൽക്കുന്നു.
- sticky scrolling സമയത്ത് ഒരു specified position-ൽ sticky ആയി പ്രവർത്തിക്കുന്നു.
- top, right, bottom, left എന്നിവ positioned elements-ന്റെ സ്ഥാനം നിയന്ത്രിക്കാൻ ഉപയോഗിക്കുന്നു.
- z-index overlap ചെയ്യുന്ന elements-ന്റെ stacking order നിയന്ത്രിക്കാൻ ഉപയോഗിക്കുന്നു.
- സാധാരണ page layout-നായി Flexbox, Grid പോലുള്ള layout systems ആണ് കൂടുതൽ അനുയോജ്യം.
Quick Quiz
Quiz & Self Assessment
അടുത്ത അധ്യായത്തിൽ Flexbox Basics പഠിക്കാം. ഒരു container-ന്റെ ഉള്ളിലുള്ള elements-നെ row, column രീതിയിൽ ക്രമീകരിക്കാനും alignment, spacing എന്നിവ നിയന്ത്രിക്കാനും Flexbox എങ്ങനെ ഉപയോഗിക്കാം എന്ന് അവിടെ പഠിക്കും.