CSS Positioning(定位)
CSS Position(定位)
CSS position 屬性,允許您將布局的一部分與另一部分重疊,還可以完成多年來通常需要使用多個(gè)表格才能完成的任務(wù)。例:
決定顯示在前面的元素!
元素可以重疊!
Positioning(定位)
CSS定位屬性允許你為一個(gè)元素定位。它也可以將一個(gè)元素放在另一個(gè)元素后面,并指定一個(gè)元素的內(nèi)容太大時(shí),應(yīng)該發(fā)生什么。
元素可以使用的頂部,底部,左側(cè)和右側(cè)屬性定位。然而,這些屬性無法工作,除非是先設(shè)定position屬性。他們也有不同的工作方式,這取決于定位方法.
有四種不同的定位方法。
Static 定位
HTML元素的默認(rèn)值,即沒有定位,元素出現(xiàn)在正常的流中。
靜態(tài)定位的元素不會受到top, bottom, left, right影響。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.static { position: static; border: 3px solid #73AD21; } </style> </head> <body> <h2>position: static;</h2> <p>使用 position: static; 定位的元素,無特殊定位,遵循正常的文檔流對象:</p> <div class="static"> 該元素使用了 position: static; </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Fixed 定位
元素的位置相對于瀏覽器窗口是固定位置。
即使窗口是滾動的它也不會移動:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.pos_fixed { position:fixed; top:30px; right:5px; } </style> </head> <body> <p class="pos_fixed">Some more text</p> <p><b>注意:</b> IE7和IE8支持只有一個(gè)!DOCTYPE指定固定值.</p> <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
注意: Fixed 定位在 IE7 和 IE8 下需要描述 !DOCTYPE 才能支持.
Fixed定位使元素的位置與文檔流無關(guān),因此不占據(jù)空間。
Fixed定位的元素和其他元素重疊。
Relative 定位
相對定位元素的定位是相對其正常位置。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h2.pos_left { position:relative; left:-20px; } h2.pos_right { position:relative; left:20px; } </style> </head> <body> <h2>This is a heading with no position</h2> <h2 class="pos_left">This heading is moved left according to its normal position</h2> <h2 class="pos_right">This heading is moved right according to its normal position</h2> <p>Relative positioning moves an element RELATIVE to its original position.</p> <p>The style "left:-20px" subtracts 20 pixels from the element's original left position.</p> <p>The style "left:20px" adds 20 pixels to the element's original left position.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
可以移動的相對定位元素的內(nèi)容和相互重疊的元素,它原本所占的空間不會改變。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h2.pos_top { position:relative; top:-50px; } </style> </head> <body> <h2>This is a heading with no position</h2> <h2 class="pos_top">This heading is moved upwards according to its normal position</h2> <p><b>注意:</b> 即使相對定位元素的內(nèi)容是移動,預(yù)留空間的元素仍保存在正常流動。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
相對定位元素經(jīng)常被用來作為絕對定位元素的容器塊。
Absolute 定位
絕對定位的元素的位置相對于最近的已定位父元素,如果元素沒有已定位的父元素,那么它的位置相對于<html>:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h2 { position:absolute; left:100px; top:150px; } </style> </head> <body> <h2>This is a heading with an absolute position</h2> <p>用絕對定位,一個(gè)元素可以放在頁面上的任何位置。標(biāo)題下面放置距離左邊的頁面100 px和距離頁面的頂部150 px的元素。.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Absolutely定位使元素的位置與文檔流無關(guān),因此不占據(jù)空間。
Absolutely定位的元素和其他元素重疊。
sticky 定位
sticky 英文字面意思是粘,粘貼,所以可以把它稱之為粘性定位。
position: sticky; 基于用戶的滾動位置來定位。
粘性定位的元素是依賴于用戶的滾動,在 position:relative 與 position:fixed 定位之間切換。
它的行為就像 position:relative; 而當(dāng)頁面滾動超出目標(biāo)區(qū)域時(shí),它的表現(xiàn)就像 position:fixed;,它會固定在目標(biāo)位置。
元素定位表現(xiàn)為在跨越特定閾值前為相對定位,之后為固定定位。
這個(gè)特定閾值指的是 top, right, bottom 或 left 之一,換言之,指定 top, right, bottom 或 left 四個(gè)閾值其中之一,才可使粘性定位生效。否則其行為與相對定位相同。
注意: Internet Explorer, Edge 15 及更早 IE 版本不支持 sticky 定位。 Safari 需要使用 -webkit- prefix (查看以下實(shí)例)。
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.sticky { position: -webkit-sticky; position: sticky; top: 0; padding: 5px; background-color: #cae8ca; border: 2px solid #4CAF50; } </style> </head> <body> <p>嘗試滾動頁面。</p> <p>注意: IE/Edge 15 及更早 IE 版本不支持 sticky 屬性。</p> <div class="sticky">我是粘性定位!</div> <div style="padding-bottom:2000px"> <p>滾動我</p> <p>來回滾動我</p> <p>滾動我</p> <p>來回滾動我</p> <p>滾動我</p> <p>來回滾動我</p> </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
重疊的元素
元素的定位與文檔流無關(guān),所以它們可以覆蓋頁面上的其它元素
z-index屬性指定了一個(gè)元素的堆疊順序(哪個(gè)元素應(yīng)該放在前面,或后面)
一個(gè)元素可以有正數(shù)或負(fù)數(shù)的堆疊順序:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> img { position:absolute; left:0px; top:0px; z-index:-1; } </style> </head> <body> <h1>This is a heading</h1> <img src="w3css.gif" width="100" height="140" /> <p>因?yàn)閳D像元素設(shè)置了 z-index 屬性值為 -1, 所以它會顯示在文字之后。</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
具有更高堆疊順序的元素總是在較低的堆疊順序元素的前面。
注意: 如果兩個(gè)定位元素重疊,沒有指定z - index,最后定位在HTML代碼中的元素將被顯示在最前面。
更多實(shí)例
實(shí)例:裁剪元素的外形
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> img { position:absolute; clip:rect(0px,60px,200px,0px); } </style> </head> <body> <img src="w3css.gif" width="100" height="140" /> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
此示例演示如何設(shè)置元素的外形。該元素被剪裁成這種形狀,并顯示出來。
實(shí)例:如何使用滾動條來顯示元素內(nèi)溢出的內(nèi)容
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.scroll { background-color:#00FFFF; width:100px; height:100px; overflow:scroll; } div.hidden { background-color:#00FF00; width:100px; height:100px; overflow:hidden; } </style> </head> <body> <p>overflow 屬性規(guī)定當(dāng)內(nèi)容溢出元素框時(shí)發(fā)生的事情。.</p> <p>overflow:scroll</p> <div class="scroll">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div> <p>overflow:hidden</p> <div class="hidden">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
這個(gè)例子演示了overflow屬性創(chuàng)建一個(gè)滾動條,當(dāng)一個(gè)元素的內(nèi)容在指定的區(qū)域過大時(shí)如何設(shè)置以適應(yīng)。
實(shí)例:如何設(shè)置瀏覽器自動溢出處理
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div { background-color:#00FFFF; width:150px; height:150px; overflow:auto; } </style> </head> <body> <p>overflow 屬性規(guī)定當(dāng)內(nèi)容溢出元素框時(shí)發(fā)生的事情。</p> <div> 當(dāng)你想更好的控制布局時(shí)你可以使用 overflow 屬性。嘗試修改 overflow 屬性為: visible, hidden, scroll, 或 inherit 并查看效果。 默認(rèn)值為 visible。 </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例:更改光標(biāo)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p>將鼠標(biāo)移動到這些字上改變鼠標(biāo)樣式cursor.</p> <span style="cursor:auto">auto</span><br> <span style="cursor:crosshair">crosshair</span><br> <span style="cursor:default">default</span><br> <span style="cursor:e-resize">e-resize</span><br> <span style="cursor:help">help</span><br> <span style="cursor:move">move</span><br> <span style="cursor:n-resize">n-resize</span><br> <span style="cursor:ne-resize">ne-resize</span><br> <span style="cursor:nw-resize">nw-resize</span><br> <span style="cursor:pointer">pointer</span><br> <span style="cursor:progress">progress</span><br> <span style="cursor:s-resize">s-resize</span><br> <span style="cursor:se-resize">se-resize</span><br> <span style="cursor:sw-resize">sw-resize</span><br> <span style="cursor:text">text</span><br> <span style="cursor:w-resize">w-resize</span><br> <span style="cursor:wait">wait</span><br> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
這個(gè)例子演示了如何設(shè)置瀏覽器來自動處理溢出。
所有的CSS定位屬性
"CSS" 列中的數(shù)字表示哪個(gè)CSS(CSS1 或者CSS2)版本定義了該屬性。
屬性 | 說明 | 值 | CSS |
---|---|---|---|
bottom | 定義了定位元素下外邊距邊界與其包含塊下邊界之間的偏移。 | auto length % inherit | 2 |
clip | 剪輯一個(gè)絕對定位的元素 | shape auto inherit | 2 |
cursor | 顯示光標(biāo)移動到指定的類型 | url auto crosshair default pointer move e-resize ne-resize nw-resize n-resize se-resize sw-resize s-resize w-resize text wait help | 2 |
left | 定義了定位元素左外邊距邊界與其包含塊左邊界之間的偏移。 | auto length % inherit | 2 |
overflow | 設(shè)置當(dāng)元素的內(nèi)容溢出其區(qū)域時(shí)發(fā)生的事情。 | auto hidden scroll visible inherit | 2 |
overflow-y | 指定如何處理頂部/底部邊緣的內(nèi)容溢出元素的內(nèi)容區(qū)域 | auto hidden scroll visible no-display no-content | 2 |
overflow-x | 指定如何處理右邊/左邊邊緣的內(nèi)容溢出元素的內(nèi)容區(qū)域 | auto hidden scroll visible no-display no-content | 2 |
position | 指定元素的定位類型 | absolute fixed relative static inherit | 2 |
right | 定義了定位元素右外邊距邊界與其包含塊右邊界之間的偏移。 | auto length % inherit | 2 |
top | 定義了一個(gè)定位元素的上外邊距邊界與其包含塊上邊界之間的偏移。 | auto length % inherit | 2 |
z-index | 設(shè)置元素的堆疊順序 | number auto inherit | 2 |