Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS3: set background image to rel attribute value

Tags:

css

I'm looking to set the background-image (or even render an image via the pseudo elements :after or :before) to the value, which will be a URL, of a rel attribute, but only in certain cases (this is a cloud file listing). For example:

HTML:

<div class="icon ${fileExtension}" rel="${fileURL}"></div>

It would be great if I could do something like this:

CSS:

.icon.png,
.icon.jpg,
.icon.jpeg,
.icon.bmp,
.icon.gif { background-image: attr(rel,url);  }

... but obviously that doesn't work as, if I'm not mistaken, the attr() CSS function only works inside pseudo element blocks.

I know there are ways of doing this using conditional JSP or even jQuery logic, but I'd like to figure out a neat way of doing it via CSS3, since I'm only concerned with modern browsers at the moment anyway.

Also, I don't want to explicitly set the background image to the URL or create an <img> element, because by default if the file is not a supported image, I'd rather display a predetermined set of icons.

like image 398
Muers Avatar asked Jul 15 '11 22:07

Muers


People also ask

Which attributes used to set the background image?

HTML | <body> background Attribute. The HTML <body> background Attribute is used to specify the background-image for the document.

Which CSS property is used to set the background image of an element?

The background-image CSS property sets one or more background images on an element.

Which css3 property is used to resize a background image?

The background-size CSS property lets you resize the background image of an element, overriding the default behavior of tiling the image at its full size by specifying the width and/or height of the image.

What is background image property?

The background-image property specifies an image to use as the background of an element. By default, the image is repeated so it covers the entire element.


2 Answers

Using

.icon:after{ content: ""attr(rel)""; }

displays the rel value as text.

A jQuery solution is to add the background-image (taken from the rel value) as inline CSS:

jQuery(function($) {
    $('.icon').each(function() {
        var $this = $(this);
        $this.css('background-image', 'url(' + $this.attr('rel') + ')');
    });
});
like image 164
ryanve Avatar answered Oct 10 '22 15:10

ryanve


I've tried to do something using jQuery but i don't exactly understand what you want so i can't go on with my code. So far i've done only this.

EDITED I hope it's exactly what you need

$(function(){
    var terms = new Array('png','jpg','jpeg','bmp','gif');

    $('.icon').each(function(){
        var t = $(this),
            rel = t.attr('rel'),
            cls = t.attr('class');

        cls = cls.split(' ');

        for (var i=0; i < terms.length; i++) {
            if (terms[i] == cls[1]) {
                t.css('background-image','url('+rel+')');
            }
        }
    });
});

if you can give me a better example, to undestand exactly what you want, i hope somebody from here will be able to solve your problem.

Regards, Stefan

like image 21
stefanz Avatar answered Oct 10 '22 14:10

stefanz