Skip to content

cu-fs1/positioning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

CSS Positioning Demo - Code Breakdown

This project demonstrates different CSS positioning methods through interactive examples. Below is a detailed explanation of the HTML structure and CSS styling.

HTML Structure (index.html)

Head Section

<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

Body Structure

The page contains a <header> and <main> section with four demonstration sections:

1. Relative Positioning Demo

<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: relative moves elements while keeping their original space in the document flow
  • Three boxes: two static (normal flow) and one relatively positioned

2. Absolute Positioning Demo

<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: absolute which removes elements from the document flow
  • The parent container has position: relative to serve as the positioning context
  • Static boxes collapse to fill the gap left by the absolutely positioned element

3. Fixed Positioning Demo

<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: fixed which positions elements relative to the viewport
  • The fixed box remains visible even when scrolling the page

4. Sticky Positioning Demo

<div class="sticky-container">
    <div class="box sticky">Sticky Box</div>
    <div class="scroll-content">
        <!-- Large text content for scrolling -->
    </div>
</div>
  • Demonstrates position: sticky which toggles between relative and fixed positioning
  • The container has overflow-y: auto to enable internal scrolling
  • Content includes substantial text to enable scrolling behavior demonstration

CSS Styling (styles.css)

Universal Reset

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
  • Removes default browser margins and padding
  • box-sizing: border-box makes width/height calculations include padding and borders

Base Body Styles

body {
    font-family: Arial, sans-serif;
    margin: 20px;
    padding: 20px;
}
  • Sets font and adds spacing around the page content

Demo Section Spacing

.demo-section {
    margin-bottom: 60px;
}
  • Adds vertical space between each positioning demonstration

Box Container & Box Styles

.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

Relative Positioning (.box.relative)

.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 Positioning (.absolute-parent & .box.absolute)

.absolute-parent {
    position: relative;
    min-height: 250px;
}

.box.absolute {
    position: absolute;
    background: #e74c3c;       /* Red */
    color: white;
    bottom: 20px;
    right: 20px;
}
  • Parent has position: relative to 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

Fixed Positioning (.box.fixed)

.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 Positioning (.sticky-container & .box.sticky)

.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: auto to enable scrolling
  • Sticky box is positioned to stick at top: 0 within its container
  • Purple background for visual distinction
  • As you scroll the container, the sticky box remains visible at the top

Content Styling

.scroll-content {
    padding: 10px;
    font-size: 20px;
}
  • Styling for scrollable content within the sticky container demonstration

Key Concepts

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published