0

We are working on an MVP in vue.js and we decided to use bootstrap to have the element styled in a consistent way.

Now we are starting to add the skin/theme to our single-page app, and we found an issue with the css rendered on the page.

We successfully managed to override the styles by using higher specificity css selectors, but we would like to optimise the output code rendered in the browser by removing the unused "base" bootstrap css code.

The question:

How can we setup our environment to make the bootstrap sass code to output clean and non-redundant css code?

Details:

  1. Bootstrap loads its own _buttons.scss file
  2. We are loading our own "theme" _buttons.scss file after bootstrap's one and we managed to have our css with higher specificity.
  3. We run the sass code compiler (on node-sass)
  4. The output css contains BOTH the bootstrap style and our own themed style for the buttons. (As an example, see the following screenshot) enter image description here

As you can see our own button style is applied as intended but we still carry over the bootstrap original style.


We would like to have OUR STYLE ONLY rendered in the browser.


Update:

The UI I'm working on uses some classes straight from bootstrap, and obviously some classes specific of our own app.

Sometimes these classes are necessary to override the bootstrap default styles.
We need to override not only the colours (which are customisable through the _variables.scss), but also some other css attributes.

We find ourselves struggling with duplicated css code rendered in the browser, where there is our own style applied and also the default bootstrap generated style which will never be applied as it's low in specificity.

I wonder if there is a way to avoid to compile sass code that doesn't need to be rendered in the browser, and at the same time avoid to "touch" the bootstrap code in ./node_modules/.

Adriano
  • 3,788
  • 5
  • 32
  • 53
  • That is typically how CSS works, as it is "cascading style sheets".and is not typically detrimental at all. However, if you look at the SCSS themeing options, you can define the colors that bootstrap compiles with, using the variable overrides, https://getbootstrap.com/docs/4.0/getting-started/theming/ Since you are specially looking to override bootstraps defaults with your own, rather than to extend the default bootstrap using cascaded rules. – Will B. Nov 05 '18 at 00:08
  • I see your point, and thanks for answering. I modified my question, as the issue is not just about the colour but is also about any other duplicated css rulesets. See under the "**Update**" section in my question. – Adriano Nov 05 '18 at 23:56
  • at that point i would start to question the use of bootstrap itself. their layouts and naming conventions are useful, but if you're going beyond just theming you might want to ditch the library itself. i don't know if their variable overrides support the level of customization you want. – worc Nov 06 '18 at 00:00
  • I agree with @worc what you describe is the cascading styling and is by design for CSS. It appears that you are attempting to make micro performance optimizations by removing bootstrap's rules for your own. If you want to be able to use bootstrap and your own theming, create separate css rules that inherit from the default. ie: replace `.btn-secondary` with your own `.btn-themed` rule. Thereby only applying your own styles without conflicting with bootstraps. Alternatively replace the individual bootstrap components you override with your own, so that the default is not compiled. – Will B. Nov 06 '18 at 00:07
  • it's not just micro-optimization, but also reducing tech debt and sheer mental overhead. otherwise you'll always have to juggle the differing priorities of selectors and which properties get applied under which circumstances. – worc Nov 06 '18 at 00:46

1 Answers1

1

Here's how you override Bootstrap (4.x) defaults.

Examine the source

First, look inside bootstrap.scss where you can see how the framework is built, component by component. You could, if you like, comment out optional components you don't need, to downsize Boostrap. But don't do that right now.

Next, look inside _variables.scss. Skim through this file and it should be clear that all customisable Bootstrap styles are defined here, including colors. Thus you can have your custom colors apply not just for buttons but throughout the whole framework. Again, you could start changing the variables you want here right now... but don't, for there is a Best Practice.

Create customisation file

Instead of editing the original source, create a new source file we'll call myproject.scss, somewhere other than the Bootstrap source folder. By keeping all changes separate, we make any future Bootstrap upgrades easy.

Add variable overrides

Now you can start copying variables you want to change. Note that variables in _variables.scss have the !default flag, which means they can be overridden elsewhere. For example if you want a different secondary color, you'll find it defined as $secondary, and so add it to myproject.scss with a new value:

$secondary: #dd5679;

Add as many variable overrides as you want.

Import Bootstrap

After that, import Bootstrap into the file. EITHER take bootstrap.scss wholesale:

@import "relative/path/to/bootstrap/bootstrap";

OR copy-paste the contents of bootstrap.scss, update the pathnames, and comment out the components you don't want:

@import "relative/path/to/bootstrap/functions";
@import "relative/path/to/bootstrap/variables";
@import "relative/path/to/bootstrap/mixins";
...
// @import "relative/path/to/bootstrap/popover";
// @import "relative/path/to/bootstrap/carousel";
@import "relative/path/to/bootstrap/utilities";
@import "relative/path/to/bootstrap/print";

The first 3 imports, "functions", "variables" and "mixins" are core and not optional components; don't exclude them.

Add project styles

After that, add your own styles. If you have a significant amount, organise them into their own partial files e.g. _mybuttons.scss (start names of partial files with an underscore), and import them.

@import "mybuttons";

Your custom Bootstrap source file is now ready.

Compile to CSS

The resulting myproject.css file is what you want to load instead of the original Bootstrap CSS file.

Community
  • 1
  • 1
LionelW
  • 376
  • 3
  • 11
  • Thanks, @LionelW. So far this is the best answer I was able to get regarding my case. I guess I'll need to duplicate the bootstrap sass code so I can override it, and eventually merge it manually for every new version of bootstrap if need be. I really hope there was a way to maintain both (bootstrap base styles, and my own style) in a systematic way rather than manual. But, hey... that's life! :D – Adriano Nov 08 '18 at 00:14
  • 1
    Well the maxim "if it ain't broke why fix it" applies here: Making changes to static files like CSS should be very rare occasions (a best practice), and unless there are noticeable bugs, there is usually no reason to upgrade Bootstrap once set up. I'm not sure what you mean by a systematic way, but this "manual" task, that offers you so much control, is ideally only a one time thing. – LionelW Nov 08 '18 at 00:33