html - CSS - 使用数据属性添加颜色 - attr(data-color color)

标签 html css less twig

我试图在我的 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

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

感谢您的帮助:)

最佳答案

您可以从 html 传递 css 值:

<button style="
    --tooltip-string: 'Ug. Tooltips.';
    --tooltip-color: #f06d06;
    --tooltip-font-size: 11px;
    --tooltip-top: -10px">
  Button
</button>

到 CSS:

button::after {
  content: var(--tooltip-string);
  color: var(--tooltip-color);
  font-size: var(--tooltip-font-size);
}

来源:https://css-tricks.com/css-attr-function-got-nothin-custom-properties/ 代码笔:https://codepen.io/chriscoyier/pen/EbxVME

关于html - CSS - 使用数据属性添加颜色 - attr(data-color color),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529374/

相关文章:

html - CSS:在图像下定位 figcaption

javascript - 为 HTML5 循环网格?

css - Futura Condensed Extra Bold 的字体系列不起作用

html - 宽度为 100% 的渐变覆盖并绑定(bind)到宽度为 50% 且最大高度为 90% 的图像

angular - 使用 angular2 cli 编译 less 文件

css - 在 IE8 中是否有用于 css 转换属性的 polyfill

html - 将外部 css 的范围限制为仅特定元素?

javascript - 私有(private)路由 React 路由器总是重定向到第一个路由

javascript - HTML 刷新时的随机图像

css - 定制Disqus