css中position有哪些不同的值-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)css中position有哪些不同的值,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對外擴展宣傳的重要窗口,一個合格的網(wǎng)站不僅僅能為公司帶來巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺,成都創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:成都房屋鑒定網(wǎng)站設(shè)計全網(wǎng)營銷推廣解決方案、網(wǎng)站設(shè)計等建站排名服務(wù)。

position屬性

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

•static
•relative
•fixed
•absolute
•sticky

然后使用top,bottom,left和right屬性定位元素。但是,除非首先設(shè)置position屬性,否則這些屬性將不起作用。根據(jù)位置值,它們的工作方式也不同。

position:static;

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

這個<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>
<h3>position: static;</h3>
<p>一個位置為position:static; 沒有任何特殊的定位; 它是始終根據(jù)頁面的正常流程定位:</p>
<div class="static">
    這個div元素的位置為:static;
</div>
</body>
</html>

position:relative;

具有position:relative;相對于其正常位置定位的元素。設(shè)置相對定位元素的top,bottom,left和right屬性將使其遠離其正常位置進行調(diào)整。其他內(nèi)容不會被調(diào)整以適應(yīng)元素留下的任何空白。

這個<div>元素的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>
<h3>position: relative;</h3>
<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>
<h3>position:fixed;</h3>
<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>
<h3>position: absolute;</h3>
<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;根據(jù)用戶的滾動位置定位元素。粘性元素在relative和之間切換fixed,具體取決于滾動位置。它被相對定位,直到在視口中滿足給定的偏移位置 - 然后它“粘住”到位(如位置:fixed)。

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

在此示例中,top: 0當(dāng)您到達其滾動位置時,粘性元素會粘到頁面頂部。

<!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>嘗試在此框架內(nèi)<b>滾動</b>以了解粘性定位的工作原理。</p>
<p>注意:IE/Edge15及更早版本不支position: sticky;。</p>
<div class="sticky">我很粘!</div>
<div style="padding-bottom:2000px">
    <p>在此示例中,當(dāng)您到達其滾動位置時,粘性元素會粘到頁面頂部(頂部: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定位屬性

屬性描述
bottom設(shè)置定位框的底部邊緣
clip剪輯一個絕對定位的元素
left設(shè)置定位框的左邊緣
position指定元素的定位類型
right設(shè)置定位框的右邊緣
top設(shè)置定位框的上邊緣
z-index設(shè)置元素的堆棧順序

css的基本語法是什么

css的基本語法是:1、css規(guī)則由選擇器和一條或多條聲明兩個部分構(gòu)成;2、選擇器通常是需要改變樣式的HTML元素;3、每條聲明由一個屬性和一個值組成;4、屬性和屬性值被冒號分隔開。

看完上述內(nèi)容,你們對css中position有哪些不同的值有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)頁題目:css中position有哪些不同的值-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article16/dgiddg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站內(nèi)鏈、App設(shè)計、云服務(wù)器、全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)