萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

HTML5 絕對定位元素對齊與偏移模式:四邊貼齊 + margin 微調 + 尺寸自適應

絕對定位元素(position:absolute)脫離文檔流,相對最近的已定位祖先元素定位。核心模式:

四邊貼齊基準

方向貼齊屬性對應反向屬性
左left: 0right: auto
右right: 0left: auto
頂top: 0bottom: auto
底bottom: 0top: auto

margin 偏移(不改變基準點)

.abs-left  { position:absolute; left:0; right:auto; margin-left: 20px;  /* 右移 20px */ }
.abs-right { position:absolute; left:auto; right:0; margin-right: -10px; /* 左移 10px */ }
.abs-top   { position:absolute; top:0; bottom:auto; margin-top: 15px;   /* 下移 15px */ }
.abs-bottom{ position:absolute; top:auto; bottom:0; margin-bottom: -5px; /* 上移 5px */ }

尺寸控制

  • 固定尺寸:width: 200px; height: 100px;
  • 內容自適應:width: auto; height: auto;(收縮包裹)
  • 雙邊拉伸:left: 0; right: 0; width: auto; → 橫向滿鋪

適用範圍

所有元素。注意:絕對定位元素的百分比尺寸相對「定位最近的祖先元素」,而非視窗或父元素。

copyright © 萬盛學電腦網 all rights reserved