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

css中position屬性有哪些用法

Mar 16, 2020 am 10:42 AM
css position

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>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>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>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>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

熱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

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

熱工具

記事本++7.3.1

記事本++7.3.1

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

HTML,CSS和JavaScript的角色:核心職責 HTML,CSS和JavaScript的角色:核心職責 Apr 08, 2025 pm 07:05 PM

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

了解HTML,CSS和JavaScript:初學者指南 了解HTML,CSS和JavaScript:初學者指南 Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

bootstrap怎麼設置框架 bootstrap怎麼設置框架 Apr 07, 2025 pm 03:27 PM

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

bootstrap怎麼寫分割線 bootstrap怎麼寫分割線 Apr 07, 2025 pm 03:12 PM

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

bootstrap怎麼插入圖片 bootstrap怎麼插入圖片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

bootstrap按鈕怎麼用 bootstrap按鈕怎麼用 Apr 07, 2025 pm 03:09 PM

如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本

bootstrap怎麼調整大小 bootstrap怎麼調整大小 Apr 07, 2025 pm 03:18 PM

要調整 Bootstrap 中元素大小,可以使用尺寸類,具體包括:調整寬度:.col-、.w-、.mw-調整高度:.h-、.min-h-、.max-h-

See all articles