Copied to clipboard

Flag this post as spam?

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


  • Owen 13 posts 105 karma points
    Feb 06, 2024 @ 11:50
    Owen
    0

    Applying CSS to a document type

    I'd like to apply a custom CSS style to a specific document type which will override the style set in the master CSS file, e.g. on all news pages I'd like the H2 to be 3rem.

    Is this possible and, if so, can anyone advise how I can implement this?

  • Danine Noble 82 posts 368 karma points
    Feb 06, 2024 @ 15:38
    Danine Noble
    0

    Hullo Owen ^^

    You would apply CSS the way you normally do. Create a scoped set of styles that targets a class (or classes) that exist in the view template that the document type uses. Then you just reference it.

    Depending on re-usability of the style group and/or how you prefer to organize things you have a few options for doing so.

    It could be included in the Layout with a conditional statement checking for Model.Content.ContentType.Alias that matches the docType; or if its more than 1 docType perhaps it's a use case for Named Sections (if needed in partial view there's an interesting Code Share alternative setup); or just included in the master css as long as your styles are scoped properly; for a few examples.

  • Owen 13 posts 105 karma points
    Feb 07, 2024 @ 15:51
    Owen
    0

    Thanks Danine. I want to change the H2 font size but only on a news item document type. Can the CSS be applied to only target that document type?

  • Danine Noble 82 posts 368 karma points
    Feb 07, 2024 @ 22:27
    Danine Noble
    0

    Yes -- using normal CSS + HTML markup; don't need anything Umbraco related :3

    Create a CSS rule in your styles that is scoped to a unique class that exists on a top-level/encompassing element in the View Template only used by your News document type.

    Example:

    news.cshml

    <article class="news-content">
      @Model.Content.NewsBodyContentField
    </article>
    

    css

    // assuming there's no excess specificity that needs to be overridden
    .news-content h2 { font-size: 3rem; } 
    

    If you're looking for some Backoffice control that hooks style sheets up to specific templates that does not exist. It's just the normal -link rel=stylesheet href- tag markup you write in the view templates that dictate what style sheets are available.

  • Owen 13 posts 105 karma points
    Feb 16, 2024 @ 16:42
    Owen
    0

    Thank you for your help. So my news page is NewsItemPage.cshtml. What code would I add and where would I add it before adding the CSS?

  • Danine Noble 82 posts 368 karma points
    Feb 21, 2024 @ 19:53
    Danine Noble
    0

    Sorry for the delay! The sample snippets I provided above is all you need.

    1. Step 1 - Make a class that has the styles you want to apply (or use an existing one that already serves this purpose)
    2. Step 2 - Apply that class to the appropriate container in your markup.

    I can't provide any more detailed assistance without knowing more about how your markup is structured and if there would be any conflicting rules with existing styles.

    There is nothing specific to Umbraco or tricky ^^ It's all basic/general CSS.

Please Sign in or register to post replies

Write your reply to:

Draft