Customizing Bootstrap, Sort of

 



Love it or hate it, Bootstrap is a mainstream CSS framework that is very popular and comes packaged with Blazor Templates.  I have gone on record that I prefer Tailwinds CSS https://codingwithdavid.blogspot.com/2022/08/jumped-feet-first-with-blazor-and.html over Bootstrap, but Bootstrap is so in use that you need to know it and use it at times.

I did post about how to update to Bootstrap 5.3   and how to add a Bootstrap dark mode switcher to your applications.

One of the biggest complaints about Bootstrap is that all the web applications look the same.  There is some truth to that, but you can add themes, or use SAAS and fully customize Bootstrap.  But these solutions come with their own issues, like upgrading to new releases of Bootstrap.

In addition, if you are building a suite of applications, say for a line of business, you have to ensure the same style and user experience is used in all the applications.  This just compounds the issues of a fully customized Bootstrap solution.

Solution

I have found a more workable solution that should be more flexible and more maintainable.  Once you think about it, this solution does make a lot of sense.  You just need to take advantage of the Cascading part of CSS.  You create a “branding CSS” that overrides the parts of the Bootstrap framework that you want to change.

By adding your own brand CSS file you can make your application look any way you want while using Bootstrap like you have been.

 

Adding your brand CSS style sheet in App.razor

        <link rel="stylesheet" href="bootstrap/bootstrap.min.css" />

        <link rel="stylesheet" href="myBrand.css" />

        <link rel="stylesheet" href="app.css" />


This works well for elements and classes. 

        h1 {

            font-size: 10rem

        }

 

        .btn-primary {

            color: white;

            background-color: #f066d9;

            border-block-color: #000;

        }


That allows your brand styles to be used instead of the Bootstrap defaults.  You can change fonts, colors, or anything that is a class or element.


Use Between Projects


You can add the myBrand.Css file to source control.  You can make the branded CSS part of your project templates as well.  Then for each new project, it is already there.


Default Bootstrap

We all have seen this image




With myBrand.CSS






Notice the size of the H tags are bigger and the color of the buttons has been changed.  Since I did not change the h1- h6 class, those are the same size in both images.

Summary

While this is a simple solution, it works for me.  I like finding these tips, it makes me much more efficient at building applications.


[source]

Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor - Cool Blur Loading Page