利用容器固定寬度作為 100% 基準,內部區塊以百分比寬度呈現數值比例,配合邊框與背景色形成長條圖。
<div class="graph">
<strong class="bar" style="width: 24%;">24%</strong>
</div>
.graph {
position: relative; /* 修正 IE 定位基準 */
width: 200px; /* 圖表總寬度 = 100% */
border: 1px solid #B1D632;
padding: 2px;
}
.graph .bar {
display: block;
position: relative;
background: #B1D632;
text-align: center;
color: #333;
height: 2em;
line-height: 2em; /* 垂直置中文字 */
}
.graph .bar span { position: absolute; left: 1em; } /* 數值微調位置 */
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS Bar Chart Demo</title>
<style>
.graph { position: relative; width: 200px; border: 1px solid #B1D632; padding: 2px; }
.graph .bar { display: block; position: relative; background: #B1D632; text-align: center; color: #333; height: 2em; line-height: 2em; }
.graph .bar span { position: absolute; left: 1em; }
</style>
</head>
<body>
<div class="graph"><strong class="bar" style="width: 24%;">24%</strong></div>
<br>
<div class="graph"><strong class="bar" style="width: 60%;">60%</strong></div>
</body>
</html>
.graph 內放多個 .bar,並設定 margin-bottom 間距。transition: width 0.5s ease-out; 實現載入動畫。height,.bar 用 height 百分比與 position: absolute; bottom: 0。<div role="progressbar" aria-valuenow="24" aria-valuemin="0" aria-valuemax="100"> 增強無障礙。簡易儀表板、技能熟練度展示、投票結果、載入進度條等輕量級資料視覺化需求,無需引入 Chart.js、D3.js 等重型庫。