Setting width with CSS attr()

后端 未结 3 1508
萌比男神i
萌比男神i 2020-12-06 03:44

I\'m trying to set the width of an element using attr() in CSS but it\'s not working. Chrome says \"invalid property value\" but I\'m not sure what\'s wrong.

I\'m tr

相关标签:
3条回答
  • 2020-12-06 04:33

    This is an experimental, or at least draft, feature of CSS, and currently, according to Mozilla Developer Network's documentation, is only compatible with the CSS content property (in which it can return a string to be placed inside a pseudo-element), but cannot (yet) be used to generate values for other properties.

    References:

    • attr() (at MDN).
    • attr() (at W3C).
    0 讨论(0)
  • 2020-12-06 04:41

    Actually, there is a way to get around the attr() solution:

    I don't recommend this, but you could account for every scenario of data-width attribute, for example:

    (stylus code)

    $limit = 300
    
    .myClass
        for num in (1...$limit)
            &[data-width="${limit}"]
                width $limit
    

    Albeit, this is a terrible approach, and leads to way too much CSS. I just wanted to point out that there's always a way.

    0 讨论(0)
  • 2020-12-06 04:42

    Support for attributes other than content is Experimental. In other words, browsers do not support this yet even though it is seemingly valid.

    Here is an example showing that it does work with content, but not width.

    0 讨论(0)
提交回复
热议问题