CSS Position Property
Master CSS positioning for dynamic web layouts
CSS Position Property Values
Static
Default value that follows normal document flow. Elements appear in order of markup from top to bottom.
Relative
Moves element relative to its natural position while maintaining its space in document flow.
Absolute
Removes element from document flow and positions relative to nearest positioned parent.
Fixed
Positions element relative to viewport and stays put during scrolling.
Browsers render page content in the order it's listed in the code. The topmost markup displays first, followed by content listed later in the HTML.
Setting Up the Exercise
Open Project Files
Navigate to Desktop > Class Files > Advanced HTML CSS Class > Position Property folder
Preview in Browser
Open position.html in Chrome to use Developer Tools for testing
Examine Structure
Note the 3 parent divs with colorful and static gray child divs for comparison
Positive vs Negative Position Values
| Feature | Positive Values | Negative Values |
|---|---|---|
| Top Property | Pushes element downward | Moves element upward |
| Left Property | Moves element to the right | Moves element to the left |
| Direction Rule | Opposite of property name | Same as property name |
Absolute Positioning Trade-offs
Without a positioned parent, absolutely positioned elements use the HTML document as reference, often causing them to appear at unexpected locations.
Creating Positioning Context
Identify Parent Container
Locate the parent element that should contain the absolutely positioned child
Add Relative Position
Set position: relative on the parent element to create positioning context
Position Child Element
Use top, right, bottom, left coordinates on absolute child relative to parent
Relatively-positioning the parent is natural because it doesn't need coordinate instructions and stays in its normal document position.
Fixed Positioning Characteristics
Viewport Reference
Fixed elements only take positioning orders from the browser window itself, not any parent elements.
Scroll Independence
Element stays in exact position while page content scrolls, creating sticky headers and navigation.
Document Flow Removal
Like absolute positioning, fixed elements are removed from normal flow and collapse to minimum size.
Creating Full-Width Fixed Headers
Removes from document flow and enables viewport positioning
Places header at very top of browser window
Stretches header across entire screen width
Verify header remains stationary while content scrolls beneath
Key Takeaways
