2

Background images have some enviable CSS properties that I'd like to use for photo galleries -- namely, background-size: cover; and background-position: center center; let you use an image of any aspect ratio and fit it attractively to a fixed-size container.

However, I don't really think setting background images with content is ideal. I'd much rather have <a href="url.jpg"><img src="url.jpg"/></a> in content than <a href="url.jpg" style="background-image: url(url.jpg);"><img src="url.jpg"/></a>. I think a script to take the former and write the latter is ideal.

Given code that looks like this:

<a href="http://thesundaybe.st/media/from-earth-phone-23.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-24.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-26.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-27.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-28.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-29.jpg"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-30.jpg"></a>

I'm trying to write a JQuery script to take the href URL and set it as the background image. Basically, turning the above code into this:

<a href="http://thesundaybe.st/media/from-earth-phone-23.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-23.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-24.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-24.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-26.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-26.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-27.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-27.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-28.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-28.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-29.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-29.jpg);"></a>
<a href="http://thesundaybe.st/media/from-earth-phone-30.jpg" style="background-image: url(http://thesundaybe.st/media/from-earth-phone-30.jpg);"></a>

Here's a jsfiddle with the basic HTML/CSS set up, just needing a bit of JQuery: http://jsfiddle.net/MRSallee/tv3q8/3/

And theoretically, this is what it'd look like when the script does the trick: http://jsfiddle.net/MRSallee/tv3q8/

I got fairly close this this bit of JQuery, but it seems to just use the first href as the background image for all of the <a> tags.

$('a').css({'background-image': 'url(' + $('a').attr('href') + ')'});

Any help is greatly appreciated. Cheers.

3 Answers 3

5

You can try this:

$('a').css('background-image', function() {
    return 'url(' + this.href + ')';
});

each loop will be used internally so it's equivalent to $('a').each + $(this).css combination.

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@PSCoder Yes, a lot of jQuery methods accept function + we can skip redundant each very often. Makes code a little bit cleaner.
1
$('a').each(function(){
   $(this).css({'background-image': 'url(' + $(this).attr('href') + ')'});
});

http://jsfiddle.net/mohammadAdil/tv3q8/8/

2 Comments

This also seems to yield the same problem I've been running into. The first href URL gets set as the background image for all of the <a> tags, rather than the unique href included in each <a> tag.
see change in this line $(this).css({'background-image': 'url(' + $(this).attr('href') + ')'});
1
$('a').each(function(){    
 $(this).css({'background-image': 'url(' + this.href + ')'});
});

Here this refers to the current element in iteration.

http://jsfiddle.net/x5kbA/

1 Comment

This is pretty similar to what I was trying earlier and seems to yield the same problem -- the first href URL gets set as the background image for all <a> tags. See: jsfiddle.net/MRSallee/tv3q8/9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.