萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

純 Div+CSS 畫圓角盒子 (Sliding Doors 無圖片版 / CSS3 Border-radius)

歷史技法:Sliding Doors (漸進式邊框模擬)

早期無 border-radius 時,用多層 <b> 或 <p> 疊加 1px 高、邊距遞減的色塊模擬圓角。原始碼冗長、維護困難,**已淘汰**,僅供考古參考。

現代標準:Border-radius (CSS3)

.rounded-box {
  border-radius: 8px;           /* 四角同半徑 */
  /* 或分別指定 */
  border-radius: 8px 4px 12px 0; /* 左上、右上、右下、左下 */
  /* 橢圓角 */
  border-radius: 50% / 20%;     /* 水平半徑 50%、垂直半徑 20% */
}

完整範例:帶陰影、內距、背景的圓角卡片

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>CSS3 圓角卡片</title>
<style>
.card {
  width:320px; margin:2rem auto; padding:1.5rem;
  background:#fff; border-radius:12px;
  box-shadow:0 4px 12px rgba(0,0,0,.1);
  font:16px/1.5 "Microsoft JhengHei", sans-serif;
}
.card h3 { margin-top:0; color:#1976d2; }
.card p { color:#333; }
</style></head><body>
<div class="card">
  <h3>圓角卡片標題</h3>
  <p>使用 border-radius 單行即可實現完美圓角,無需切圖、無需巢狀標籤,支援所有現代瀏覽器 (IE9+)。</p>
</div>
</body></html>

IE6-8 兼容方案 (若必須支援)

  • PIE.htc:behavior:url(PIE.htc) 讓 IE6-8 支援 border-radius、box-shadow、linear-gradient。
  • DD_roundies:專門模擬圓角的 JS 庫。
  • 優雅降級:IE 顯示直角,不影響功能,體驗可接受。

進階:八角形 / 獨特形狀

.octagon {
  width:100px; height:100px; background:#2196f3;
  clip-path: polygon(30% 0, 70% 0, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0 70%, 0 30%);
}

clip-path 可任意裁切形狀,支援度較 border-radius 略低 (IE 不支援),但可作為漸進增強。

copyright © 萬盛學電腦網 all rights reserved