This project demonstrates different CSS positioning methods through interactive examples. Below is a detailed explanation of the HTML structure and CSS styling.
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Positioning Demo</title>
<link rel="stylesheet" href="styles.css">- Charset: Specifies UTF-8 encoding for proper character rendering
- Viewport Meta: Ensures responsive design by setting viewport width to device width with 1:1 initial zoom
- Title: Browser tab title
- CSS Link: Connects external stylesheet for styling
The page contains a <header> and <main> section with four demonstration sections:
<section class="demo-section">
<div class="box-container">
<div class="box static">Static</div>
<div class="box relative">Relative (Moved!)</div>
<div class="box static">Static</div>
</div>
</section>- Shows how
position: relativemoves elements while keeping their original space in the document flow - Three boxes: two static (normal flow) and one relatively positioned
<section class="demo-section">
<div class="box-container absolute-parent">
<div class="box static">Static</div>
<div class="box absolute">Absolute</div>
<div class="box static">Static</div>
</div>
</section>- Demonstrates
position: absolutewhich removes elements from the document flow - The parent container has
position: relativeto serve as the positioning context - Static boxes collapse to fill the gap left by the absolutely positioned element
<div class="box fixed">Fixed Box</div>
<div class="scroll-filler">
<p>Scroll down to see the fixed box stay in place...</p>
<div class="filler-line"></div> <!-- Multiple lines for scrolling -->
</div>- Shows
position: fixedwhich positions elements relative to the viewport - The fixed box remains visible even when scrolling the page
<div class="sticky-container">
<div class="box sticky">Sticky Box</div>
<div class="scroll-content">
<!-- Large text content for scrolling -->
</div>
</div>- Demonstrates
position: stickywhich toggles between relative and fixed positioning - The container has
overflow-y: autoto enable internal scrolling - Content includes substantial text to enable scrolling behavior demonstration
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}- Removes default browser margins and padding
box-sizing: border-boxmakes width/height calculations include padding and borders
body {
font-family: Arial, sans-serif;
margin: 20px;
padding: 20px;
}- Sets font and adds spacing around the page content
.demo-section {
margin-bottom: 60px;
}- Adds vertical space between each positioning demonstration
.box-container {
border: 1px solid #ccc;
padding: 10px;
min-height: 200px;
}
.box {
border: 2px dashed #333;
padding: 14px;
background: #f5f5f5;
display: flex;
align-items: center;
justify-content: center;
min-width: 140px;
}.box-container: Creates a bordered container with minimum height for demos.box: Base styling for all boxes with centered content using flexbox
.box.relative {
position: relative;
background: #e67e22; /* Orange */
color: white;
top: 30px;
left: 40px;
}- Positions the box 30px down and 40px right from its normal position
- Orange background for visual distinction
- The box maintains its original space in the document flow
.absolute-parent {
position: relative;
min-height: 250px;
}
.box.absolute {
position: absolute;
background: #e74c3c; /* Red */
color: white;
bottom: 20px;
right: 20px;
}- Parent has
position: relativeto establish positioning context for the child - Absolutely positioned box is placed 20px from bottom and 20px from right
- Red background for visual distinction
- Removed from document flow, allowing other content to collapse into its space
.box.fixed {
position: fixed;
background: #27ae60; /* Green */
color: white;
top: 20px;
right: 20px;
}- Positioned 20px from top and 20px from right of the viewport
- Green background for visual distinction
- Stays in place during scrolling (positioned relative to browser window, not page)
.sticky-container {
border: 1px solid #ccc;
padding: 10px;
height: 300px;
overflow-y: auto;
}
.box.sticky {
position: sticky;
background: #9b59b6; /* Purple */
color: white;
top: 0;
width: 100%;
}- Container has fixed height with
overflow-y: autoto enable scrolling - Sticky box is positioned to stick at
top: 0within its container - Purple background for visual distinction
- As you scroll the container, the sticky box remains visible at the top
.scroll-content {
padding: 10px;
font-size: 20px;
}- Styling for scrollable content within the sticky container demonstration
| Positioning Type | Positioning Context | Document Flow | Use Case |
|---|---|---|---|
| Relative | Its normal position | Maintains space | Slight adjustments to element position |
| Absolute | Nearest positioned ancestor | Removed | Overlays, modals, precise placement |
| Fixed | Viewport (browser window) | Removed | Navigation bars, floating buttons |
| Sticky | Viewport within container bounds | Maintains space | Table headers, section headings |