RE: To Include or Not to Include CSS in Display Templates

Amos Fong, modified 6 Years ago. Junior Member Posts: 39 Join Date: 8/15/17 Recent Posts
What's the best practice for using custom CSS in Display templates?  Is it a good idea?  Bad idea?

There are certain instances where I can't see how this is avoidable (setting the background of a div to the image in the structure), but what is the best way?

I like the idea that the display template can basically be self-contained, with it's own CSS, but then does that cause issues elsewhere?

​​​​​​​Thanks!
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
John CressmanWhat's the best practice for using custom CSS in Display templates?  Is it a good idea?  Bad idea?

There are certain instances where I can't see how this is avoidable (setting the background of a div to the image in the structure), but what is the best way?

I like the idea that the display template can basically be self-contained, with it's own CSS, but then does that cause issues elsewhere?

I tend to go with my default answer: "It depends (TM)"

If it's positional (e.g. columns or embedding an image in text, floating) I lean towards embedded CSS. If it's decorating with a specific color (that might be changed with the next CI/theme update, I lean towards not doing so. If the image that you mention is part of the content (e.g. an uploaded image): leaning towards yes. If it's a design element (e.g. pretty border): No.

To summarize: It depends. ;)

After all, you'll have to evaluate if the extra price for "doing it properly" is worth it - e.g. do you want to redeploy your theme? Do you even have control over it? Is this a one-off situation or a general strategy? I tend to solve "one-offs" on the spot, knowing that they can easily creep up to "one-more-offs" and "only-one-more-off-please". When I realize that there's now plenty of them, then it's time to remodel and generalize. But I tend to not build a pretty and future-proof solution that is so unusual that nobody will ever be aware of either situation (neither the shortcut, nor the brilliantly architected version)