核心 CSS
.graph {
position: relative; /* 修正 IE 定位基準 */
width: 200px;
border: 1px solid #f00;
padding: 2px;
}
.graph .bar {
display: block;
position: relative;
background: #f00;
text-align: center;
color: #333;
height: 2em;
line-height: 2em; /* 文字垂直置中 */
}
HTML 使用
<div class="graph">
<strong class="bar" style="width: 64%;">cssbbs.com 64%</strong>
</div>
原理解析
.graph 為容器,固定寬度、邊框、內距。
.bar 為進度條本體,width:xx% 直接映射數值。
position:relative 確保 .bar 在 .graph 內定位(修正 IE6/7 錯誤基準)。
line-height:height 實現單行文字垂直置中。
擴展建議
- 動畫過渡:
.bar { transition: width 0.5s; } 數值變化平滑動畫。
- 多色段: 多個
.bar 疊加或並排,寬度累加 100%。
- 語意化: 用
<progress value="64" max="100"> 原生標籤(HTML5),CSS 僅美化,無障礙最佳。
- 響應式: 容器
width:100%; max-width:400px; 適應螢幕。