CSS Positioning: fixed

Here's an English summary of the CSS positioning lesson from video transcript

https://www.bilibili.com/video/BV1qJrgYNEKG?t=1.0&p=81

📐 CSS Positioning Overview

What We've Learned So Far

Before positioning, we could only style content (colors, sizes, fonts, borders, backgrounds). Now we're learning positioning to control layout — where elements actually sit on the page.


🎯 The Five Positioning Properties

PropertyPurpose
positionSpecifies the positioning type
topDistance from the top
bottomDistance from the bottom
leftDistance from the left
rightDistance from the right

Key rule: You can only use top/right/bottom/left after you've set a position value (other than static).


📄 What is "Document Flow"?

Document Flow: The browser's default way of arranging elements — from top to bottom, and left to right within each row.

Key Concept: Some positioning types 脱离文档流 (remove elements from the document flow), meaning:

  • The element no longer occupies space in the layout

  • Other elements can move into its original position


🧩 The Positioning Values

1. static (Default)

  • What it does: No positioning; element stays in normal document flow

  • top/right/bottom/left: ❌ 无效 (have no effect)

  • Usage: Rarely used explicitly (it's the default)

2. fixed

  • What it does: Positions the element relative to the browser viewport

  • Behavior: Stays in the same place even when scrolling

  • Document Flow: ✅ 脱离文档流 (removed from flow)

  • Example:

css

.fixed-div {
    position: fixed;
    right: 10px;  /* 10px from the right edge of viewport */
    top: 20px;    /* 20px from the top edge of viewport */
}

Key point about right values:

  • right: 0; → element touches the right edge of viewport

  • right: 10px; → element is 10px away from the right edge (拉开距离)

  • right: -10px; → element overlaps the right edge by 10px (拉近距离)

3. relative

  • What it does: Positions element relative to its normal position in the flow

  • Document Flow: ❌ 不脱离文档流 (stays in flow — original space is preserved)

  • Usage: Commonly used as a parent container for absolute children ("父相子绝")

4. absolute

  • What it does: Positions element relative to its nearest positioned ancestor (non-static)

  • If no positioned ancestor exists: Positions relative to <html>

  • Document Flow: ✅ 脱离文档流 (removed from flow)

  • Classic pattern: Parent relative + Child absolute

5. sticky

  • What it does: Switches between relative and fixed based on scroll position

  • Document Flow: ❌ 不脱离文档流 (stays in flow)

  • Requires: A threshold value like top: 30px;


📊 Quick Reference Table

Position ValueReference PointLeaves Document Flow?top/left Work?
staticNone (default)❌ No❌ No
relativeIts own normal position❌ No✅ Yes
absoluteNearest positioned ancestor✅ Yes✅ Yes
fixedBrowser viewport✅ Yes✅ Yes
stickyViewport (at threshold)❌ No✅ Yes

🎨 The "Parent Relative, Child Absolute" Pattern

This is the most common positioning pattern:

html

<style>
  .parent {
    position: relative;  /* Anchor for child */
  }
  .child {
    position: absolute;
    top: 0;
    right: 0;
  }
</style>

<div class="parent">
  <div class="child">I'm in the top-right corner!</div>
</div>

Why it works: The child positions itself relative to the parent because the parent has position: relative;.


💡 Key Takeaways

  1. Positioning is for layout — not just styling content

  2. relative doesn't leave the flow — it keeps its original space

  3. absolute and fixed leave the flow — they become "floating" elements

  4. "父相子绝" (Parent Relative, Child Absolute) is the go-to pattern

  5. sticky is great for sidebars — it's smooth and respects footer boundaries

https://www.bilibili.com/video/BV1qJrgYNEKG?t=1.0&p=81

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值