Hi all!
It has been a while that I have written something. So today I want to present to you an approach that I used to parameterize bootstrap styles CSS that we were using as a web resource embedded in a form in Dynamics.
So the problem I addressed was that since the "Themes" in Dynamics CRM are easily updated from the Dynamics GUI itself, all other web resources that were aligned with the existing color scheme of Dynamics now looked out of place as their CSS would not automatically align to the changes in the Themes from the GUI. So we needed to find a solution that would allow us to easily update the CSS colors when such updates were being made. Since we were using the Bootstrap CSS library to style our web resources, it would be a lot of effort and prone to error if we tried to manually update the CSS (by doing a find and replace) whenever such a change was made.
So here we used LESS to use variables straight into the CSS!
The below snapshots present the starting few lines of the Bootstrap LESS code where we declared the variables and the darken function to be used for the colors throughout the generated CSS
The next snapshot shows how the variables are used in the Bootstrap LESS code itself
The best place to learn the basics of LESS (this article only considers a very trivial use case) is here.
Once the Bootstrap LESS file is ready we can use any LESS "compiler". I have used the LESSC NPM package to convert the LESS code to CSS, which I then updated to Dynamics as a web resource. Details on usage and how to install the package can be found here.It has been a while that I have written something. So today I want to present to you an approach that I used to parameterize bootstrap styles CSS that we were using as a web resource embedded in a form in Dynamics.
So the problem I addressed was that since the "Themes" in Dynamics CRM are easily updated from the Dynamics GUI itself, all other web resources that were aligned with the existing color scheme of Dynamics now looked out of place as their CSS would not automatically align to the changes in the Themes from the GUI. So we needed to find a solution that would allow us to easily update the CSS colors when such updates were being made. Since we were using the Bootstrap CSS library to style our web resources, it would be a lot of effort and prone to error if we tried to manually update the CSS (by doing a find and replace) whenever such a change was made.
So here we used LESS to use variables straight into the CSS!
The below snapshots present the starting few lines of the Bootstrap LESS code where we declared the variables and the darken function to be used for the colors throughout the generated CSS
The next snapshot shows how the variables are used in the Bootstrap LESS code itself
The best place to learn the basics of LESS (this article only considers a very trivial use case) is here.
The magic command (from the above link) is (assuming LESSC is installed globally):
lessc Bootstrap.less Bootstrap.css
which compiles the Bootstrap LESS file with all the variables to the Bootstrap CSS file with the variables replaced by the values or results of the functions as specified. The generated CSS can now be replaced in the web resources of Dynamics, possibly after minification with some tool. We can also use grunt to automate the tasks of compiling the LESS to CSS and then minifying the CSS, all using a single command!
Hope this helps and makes your life easier customizing the look and feel of Dynamics CRM
Cheers!

