CSS中position屬性詳解

來源:互聯網
上載者:User
CSS中position屬性是設定元素的定位類型,它有absolute(絕對位置)、relative(相對定位)、static(靜態定位,預設值)、fixed(固定定位)四種參數。

  • static:預設。位置設定為 static 的元素,它始終會處於頁面流給予的位置(static 元素會忽略任何 top、bottom、left 或 right 聲明)。

  • relative:位置被設定為 relative 的元素,可將其移至相對於其正常位置的地方,因此 "left:20" 會將元素移至元素正常位置左邊 20 個像素的位置。

  • absolute:位置設定為 absolute 的元素,可定位於相對於包含它的元素的指定座標。此元素的位置可通過 "left"、"top"、"right" 以及 "bottom" 屬性來規定。

  • fixed:位置被設定為 fixed 的元素,可定位於相對於瀏覽器視窗的指定座標。此元素的位置可通過 "left"、"top"、"right" 以及"bottom" 屬性來規定。不論視窗滾動與否,元素都會留在那個位置。工作於 IE7(strict 模式)。

樣本

使用position屬性定位<h2>元素,代碼如下

<!DOCTYPE html><html><head><meta charset="utf-8"> <title>php中文網</title> <style>h2{position:absolute;left:100px;top:150px;}</style></head><body><h2>這是一個絕對位置了的標題</h2><p>用絕對位置,一個元素可以放在頁面上的任何位置。標題下面放置距離左邊的頁面100 px和距離頁面的頂部150 px的元素。.</p></body></html>
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.