Copied to clipboard

Flag this post as spam?

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


  • Hannah Anais 10 posts 50 karma points
    Feb 16, 2015 @ 21:16
    Hannah Anais
    0

    Importing CSS to Templates

    I am new to Umbraco and I have a question somebody could help me with.

    I have created a small site that has a homepage. This homepage uses the Master Layout. Im my Master Layout Template I have linked to my styles.css. This all works fine.

    I then created a new Template called Services which also uses the Master Layout. However, this page would use a file called services.css. I don not wish to place a link in the Master Layout to this file. Instead only call this CSS file just for this template/web page. How would I do this?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 16, 2015 @ 21:45
    Dennis Aaen
    1

    Hi Hannah and welcome to our.

    Is there a specific reason why you need to create 2 different CSS files. You could just have one CSS file and then just add the CSS seletors in there for the service page. If it because the services template should have a different layout and skinning then I would not inhert from the master.

    Since you are new to Umbraco in think you would get a lot, by watching these videos http://umbraco.tv/videos/umbraco-v7/implementor/, on Umbraco TV. These videos are showing the basic concepts of Umbraco, some of the video are free to watch and, some needs that you subscribe to the Umbraco TV, you can see the plans here: http://umbraco.tv/plans-signup/

    Here are specific chapters about CSS http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/stylesheets-and-javascript/using-stylesheets/ and templates and mater template. http://umbraco.tv/videos/umbraco-v7/implementor/fundamentals/templating/introduction/. Other good links to learn Umbraco is https://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/ and this article written by Blake Smith http://24days.in/umbraco/2014/how-to-set-up-an-umbraco-site/

    Hope this helps, if you have further questions to this keep asking, we are friendly people try to help each other.

    /Dennis

  • Martin 114 posts 313 karma points
    Feb 16, 2015 @ 21:50
    Martin
    0

    Hi,

    I would say that it normal to include all the css files you need within the head section, and I guess you have your head section in your Master Layout, so just go ahead and add all css files you need. For example <link rel="stylesheet" href="/css/MainLayout.css" />

    You should also look into @RenderBody() and @RenderSection which are very useful. Watch the videos at Umbraco.tv

    Good luck,

  • Hannah Anais 10 posts 50 karma points
    Feb 16, 2015 @ 22:02
    Hannah Anais
    0

    OK, That's great. I was planning on using some social share buttons, a slider and a few other things that are not required on the homepage. As the CSS for these things is fairly bloated, I was after a way that I didn't call these stylesheets on the homepage as well, as my styles.css that is used site wide is getting rather long.

    The same concept may be needed for scripts you see. No point calling a script in the head of the Master Layout if it's not being used on teh page. This will throw an error. Unless I use a JQuery IF statement to check if an element exists I suppose.

  • Martin 114 posts 313 karma points
    Feb 16, 2015 @ 22:14
    Martin
    0

    As long as you have all the style sheets you need declared within the head section, you just use the name of the class and id styles where you what to apply them. You don't need to call specific sheet once they are declared. Just remember that if you use the same class name in different style sheet, the order you declare the sheets will matter.

  • Hannah Anais 10 posts 50 karma points
    Feb 16, 2015 @ 22:34
    Hannah Anais
    0

    I understand. I may have <section> with 10% padding on the homepage and 30% padding on another page. I would need to target a containing element higher up. Some people add classes to the body tag. body.homepage body.about-page. How is this done in Umbraco?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 16, 2015 @ 22:44
    Dennis Aaen
    101

    Hi Hannah,

    If you pages has different document types, then you could add this @Umbraco.Field("nodeTypeAlias") on a class to the body tag, This will gives you the alias of the document type. 

    @Umbraco.Field("nodeTypeAlias")

    Hope this helps,

    /Dennis

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 16, 2015 @ 22:47
    Jan Skovgaard
    0

    Hi Hannah and welcome to our :)

    How you choose to add classes and render your HTML structure is entirely up to you and not as such Umbraco specific. You are in control of the final rendered webpage.

    I have just skimmed the former posts but all you need to do is to decide how you want to do it.

    You could consider using another class on the

    if it makes sense to do so. Otherwise the approach of having a specific page-class on the <body> or the <html> is a good option so you're able to overwrite the home-page specific styling.

    /Jan

  • Hannah Anais 10 posts 50 karma points
    Feb 16, 2015 @ 23:04
    Hannah Anais
    0

    Thanks. The below works really well in case I have two different rules for the same CSS selector and could use body.homepage (Umbraco Alias) body.services to separate these CSS rules so they don't cause styling wars :)

    @Umbraco.Field("nodeTypeAlias")
  • Matthew Kirschner 323 posts 611 karma points
    Feb 16, 2015 @ 23:21
    Matthew Kirschner
    2

    Hi, Hannah.

    It is possible to include a css or js file only in the templates that need them. So you have styles.css in your master template and you want services.css for the services template? This is easy to do with the ClientDependency framework.

    In both your master and services templates, add a reference to ClientDependency:

    @using ClientDependency.Core.Mvc
    

    In the head of your master template add:

    @Html.RenderCssHere()
    @Html.RenderJsHere()
    

    Finally, in your services template, add:

    @{
      Layout = "MasterLayout";
    
      Html.RequiresCss("~/css/services.css");
      Html.RequiresJs("~/scripts/services.js");
    }
    

    This assumes that your project already includes the ClientDependancy framework.

    Of course, if you wanted your services template to simply inherit the structure of your master layout and not the styling, then you'll have to inherit from another template.

Please Sign in or register to post replies

Write your reply to:

Draft