Tips for maximizing bootstrap efficiency by eliminating unnecessary CSS code during rendering

Currently, we are developing an MVP using vue.js and have chosen to incorporate bootstrap for consistent styling of the elements on our single-page app.

As we begin to implement the skin/theme into our application, we have encountered an issue with unnecessary css being rendered on the page. Although we have successfully overridden these styles with higher specificity css selectors, we aim to optimize the code output by removing any unused "base" bootstrap css.

The Question:

How can we configure our environment to eliminate redundant css code from the bootstrap sass files?

Additional Details:

  1. Bootstrap loads its own _buttons.scss file upon initialization.
  2. We have added our custom "theme" _buttons.scss file after the bootstrap file, ensuring our styles take precedence.
  3. We compile the sass code using node-sass compiler.
  4. The resulting css includes both the original bootstrap styles as well as our themed styles for buttons.

In the screenshot provided, you can see that our button style is properly applied, but the default bootstrap style remains present in the output.


Our goal is to only display OUR STYLES in the browser.


Update:

The user interface we are creating includes classes directly from bootstrap along with our custom application-specific classes.

At times, it is necessary to override the default bootstrap styles using our custom styles, including colors and other css attributes defined in _variables.scss.

However, we are encountering duplicated css in the browser; displaying both our custom styles and the default bootstrap styles which are not required due to lower specificity.

We are looking for a solution to prevent compiling unnecessary sass code while also refraining from modifying the bootstrap code within ./node_modules/.

Answer №1

Learn how to customize Bootstrap (version 4.x) defaults.

Study the source code

Begin by examining the bootstrap.scss file, which provides a detailed view of how the framework is structured component by component. You have the option to eliminate unnecessary components to streamline Bootstrap, but it's advisable to hold off on making changes at this point.

Next, explore the _variables.scss file. By reviewing this file, you'll discover that all customizable Bootstrap styles are defined here, including color schemes. This allows you to establish custom colors not just for buttons but for the entire framework. While you may be tempted to modify variables immediately, it's best to follow a specific approach.

Create a customization file

Avoid altering the original source code and instead create a new file named myproject.scss outside the Bootstrap source directory. Keeping modifications separate facilitates seamless future upgrades of Bootstrap.

Implement variable overrides

You can now begin transferring the variables you wish to adjust. Note that variables in _variables.scss feature the !default flag, allowing them to be overridden elsewhere. For instance, if you desire a different secondary color, locate the definition like $secondary, and add it with a new value in myproject.scss:

$secondary: #dd5679;

Include as many variable overrides as needed.

Integrate Bootstrap

Subsequently, import Bootstrap into the file. You can either incorporate the complete bootstrap.scss:

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

Or manually copy the contents of bootstrap.scss, update the paths accordingly, and selectively comment out unwanted components:

@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 initial three imports ("functions", "variables", and "mixins") are essential core components and should not be excluded.

Incorporate project styles

Add your customized styles after this step. If there are numerous styles, consider organizing them into individual partial files like _mybuttons.scss (remember to begin names of partial files with an underscore), and then import them.

@import "mybuttons";

Your personalized Bootstrap source file is now prepared.

Compile to CSS

The resulting myproject.css file should replace the original Bootstrap CSS file when loading the page.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML/CSS Inactive: Troubleshooting and Solutions

There seems to be an issue with the :active state, if anyone can spot the problem and provide assistance, it would be greatly appreciated. Here is the HTML code: http://pastebin.com/4wCi3L2Z And here is the CSS code: http://pastebin.com/jZvgdDaA Thank y ...

A guide to personalizing the woocommerce 'best-selling items' widget

WooCommerce widgets are great, but I'm struggling to customize the styling. Can anyone lend a hand? ...

Having images that are too large in size and using a high percentage can

I'm encountering a strange issue. When I define the width and height of my images in pixels in my CSS file, all my jQuery animations and events work perfectly fine. However, when I switch to using percentages or 'auto' for the image dimensio ...

Show the bottom left quarter of the image in an HTML display

Currently, I am still new to HTML coding and experimenting with displaying specific sections of an image. The current code I have is this: <img id="theImg" style="width:100%;" src="https://'myimage.jpg'" /> I'm struggling to figure o ...

