Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jordy Vialoux 73 posts 103 karma points
    Nov 18, 2014 @ 21:59
    Jordy Vialoux
    0

    Umbraco 7: Select Body Background Image

    Hi Guys,

    I'm wanting to be able to manage the body background image used for my pages. 

    Within my "Master" Doctype I have created a tab called "Media" ( to keep it tidy ) and a generic property which is using a Media Picker. 

    I've created a custom media type caleed "Body Background Image" which the user can create as many as the like as a child of the "Folder" media type.

    Using Razor, how can I append the image to render as a CSS background image? 

    A static trick I am using at the moment is setting the "node name" on the body as a class using: 

    <body class="@Model.Content.DocumentTypeAlias.ToLower()">

     

    Ideally, I want to replace this with a content manageable solution. 

    I have tried something like this and my page bombed: 

    var bodyImage = Umbraco.TypedMedia(Model.Content.GetPropertyValue("bodyBackgroundImage"));
    <body style="background-image: url(@bodyImage.GetPropertyValue("umbracoFile"));">
  • Vasia Vasia 49 posts 241 karma points
    Nov 18, 2014 @ 22:07
    Vasia Vasia
    2

    Basically, you can add style attr to your body tag:

    <body style="background-image: url("@Umbraco.Media("image").Url");"

    Hope this helps.

    Oleg

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 18, 2014 @ 22:10
    Jan Skovgaard
    1

    Hi Jordy

    Just a note on the media type you created...if it's just an image there's no reason to make a custom media type for it? You should be able to just upload it as images in a folder you could name "Background images".

    However in order to be able to render the images as background images I think you should be able to use one of the examples provided here http://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Media-Picker and if you're using the image cropper you can see some examples here http://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors-v7/Image-Cropper

    Then it's simply just a matter of creating a macro were you get the image and wrap it in the inline style attribute and place the macro in the element.

    So in your macro the code could look something like this - Assuming you're using Strongly typed.

    @{
        if(Model.Content.HasValue("mainImage")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage")); 
            style="background: url(@mediaItem.GetPropertyValue("umbracoFile")) no-repeat"
        }   
    }
    

    Then you should be able to call the macro like this <body <umbraco:Macro Alias="YourMacroAlias" runat="server"></umbraco:Macro>> - I know it looks a bit ugly (ok, very ugly) but it should work.

    Hope this makes any sense to you.

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Nov 18, 2014 @ 22:11
    Jan Skovgaard
    1

    Too slow - And Olegs way is even simpler than my proposal :D

    /Jan

  • Jordy Vialoux 73 posts 103 karma points
    Nov 18, 2014 @ 22:11
    Jordy Vialoux
    0

    Hi Oleg,

    Thanks for this and I think this is definitely on the path of what I'm after however, it didn't work. The alias of my media type is "bodyBackgroundImage" and this is stored within a generic umbraco folder within "Media". I changed "image" to look for "bodyBackgroundImage" and unfortunately nothing is showing.

  • Jordy Vialoux 73 posts 103 karma points
    Nov 18, 2014 @ 22:13
    Jordy Vialoux
    0

    Thanks Jan - I agree re Olegs post but thank you for your help! I haven't upgraded my Umbraco to the image cropper version just yet! 

Please Sign in or register to post replies

Write your reply to:

Draft