構建輕量級數據視覺化的三大核心 CSS 模式,代碼可直接複製適配項目,零依賴、語意化、易擴展。
適用:單指標、進度條、技能熟練度
<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>
<div class="graph">
<strong class="bar" style="width: 24%;">24%</strong>
</div>
適用:多指標對比、儀表盤卡片、問卷結果
<style>
.chart { margin: 0; padding: 0; }
.chart dt {
position: relative; clear: both; float: left;
width: 104px; height: 20px; line-height: 20px;
margin-right: 17px; font-size: .75em; text-align: right;
}
.chart dd {
position: relative; float: left;
width: 197px; height: 20px; margin: 0 0 15px;
background: url(g_colorbar.jpg); /* 軌道背景 */
}
* html .chart dd { float: none; } /* IE6 hack */
.chart dd div {
position: relative; height: 20px; width: 75%;
background: url(g_colorbar2.jpg); /* 進度填充 */
text-align: right;
}
.chart dd div strong {
position: absolute; right: -5px; top: -2px;
display: block; width: 9px; height: 24px;
background: url(g_marker.gif); /* 端點標記 */
text-indent: -9999px; overflow: hidden;
}
</style>
<dl class="chart">
<dt>Love Life</dt>
<dd><div style="width:25%;"><strong>25%</strong></div></dd>
<dt>Education</dt>
<dd><div style="width:55%;"><strong>55%</strong></div></dd>
<dt>War Craft 3 Rank</dt>
<dd><div style="width:75%;"><strong>75%</strong></div></dd>
</dl>
適用:統計分佈、錯誤等級分佈、流量時段對比
<style>
#vertgraph {
width: 378px; height: 207px; position: relative;
background: url(g_backbar.gif) no-repeat; /* 底部座標軸 */
}
#vertgraph ul { margin:0; padding:0; width:100%; height:100%; }
#vertgraph li {
position: absolute; bottom: 34px; width: 28px;
background: url(g_colorbar3.jpg) no-repeat; /* 柱狀漸變 */
text-align: center; font-weight: bold; color: white; line-height: 2.5em;
}
#vertgraph li.critical { left: 24px; background-position: 0; }
#vertgraph li.high { left: 101px; background-position: -28px; }
#vertgraph li.medium { left: 176px; background-position: -56px; }
#vertgraph li.low { left: 251px; background-position: -84px; }
#vertgraph li.info { left: 327px; background-position: -112px; }
</style>
<div id="vertgraph">
<ul>
<li class="critical" style="height: 150px;">22</li>
<li class="high" style="height: 80px;">7</li>
<li class="medium" style="height: 50px;">3</li>
<li class="low" style="height: 90px;">8</li>
<li class="info" style="height: 40px;">2</li>
</ul>
</div>
📦 這三個模式覆蓋 80%+ 輕量圖表需求,建議封裝為組件庫(React/Vue/Svelte)或 Web Components 復用。