Transform all characters to lowercase, then capitalize them using only CSS

I came across a discussion on this topic at: First lowercase the text then capitalize it. Is it possible with CSS? However, the solution provided was not purely based on CSS. I have a div that contains the following text: <div> RaWr rAwR </div&g ...

Tips for entering the lowest and highest values into the input field

I am looking to track the daily minimum and maximum prices of shares for various companies. Currently, I'm attempting to utilize this tag: <input type="number" name="number" placeholder="minprice"> <input type="number" name="number" place ...

Is there a way to insert a space in a single iteration of an SQL query in PHP without impacting the others?

Is there a way to add a space between two words for certain iterations of an SQL query without affecting all uses? I attempted using CSS margins, but it didn't have the desired effect. Here is a snippet of the code I am currently working on: <div ...

Adding Dynamic Shadow to Bootstrap 4 Card

I've been trying to figure out how I can add an animated shadow effect to a Bootstrap 4 Card. Currently, my Cards are arranged like this: Image of Bootstrap Card My objective is to create an animated shadow around the card similar to the one shown i ...

CSS selector for children up to a specific element is found

Is there a CSS selector that can target all descendants until a specific tag is reached? For example, given the DOM structure below, I want to apply a selector to "id1" so that it styles all descendants up to but not including "id4". This means the divs c ...

Managing images in JavaScript while conserving memory

Welcome I am embarking on a project to construct a webpage using HTML, CSS, JavaScript (jQuery), and nearly 1000 images. The length of the HTML page is substantial, around 5000px. My vision involves creating a captivating visual experience for users as t ...

How can certain HTML be redirected or hidden for users on mobile devices?

Lately, I've been putting the final touches on my website to present to potential employers as I will soon be starting my job search after graduation. I recently added an "about" section with parallax scrolling effect, but unfortunately, it's not ...

How can I integrate material-ui with react-jsonschema-form?

Currently, I am working on a form utilizing react-jsonschema-form. However, I would like to further enhance the appearance of my application (including the form) by incorporating Material-UI. I am encountering difficulties integrating both frameworks toget ...

Show the content within the carousel indicators div

I'm working with a bootstrap carousel that has 3 tabs and I want to name them Step 1, Step 2, and Step 3. Even though I've placed these names in the carousel-indicator divs, they are not showing up. Any assistance on this issue would be greatly ...

Item that was chosen is not visible in the dropdown menu

Here is a snippet of HTML code: <div class="col-lg-7 col-md-7 col-sm-7"> <select class="tblselect2 "> <option value="">Item 1</option> <option value="">Item 1</option> </select> </div> T ...

Hide the div only if a specific element is found on the page

I am currently exploring different methods to show or hide a left-hand navigation menu on my Volusion store. Is it possible to use JavaScript or jQuery to alter the CSS display property of a div based on the presence of another element on the page? The i ...

What is the reason for WordPress theme CSS and JS files loading after the head tag?

I'm experiencing an issue where my Wordpress theme's CSS and JS files are loading after the head tag. Additionally, when I view the source in Firefox, they are showing up in red, indicating incorrect markup. Can anyone help me figure out what&ap ...

Displaying text around columns and images using Bootstrap 4

Hello, I am facing an issue with my text not wrapping around the images while using Bootstrap 4. Instead of wrapping around, it stays on the right side. Could it be because I placed the images in separate columns? I want the two images to stack on top of ...

Passing parameters in a jQuery function through a button click

I am trying to figure out how to pass the @item.Id value as a parameter in the click function for a button. Here is the code snippet: <button id="buttonEdit" style="color:darkgreen" data-toggle="modal" data-target="#Ed ...

Can you explain the distinction between Declared Values and Specified Values?

I found myself confused by the distinction between Declared Values and Specified Values. Initially, I believed that the Declared Values were set by the author. These values then filter down to a single value, known as the "winning value", which becomes t ...

The appearance of a <div> element results in a border being applied to a different element

When hovering between the two borders, an undesirable (albeit very small) border appears around them. Can anyone assist me with this issue and explain why it is happening? I have attempted to set a transparent border on each element but without success. R ...