content: attr(屬性名);
僅能用於 content 屬性(偽元素 ::before/::after),CSS3 擴展草案擬開放至更多屬性。
<h3 data-prefix="🔥 熱門">文章標題</h3>
h3::before { content: attr(data-prefix) " "; color: #f60; }
@media print {
a[href]::after {
content: " (" attr(href) ")";
font-size: 0.85em; color: #555; word-break: break-all;
}
}
<button data-tip="複製成功">複製</button>
button { position: relative; }
button::after {
content: attr(data-tip);
position: absolute; bottom: 120%; left: 50%; transform: translateX(-50%);
padding: 4px 8px; background: #333; color: #fff; border-radius: 4px;
white-space: nowrap; opacity: 0; pointer-events: none; transition: .2s;
}
button:hover::after { opacity: 1; }
content: attr():全瀏覽器(含 IE8+)支援。attr(type) 類型轉換(如 attr(data-size px)):**尚未廣泛支援**,僅實驗性實現。value、checked 需 JS)。content 屬性(現階段)。