web
You’re offline. This is a read only version of the page.
close
Skip to main content
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 91

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.
I have the same question (0)
  • 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

  • unicorn_tech Profile Picture
    91 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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 247

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 166 Super User 2025 Season 2

#3
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 164

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans