CSS - Add Color with a data attribute - attr(data-color color)

css attr
css attr background-image
css attr parent
data attribute css
data-color attribute html
data-color bootstrap
css set element attribute
css mdn attr

I'm trying to add color to different element with a data attribute in my css but doensn't work ...

I follow this instructions :

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);
}

Nothing appears...Am I right ?

In my chrome inspector I have this :

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

I don't understand why... ???

Thanks for your help :)


Always a good idea to read the documentation: https://developer.mozilla.org/en/docs/Web/CSS/attr

Surprise! If nothing supports it, then it won't work ;)

Alternative: If you know you only have a limited range of colours, try:

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

As you can see, this allows flexibility, such as defining your own colours ;)

html - CSS - Add Color with a data attribute, Always a good idea to read the documentation: https://developer.mozilla.org/en/​docs/Web/CSS/attr. screenshot of support table. Surprise! If nothing supports it,  I'm trying to add color to different element with a data attribute in my css but doensn't work I follow this instructions : The attr() Function: Properties Values Collected from the Edited Document.


If you are talking only about colors, you can use currentColor value as a proxy.

For example:

HTML

<td>
   <span class="bgborder" style="color: #e7663f"></span>
   <i class="fa fa-copy"></i>
</td>

CSS

.bgborder {
  background-color: currentColor;
}

The CSS attr() function got nothin' on custom properties, Always a good idea to read the documentation: https://developer.mozilla.org/en/​docs/Web/CSS/attr. screenshot of support table. Surprise! If nothing supports it,  Question: Tag: html,css,less,twig I'm trying to add color to different element with a data attribute in my css but doensn't work I follow this instructions : The attr() Function: Properties Values Collected from the Edited Document.


You can pass css values from html:

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

to css:

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

source: https://css-tricks.com/css-attr-function-got-nothin-custom-properties/ codepen: https://codepen.io/chriscoyier/pen/EbxVME

[attribute], But you can't put HTML in the attribute value, so those tooltips are limited to a string value, and <h2 data-color="#f06d06"> Custom Colored Header </h2>. As you can see, we used the content css property, and the attr() value with the name of our data attribute inside it. This will add the content of data-link=”” before the element. So now, it should look something like this: See the Pen GpqYXv by Andor Nagy (@andornagy) on CodePen.


Currently, the CSS attr function can only be used with the content property in browsers

See here for compatibility

Per the CSS2 spec:

Limited to the content property

CSS3 will extend this (proposal)

..can be used on all properties; may return other values than <string>

attr(), We can use any of an element's attributes as selectors. Attribute selection has a special syntax. Here's an example: a[href="https://css-tricks.com"] { color: #​E18728; } it */ } [data-value~="foo"] { /* Attribute has this value in a space-​separated To use this selector, add an asterisk (*) before the equals sign. The CSS attr() function got nothin’ on custom properties. Normally, the connection between CSS and HTML is that CSS selectors match HTML elements, and the CSS styles them. CSS doesn't know about the actual content in the HTML. But there is a way CSS can get its hands on data in HTML, so long as that data is within an attribute on that HTML element.


I guess that you are looking is CSS variables and you can use at Chrome, Firefox and Safari. Check the browser support at Can I use

And can see more at the video from Lea Verou

HTML Global data-* Attributes, Simple usage */ attr(data-count); attr(title); /* With type */ attr(src url); color, <​color>, The attribute value is parsed as a hash (3- or 6-value hash) or a If not set, CSS will use the default value defined for each <type-or-unit> . The attr() CSS function is used to retrieve the value of an attribute of the selected element and use it in the stylesheet. It can also be used on pseudo-elements, in which case the value of the attribute on the pseudo-element's originating element is returned.


CSS [attribute|=value] Selector, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The attr () function returns the value of an attribute of the selected elements. Browser Support. The numbers in the table specify the first browser version that fully supports the function. attr ( attribute-name) Required. The name of the attribute of the HTML element (s) CSS Functions Reference. If you want to report an error, or if you


How You Can Use HTML5 Custom Data Attributes and Why, Select and style elements, where the lang attribute's value starts with "en": [lang|=​en] { background-color: yellow; }. Try it Yourself ». More "Try it Yourself"  In CaioToOn's fiddle the CSS code can use the data properties for setting the content. Unfortunately it is not working for the width and height (tested in Google Chrome 35, Mozilla Firefox 30 & Internet Explorer 11). But there is a CSS3 attr () Polyfill from Fabrice Weinberg which provides support for data-width and data-height.


CSS - Add Color with a data attribute - attr(data-color color), Gajendar Singh walks you through HTML5 custom data attributes, why they're useful, how to style them with CSS and Its main purpose is to allow the developer to assign style information to a set of elements. color: white; Learn how to change the color of the placeholder attribute with CSS. In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector. Note that Firefox adds a lower opacity to the placeholder, so we use opacity: 1 to fix this. Tip: Read more about the ::placeholder selector in