
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 引入
- CSS - 測量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 內聯塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - Clearfix
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 轉換
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪輯
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 滾動溢位
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊大小
- CSS - 最小塊大小
- CSS - 混合模式
- CSS - 最大內聯大小
- CSS - 最小內聯大小
- CSS - 偏移
- CSS - 重音顏色
- CSS - 使用者選擇
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - 重要
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框影像
- CSS - 多背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾斷開
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2d 變換
- CSS - 3d 變換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式影像
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS - 定位
CSS **定位**有助於操作網頁中任何元素的位置。在本教程中,我們將學習 position 屬性及其關聯的值。
目錄
什麼是 CSS 位置?
在 CSS 中,**position** 屬性用於透過調整網頁中元素的位置來建立浮動元素、浮動側邊欄和其他互動式功能。
除了 position 屬性之外,其他屬性如 **top,bottom,right** 和 **left** 用於控制其在頁面上的精確位置。它們指定元素與其邊緣的偏移量。我們將在本教程中看到這些示例。
語法
以下是 css position 的可能值。
position: static | relative | absolute | fixed | sticky;
靜態定位元素
當您使用 **position: static** 屬性時,元素將在文件流中正常定位。left、right、top、bottom 和 z-index 屬性不會影響元素。這是 position 屬性的預設值。
示例
在此示例中,我們定義了兩個靜態定位的 div 元素和一個相對定位的元素。檢視區別。
<!DOCTYPE html> <html lang="en"> <head> <style> .container { padding: 20px; border: 1px solid #ccc; } .static-element { position: static; background-color: lightblue; padding: 10px; margin: 10px 0; } .non-static-element { position: relative; top: 20px; background-color: lightgreen; padding: 10px; } </style> </head> <body> <div class="container"> <div class="static-element"> This is a static element (default position). </div> <div class="non-static-element"> This element is not static and is moved 20px down. </div> <div class="static-element"> This is another static element (default position). </div> </div> </body> </html>
相對定位元素
CSS **position: relative** 屬性相對於其在頁面上的原始位置定位元素。您可以使用 left、right、top 和 bottom 屬性來移動元素,但它仍將在文件流中佔用空間。
示例
在此示例中,我們定義了一個靜態定位的 div 元素和一個相對定位的元素。檢視區別。
<!DOCTYPE html> <html lang="en"> <head> <style> *{ padding: 15px; } .container { border: 1px solid #ccc; } .static-element { position: static; background-color: lightblue; } .relative-element { position: relative; top: 20px; left: 30px; background-color: lightgreen; } .normal-flow { background-color: lightcoral; margin: 10px 0; } </style> </head> <body> <div class="container"> <div class="static-element"> This is a static element (default position). </div> <div class="relative-element"> This element is relatively positioned 20px down and 30px right. </div> <div class="normal-flow"> This element is in the normal document flow, after the relative element. </div> </div> </body> </html>
絕對定位元素
具有 **position: absolute** 的元素將從文件流中移除,並相對於其最近的已定位祖先(如果有)進行定位。如果沒有已定位的祖先,則元素相對於視口進行定位。
您可以使用 top、right、bottom 和 left 屬性來指定元素相對於其包含塊的位置。
示例
此示例顯示了 position absolute 的用法
<!DOCTYPE html> <html lang="en"> <head> <style> *{ padding: 15px; } .container { /* This makes container positioned ancestor */ position: relative; width: 70%; height: 200px; border: 1px solid #ccc; } .static-element { position: static; background-color: lightblue; padding: 10px; margin: 10px 0; } .absolute-element { position: absolute; /* 50px from top of nearest positioned ancestor */ top: 50px; /* 30px from left of nearest positioned ancestor */ left: 30px; background-color: lightgreen; padding: 10px; } </style> </head> <body> <div class="container"> <div class="static-element"> This is a static element (default position). </div> <div class="absolute-element"> This element is absolutely positioned 30px from top and 50px from left. </div> </div> </body> </html>
固定定位元素
在 CSS 中,**position: fixed** 屬性用於使元素即使在使用者滾動時也保持在螢幕上的相同位置。然後,您可以使用 left、right、top 和 bottom 屬性將元素定位到所需位置。
示例
在此示例中,您可以看到當您向下滾動時,段落元素仍然存在。
<html> <head> <style> div { width: 100%; height: 500px; background-color: lightgrey; overflow: auto; padding: 15px; } .fixed-position { position: fixed; top: 50%; left: 20%; padding: 5px; background-color: white; } </style> </head> <body> <div> <p>You can Scroll down...</p> <p class="fixed-position"> Tutorialspoint CSS Position Fixed </p> <p> White screen will remain at 50% height from top </p> </div> </body> </html>
粘性定位元素
在 CSS 中,**position: sticky** 屬性用於使任何元素即使在使用者滾動時也保持在容器頂部。然後,您可以使用 left、right、top 和 bottom 屬性將元素定位到所需位置。
**position: sticky** 屬性是 **position: relative** 和 **position: fixed** 屬性的組合。
示例
在此示例中,您可以看到當您滾動時,fixed 移動到頂部
<html> <head> <style> div { width: 100%; height: 500px; background-color: lightgrey; overflow: auto; padding: 15px; } .sticky-position { position: sticky; top: 50%; left: 20%; padding: 5px; background-color: white; } </style> </head> <body> <div> <p>You can Scroll down...</p> <p class="sticky-position"> TutorialsPoint CSS Position Fixed </p> <p> White screen will stick at top of screen when you scroll down. </p> </div> </body> </html>
在影像中定位文字
在以下示例中,您可以使用 **position: absolute** 屬性在影像內不同方向定位文字。文字元素位於左上角、右上角、左下角和右下角。
示例
在以下示例中,我們在影像上定位了文字。
<html> <head> <style> .container { position: relative; border: 2px solid #ef2c2c; } .center { position: absolute; top: 45%; width: 100%; text-align: center; } .top-left { position: absolute; top: 12px; left: 30px; } .top-right { position: absolute; top: 12px; right: 30px; } .bottom-left { position: absolute; bottom: 12px; left: 30px; } .bottom-right { position: absolute; bottom: 12px; right: 30px; } img { width: 100%; opacity: 0.7; } </style> </head> <body> <div class="container"> <img src="/css/images/red-flower.jpg" width="1000px" height="350px"> <h3 class="center"> Text at Center </h3> <h3 class="top-left"> Text at Top Left </h3> <h3 class="top-right"> Text at Top Right </h3> <h3 class="bottom-left"> Text at Bottom Left</h3> <h3 class="bottom-right"> Text at Bottom Right </h3> </div> </body> </html>
CSS 與位置相關的屬性
以下是所有與 **position** 相關的 CSS 屬性列表
屬性 | 描述 | 示例 |
---|---|---|
bottom | 與 position 屬性一起使用以放置元素的底部邊緣。 | |
clip | 設定元素的剪下蒙版。 | |
left | 與 **position** 屬性一起使用以放置元素的左邊緣。 | |
overflow | 確定如何呈現溢位內容。 | |
right | 與 **position** 屬性一起使用以放置元素的右邊緣。 | |
top | 設定元素的定位模型。 | |
vertical-align | 設定元素的垂直位置。 | |
z-index | 設定當前元素的渲染層。 |