搜尋
首頁web前端css教學css中position屬性有哪些用法

css中position屬性有哪些用法

Mar 16, 2020 am 10:42 AM
cssposition

css中position屬性有哪些用法

position屬性

position屬性指定用於元素的定位方法的類型(靜態,相對,固定,絕對或黏性)。有五種不同的值:

•static
•relative
•fixed
•absolute
•sticky

然後使用top,bottom, left和right屬性定位元素。但是,除非先設定position屬性,否則這些屬性將無法運作。根據位置值,它們的工作方式也不同。

position:static;

預設情況下,HTML元素定位為靜態。靜態定位元素不受top,bottom,left和right屬性的影響。元素position:static;沒有以任何特殊方式定位; 它總是根據頁面的正常流程定位:

(推薦教學:CSS入門教學

這個< ;div>元素的position:static;

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css</title>
    <style>
        div.static {
            position: static;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2 id="position-nbsp-static">position: static;</h2>
<p>一个位置为position:static; 没有任何特殊的定位; 它是始终根据页面的正常流程定位:</p>
<div class="static">
    这个div元素的位置为:static;
</div>
</body>
</html>

position:relative;

具有position:relative;相對於其正常位置定位的元素。設定相對定位元素的top,bottom,left和right屬性將使其遠離其正常位置進行調整。其他內容不會被調整以適應元素留下的任何空白。

這個

元素的position:relative;
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css</title>
    <style>
        div.relative {
            position: relative;
            left: 30px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2 id="position-nbsp-relative">position: relative;</h2>
<p>position:relative的元素; 相对于其正常位置定位:</p>
<div class="relative">
    这个div元素有position: relative;
</div>
</body>
</html>

position:fixed;

元素position:fixed;相對於視窗定位,這意味著即使頁面滾動,它也始終保持在同一位置。 top,bottom,left和right屬性用於定位元素。固定元素不會在頁面中留下通常位於其中的間隙。注意頁面右下角的固定元素。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css教程(jc2182.com)</title>
    <style>
        div.fixed {
            position: fixed;
            bottom: 0;
            right: 0;
            width: 300px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2 id="position-fixed">position:fixed;</h2>
<p>position:fixed; 相对于视口定位,这意味着即使页面滚动,它也始终保持在同一位置:</p>
<div class="fixed">
    这个div元素有position: fixed;
</div>
</body>
</html>

position:absolute;

具有position:absolute;相對於最近定位的祖先定位的元素(而不是相對於視口定位,如fixed)。然而; 如果絕對定位元素沒有定位祖先,它將使用文檔正文,並隨頁面滾動一起移動。注意: 「定位」元素的位置是除了static之外的任何元素。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css教程(jc2182.com)</title>
    <style>
        div.relative {
            position: relative;
            width: 400px;
            height: 200px;
            border: 3px solid #73AD21;
        }
        div.absolute {
            position: absolute;
            top: 80px;
            right: 0;
            width: 200px;
            height: 100px;
            border: 3px solid #73AD21;
        }
    </style>
</head>
<body>
<h2 id="position-nbsp-absolute">position: absolute;</h2>
<p>position:absolute;的元素; 相对于最近定位的祖先定位(而不是相对于视口定位,如fixed):</p>
<div class="relative">这个div元素有 position: relative;
    <div class="absolute">这个div元素有 position: absolute;</div>
</div>
</body>
</html>

position:sticky;

position:sticky;根據使用者的捲動位置定位元素。黏性元素在relative和之間切換fixed,取決於滾動位置。它被相對定位,直到在視口中滿足給定的偏移位置 - 然後它“粘住”到位(如位置:fixed)。

注意: Internet Explorer,Edge 15及更早版本不支援黏性定位。 Safari需要-webkit-前綴(請參閱下面的範例)。您還必須指定的至少一個top,right,bottom或left為黏稠的定位工作。

在此範例中,top: 0當您到達其捲動位置時,黏性元素會黏到頁面頂部。

<!DOCTYPE html>
<html>
<head>
    <style>
        div.sticky {
            position: -webkit-sticky;
            position: sticky;
            top: 0;
            padding: 5px;
            background-color: #cae8ca;
            border: 2px solid #4CAF50;
        }
    </style>
</head>
<body>
<p>尝试在此框架内<b>滚动</b>以了解粘性定位的工作原理。</p>
<p>注意:IE/Edge15及更早版本不支position: sticky;。</p>
<div class="sticky">我很粘!</div>
<div style="padding-bottom:2000px">
    <p>在此示例中,当您到达其滚动位置时,粘性元素会粘到页面顶部(顶部:0)。</p>
    <p>向上滚动以消除粘性。</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
    <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
</body>
</html>

相關影片教學推薦:css影片教學

#

以上是css中position屬性有哪些用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:脚本之家。如有侵權,請聯絡admin@php.cn刪除
丟失的CSS技巧cohost.org丟失的CSS技巧cohost.orgApr 25, 2025 am 09:51 AM

在這篇文章中,布萊克·莫里(Blackle Mori)向您展示了一些駭客,同時試圖推動同位HTML支持的極限。如果您敢於使用這些,以免您也被標記為CSS罪犯。

光標的下一個CSS樣式光標的下一個CSS樣式Apr 23, 2025 am 11:04 AM

具有CSS的自定義光標很棒,但是我們可以將JavaScript提升到一個新的水平。使用JavaScript,我們可以在光標狀態之間過渡,將動態文本放置在光標中,應用複雜的動畫並應用過濾器。

世界碰撞:使用樣式查詢的鑰匙幀碰撞檢測世界碰撞:使用樣式查詢的鑰匙幀碰撞檢測Apr 23, 2025 am 10:42 AM

互動CSS動畫和元素相互啟動的元素在2025年似乎更合理。雖然不需要在CSS中實施乒乓球,但CSS的靈活性和力量的增加,可以懷疑Lee&Aver Lee有一天會成為一種

使用CSS背景過濾器進行UI效果使用CSS背景過濾器進行UI效果Apr 23, 2025 am 10:20 AM

有關利用CSS背景濾波器屬性來樣式用戶界面的提示和技巧。您將學習如何在多個元素之間進行背景過濾器,並將它們與其他CSS圖形效果集成在一起以創建精心設計的設計。

微笑嗎?微笑嗎?Apr 23, 2025 am 09:57 AM

好吧,事實證明,SVG的內置動畫功能從未按計劃進行棄用。當然,CSS和JavaScript具有承載負載的能力,但是很高興知道Smil並沒有像以前那樣死在水中

'漂亮”在情人眼中'漂亮”在情人眼中Apr 23, 2025 am 09:40 AM

是的,讓#039;跳上文字包裝:Safari Technology Preview In Pretty Landing!但是請注意,它與在鉻瀏覽器中的工作方式不同。

CSS-tricks編年史XLIIICSS-tricks編年史XLIIIApr 23, 2025 am 09:35 AM

此CSS-tricks更新了,重點介紹了年鑑,最近的播客出現,新的CSS計數器指南以及增加了幾位新作者,這些新作者貢獻了有價值的內容。

tailwind的@Apply功能比聽起來更好tailwind的@Apply功能比聽起來更好Apr 23, 2025 am 09:23 AM

在大多數情況下,人們展示了@Apply的@Apply功能,其中包括Tailwind的單個property實用程序之一(會改變單個CSS聲明)。當以這種方式展示時,@Apply聽起來似乎很有希望。如此明顯

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具