DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
寬度百分比怎麼算?轉換公式與實例一次看懂
編輯:布局實例     

把固定像素寬度換成百分比,是響應式布局的基本功。核心公式其實很簡單:

轉換公式

目標寬度 ÷ 上下文寬度 = 結果(百分比寬度)

也就是:當前元素的像素寬度 ÷ 其父元素的像素寬度 = 當前元素的百分比寬度。

範例結構

<div id="wrapper">
  <header></header>
  <section></section>
  <aside></aside>
</div>

原始 CSS(像素)

#wrapper { width:1000px; }
header  { width:1000px; }
aside   { width:220px; float:left; }
section { width:770px; float:right; }

換算成百分比

#wrapper { width:98%; }        /* 外圍寬度,數值可依需求調整 */
header  { width:100%; }        /* 1000÷1000=1=100% */
aside   { width:22%;  float:left;  }  /* 220÷1000=0.22=22% */
section { width:77%;  float:right; }  /* 770÷1000=0.77=77% */

只要記住「子元素寬度除以父元素寬度」這個原則,任何像素值都能輕鬆換算成百分比。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved