May I ask why you chose to use the Media section to hold your image and inline CSS to display the background image?
My recommendation is to store the image "bg1.jpg" in the "images" folder on your server resulting in fewer calls to the database to display the image. Then, I would move the style attribute to a CSS file and control the styles of that div from there keeping your styles in a central place. It is part of the best practices in Umbraco to go this route. All of this is on the assumption you are not going to be changing the background image often if ever.
Normally i couldn't agree more with you. But my client has request this.
I already had to overcome a lot of trouble with uploading images and cropping the just for this.
Untill now i have only found 1 option where you have to include <div id="pagetotal".... into the xslt part of the umbraco item. Not very nice but it should work.
But since i'm fairly new to xslt, i haven't succeeded yet.
Looks like your div is picking up the runat=server part of the umbraco:item. Maybe you can play around a bit with spacing (replace = by a space) or by using single quotes on either the umbraco-item or the style attribute.
Otherwise you could always create an XSLT file which builds the div and insert it using a macro.
If that doesn't work, I would go with the route Yannick suggested and create a new XSLT file to put the div together and attach it to a macro. Then, you can try enabling caching on the macro to prevent it from calling the database on every page load.
Nice. I needed the same sort of solution. I'm updating my own version, as the XSL syntax posted by @Paul Blair is not compatible with the 4.7 Umbraco I'm running.
create a div with a backgroud-image style with inline xslt
Hi,
XSLT, learning and learning.
Have the following issue
I'm trying to create something like this:
As u can see this is an image from my media. (The field BackImage is a media picker value)
So i created something like this:
This line doesn't even show in the preview..
Any help is welcome
After some experimenting this is what i created now:
And the result is: <div id="pagetotal" style="background-image:url(=/media/1232/bg1.jpg)">
almost correct except for the '='
So after removing de '=' i get something like this as result:
<div id="ctl00_ctl00_ctl00_ContentPlaceHolderDefault_pagetotal" style=""background-image:url(<umbraco:Item" field="BackImage" xslt="umbraco.library:GetMedia({0},'true')/data[@alias='umbracoFile']"></div>)">
HELP.....
Maarten,
May I ask why you chose to use the Media section to hold your image and inline CSS to display the background image?
My recommendation is to store the image "bg1.jpg" in the "images" folder on your server resulting in fewer calls to the database to display the image. Then, I would move the style attribute to a CSS file and control the styles of that div from there keeping your styles in a central place. It is part of the best practices in Umbraco to go this route. All of this is on the assumption you are not going to be changing the background image often if ever.
Hi Donald,
Normally i couldn't agree more with you. But my client has request this.
I already had to overcome a lot of trouble with uploading images and cropping the just for this.
Untill now i have only found 1 option where you have to include <div id="pagetotal".... into the xslt part of the umbraco item. Not very nice but it should work.
But since i'm fairly new to xslt, i haven't succeeded yet.
Looks like your div is picking up the runat=server part of the umbraco:item. Maybe you can play around a bit with spacing (replace = by a space) or by using single quotes on either the umbraco-item or the style attribute.
Otherwise you could always create an XSLT file which builds the div and insert it using a macro.
Maybe try replacing the double quotes with single quotes in the umbraco:item part. For example:
If that doesn't work, I would go with the route Yannick suggested and create a new XSLT file to put the div together and attach it to a macro. Then, you can try enabling caching on the macro to prevent it from calling the database on every page load.
--
Donald
Hi,
I have just implemented the same thing for a customer. I put my code into a macro though. Here is the XSLT:
Nice. I needed the same sort of solution. I'm updating my own version, as the XSL syntax posted by @Paul Blair is not compatible with the 4.7 Umbraco I'm running.
is working on a reply...