html – CSS – 使用数据属性添加颜色 – attr(数据颜色)

前端之家收集整理的这篇文章主要介绍了html – CSS – 使用数据属性添加颜色 – attr(数据颜色)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用我的css中的数据属性为不同的元素添加颜色但是不起作用……

我按照这个说明操作:

The attr() Function: Properties Values Collected from the Edited Document.

W3C

HTML

<table>
    <tr>
        <td>
            <span class="bgborder" data-color="#e7663f"></span>
            <i class="fa fa-copy"></i>
        </td>
        <td>
            <span>Blaablaaablaaa</span>
        </td>
    </tr>
</table>
<table>
    <tr>
        <td>
            <span class="bgborder" data-color="#77c385"></span>
            <i class="fa fa-upload fa-fw"></i>
        </td>
        <td>
            <span>Blablaablaaa</span>
        </td>
    </tr>
</table>

CSS

.bgborder {
    display: block;
    width: 5px;
    height: 100%;
    position: absolute;
    top: 0;
    background-color: attr(data-color color);
}

什么都没有出现……我是对的吗?

在我的chrome检查员中,我有这个:

background-color: attr(data-color color); 
/!\ Invalid property value

我不明白为什么…… ???

谢谢你的帮助 :)

解决方法

阅读文档总是一个好主意: https://developer.mozilla.org/en/docs/Web/CSS/attr

惊喜!如果没有什么支持它,那么它将无法工作;)

替代方案:如果您知道自己只有有限的颜色范围,请尝试:

[data-color=red] {background-color:red !important}
[data-color=blue] {background-color:blue !important}
[data-color=zophan-blue] {background-color:#33ccff !important}

如您所见,这允许灵活性,例如定义您自己的颜色;)

原文链接:https://www.f2er.com/html/232284.html

猜你在找的HTML相关文章