元素寬高為 0,四邊框寬度相等時形成四個三角形拼接的正方形。透過設置三邊透明、一邊有色,即得單一方向三角形。
保留上邊框色,其餘設透明 → 向下三角形:
四方向控制:
.arrow {
float: left;
width: 0; height: 0; font-size: 0; line-height: 0;
border-style: solid; border-width: 10px;
border-color: #000 transparent transparent transparent; /* 向下 */
}
/* 向上 */ border-color: transparent transparent #000 transparent;
/* 向左 */ border-color: transparent #000 transparent transparent;
/* 向右 */ border-color: transparent transparent transparent #000;
<div class="demo">
<div class="content">動態內容,高度自適應</div>
<b class="bottom-arrow"></b>
</div>
.demo { position: relative; }
.content {
position: relative; z-index: 1;
border: 1px solid #666; border-radius: 3px;
background: #fff; padding: 5px;
box-shadow: 3px 3px 4px #999;
/* IE 陰影濾鏡 */
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#999999');
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='#999999')";
}
.bottom-arrow {
position: absolute; top: 10px; left: 20px;
width: 0; height: 0; font-size: 0; line-height: 0;
border-style: solid; border-width: 10px;
border-color: #000 transparent transparent transparent;
/* IE6 透明邊框 hack */
_border-color: #000 tomato tomato tomato;
_filter: chroma(color=tomato);
}
transparent 關鍵字tomato) 作為透明色佔位filter: chroma(color=tomato) 將該顏色設為透明::before/::after 偽元素替代額外 HTML 標籤transform: rotate(45deg) 實現更靈活角度clip-path: polygon(...) 繪製複雜箭頭transition: border-color .2s, transform .2s