Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css Content, Attr and Url in the same sentence

I have used the content attribute for a long time, and today I wanted to try something new. Instead of using JS to display a image tooltip I wanted to know if it was possible to do it dynamically with CSS.

So I tried:

.TableLine:hover:after{
  content: url("../Img/Photo/"attr(id)".jpg"); 
}

where attr(id) is supposed to return the ID of the picture (alphanumeric) which is also the name of the picture.

It doesn't work at all, it has no effect. I think that the block did not parse because adding a border or background to the block also seems to have no effect.

When I just use the attr(id) alone, without the url thing, it works perfectly. It also works when I replace attr(id) with the real name of the picture.

After searching a while on the web I haven't found anything relevant so here I am. Is that a known bug or just my mistake? :)

like image 525
h1fra Avatar asked Feb 11 '12 21:02

h1fra


People also ask

Can I use content ATTR?

While attr() is supported for effectively all browsers for the content property, CSS Values and Units Level 5 adds the ability to use attr() on any CSS property, and to use it for non-string values (e.g. numbers, colors).

What is attr () in CSS?

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.

How do you give content in CSS?

It can be displayed using either the counter() or counters() function. The counter() function has two forms: 'counter(name)' or 'counter(name, style)'. The generated text is the value of the innermost counter of the given name in scope at the given pseudo-element.

What does content mean in CSS?

The content property in CSS defines the content of an element. You may have heard that this property only applies to the ::before and ::after pseudo-elements. In this article, we'll explore various use cases for the content property, including outside of pseudo-elements.


2 Answers

It's neither a bug nor a mistake. The currently supported syntax (CSS2.1) for content is:

content: normal | none | 
         [ <string> | <uri> | <counter> | attr() |
           open-quote | close-quote | no-open-quote | no-close-quote ]+ | inherit

I.e.:

  • The literal normal, none or inherit

  • Or any number of these in succession:

    • a string - "hello"
    • a (constant) URI - url("image.jpg")
    • a counter - counter(section)
    • an attribute - attr(id)
    • open-quote, close-quote, no-open-quote, no-close-quote

The specs don't allow for them to be "nested", they can only follow each other, e.g.:

content: "Photo: " url("../Img/Photo.jpg") attr(id);
/* Which is not what you want */

The current CSS3 drafts don't allow for it either. Possibly - if it's been discussed - because most use cases would have little to do with presentation and more to do with actual content.

like image 166
JimmiTh Avatar answered Oct 14 '22 06:10

JimmiTh


As of W3C Candidate Recommendation 28 August 2012, there is syntax for specifying the type returned by attr().

It is described here.

Long story short, the following would work:

content: attr(id url);

But it still doesn't seem like you would be able to concatenate that with other strings, which is annoying.

Anyhow, this still doesn't seem to be implemented anywhere.

Check Browser compatibility

like image 39
Joe Thomas Avatar answered Oct 14 '22 07:10

Joe Thomas