Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Portal - CSS changing the header per page

(0) ShareShare
ReportReport
Posted on by 89

Hello.  On my home page template, I have the following .css applied.

.section-landing {
    background: url("wave-red1.jpg") no-repeat center!important;
    background-size: cover!important; }
This is the only way I've gotten an image that doesn't repeat, and I'm happy to just keep using this home page template.  However, I would like to change the picture (so a different url) for each page - is there a way to do the css to apply to particular pages? 
Thank you.
  • unicorn_tech Profile Picture
    89 on at
    RE: Portal - CSS changing the header per page

    Thank you Matt Bayes .  I'm not really familiar with Javascript, so I'm not sure I could pull off the above. I appreciate the idea of looking at the Header Web template, and I had some success by adding a picture to the header web template - and thankfully, this is also behaving in a responsive fashion, which I wasn't able to do with the CSS code alone.  I will keep this though, and as I learn more about liquid and JS, hopefully I can use this as well.

  • Verified answer
    - Matt Bayes - Profile Picture
    890 on at
    RE: Portal - CSS changing the header per page

    Hey Unicorn Tech!

    To my knowledge, there is no way to do the conditional CSS just using CSS alone. If you wanted to set a specific image based on the webpage accessed you would have to use Javascript/JQuery.

    If you were to edit the Header web template, you should be able to add a function near the bottom of the file that will read the current path (using the location object), and based on the page URL you will have to override the styling for the .section-landing to set the background appropriately.

    There are a few ways you could accomplish this but I think utilizing the Header web template is the easiest and adding the JS function to the file should be straight-forward.
    If you were partial to using Liquid code you could do a hybrid approach and use Liquid to retrieve the page URL (instead of using JS to strip the URL), save that page URL to a hidden HTML element on the header, and then use JS to pull that value and assign the image.

    For example:

    {% assign page_url = page.url %}
    
    
    
    function setHeaderImage() {
        let url = $("#pageUrl").text();
        let imageUrl = "";
        
        switch (url) {
            case "/page-1":
                imageUrl = "page-1-image.jpg";
                break;
                
            case "/page-2":
                imageUrl = "page-2-image.jpg";
                break;
        }
        
        $(".section-landing").css("background", "url("   imageUrl   ") no-repeat center !important");
    };

    The alternative (which may be cleaner) would be to utilize classes for your styling and handle it all in your stylesheet. You would still need to write JS code to add the class to the element, however, you could add "page-1-class", "page-2-class", etc. to the ".section-landing" element and then handle the image setting in those classes in your stylesheet.

    Either way will work, it's really up to you!

    Thanks!

    Matt Bayes

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans