Is it considered a bad habit to utilize custom CSS on Bootstrap classes?

Imagine I have the following code snippet in my index.html file, and I am utilizing Bootstrap:

<head>
bootstrap stylesheet
main.css stylesheet
...

<button class="btn btn-primary">Button</button

The class "btn-primary" is used to style the button blue.

Now, I want to change all my primary buttons to be red instead.

Should I directly modify the Bootstrap class or create my own custom class in main.css?

btn-primary{ background: red; }

OR

btn-red{ background: red; } //applying this class to the specific button

My only concern here is about following best practices.

Both approaches will achieve the desired result.

I simply wish to know if using Bootstrap's classes in this way is considered a frowned-upon practice. What do you think?

Answer №1

It's important to follow good coding practices by refraining from editing the framework code provided in the files.

In my own approach, I create an override.css file (or section within a file) specifically for overriding framework styles.

For example:

/* Bootstrap.css */
.btn-primary {
    background-color: not-red;
}

/* Override.css - make sure this loads after Bootstrap.css */
.btn-primary {
    background-color: red;
}

**Keep in mind that frameworks may have more specific selectors than just class styles, such as form .btn-primary for buttons inside forms. In such cases, you can either be more specific with your styling or use !important to ensure your style is applied.

By using this method, updating the framework like Bootstrap becomes easier while maintaining your custom styles. This also helps prevent redundant classes and allows you to combine Bootstrap classes with your own unique styles effectively.

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

Inline-block elements within other inline-block elements may not wrap correctly

Perfect Solution from Oriol: Oriol provided a great solution to my issue. Instead of using inline-block on both containers, he suggested floating the first container and leaving the second container with no styling. Additionally, he recommended using overf ...

Achieve smooth scaling of font size in CSS according to body width without the need for JavaScript

Can the font size be smoothly scaled in relation to the width of the body or parent element? I am seeking a solution that does not involve javascript. Is it feasible to relate rem to a specific width? ...

Having trouble with Fancybox loading images from an external URL?

Here is an example of functioning HTML code: <a class="fancybox-gallery" href="http://sale.coupsoft.com/uploads/938218/logo.png"> <img class="animate scale animated" src="http://sale.coupsoft.com/uploads/938218/logo.png" alt="Image1"> ...

Only the Backdrop is triggered by Bootstrap Modal

I've been struggling to figure out why the bootstrap modal is not showing up after hours of troubleshooting. All the files and syntax seem correct, as I have compared them with another working site where the modal functions perfectly. I even disabled ...

Eliminating the final space in CSS flexbox layout

When I set the display of an element to flex, I noticed that the last space in a text string gets removed. <div class="has_flex"> Some text <a href="link">Link</a></div> After setting display to flex: <div class="has_flex"> ...

Issue with flex-grow property not functioning as expected in Internet Explorer 11

Hello! I'm running into an issue with flexbox in Internet Explorer: http://jsfiddle.net/EvvBH/ I've noticed that the #two element has flex: auto, which is meant to make it expand to fill the container, even if there's limited content. Ho ...

clicking on internal links in the bootstrap side menu causes it to jump

Im trying to add a side menu to a help page. The menu and internal links are functioning properly, but when clicked, the side menu partially goes behind the navbar. As a beginner, I'm not sure how to resolve this issue. Can someone offer some guidan ...

Is there a way to make jQuery Validate display errors within the form elements rather than outside of them?

Given the jQuery Validate basic example provided in the link below, what method can be used to display error messages within form elements whenever feasible (excluding checkboxes)? http://docs.jquery.com/Plugins/validation#source ...

The orientation of the rating stars has transformed into a vertical layout

I'm having trouble creating a rating interface that prompts the user to rate the website (by clicking the stars) and leave a comment. The stars are displaying vertically instead of horizontally, as shown in the image provided in the link. I've tr ...

Controlling CSS Styles in Angular using TypeScript

Currently, I am working on an Angular project that involves dynamically populating a calendar. In addition to this, I have a range of dates and the task at hand is to modify the background for specific days within this date range. To manage dates effective ...

Capturing Ajax Success in Rails

After extensive googling, I have been unable to find my mistake. As a beginner with JS, this may be an easy fix for someone with more experience. Working with Rails 4. I am working on a modal that contains a form and I want to perform certain actions afte ...

The CSS 'd' attribute for the <path> element is not functioning properly in Firefox

My generated code cannot be directly modified due to its association with a large JS code. A portion of this code creates an SVG shape, which I attempted to override through CSS. However, my solution does not function properly in Firefox! Below is the ori ...

Joomla CSS styling malfunctioning

I've been exploring the creation of an in line menu using Joomla 1.6 and CSS. The issue arises when Joomla removes <span class="dmenu"> from the document before saving, despite having all cleanup options turned off. This led me to try a couple ...

How can you apply the CSS styles of your grandparents without inheriting the styles of

Although it may seem unlikely, I still wanted to inquire about the possibility. <body> //body with 'back' background <div id="div1"> //div1 with 'front' background <div id="child1"> //c ...

Utilize itextsharp to transform HTML into PDF files

When I try to convert HTML to PDF using iTextSharp, the CSS styling I apply to the webpage does not get carried over to the converted PDF file. Here is the CSS code I am using: <style type="text/css"> .cssformat { ...

Arranging Cards in Layers on Smaller Screens Using Bootstrap Framework

My row of cards in various sizes looks good on larger screens like this: https://i.sstatic.net/1qF53.png However, on smaller screens, the cards lose their appeal. https://i.sstatic.net/o1NS8.png To improve this, I want to stack the cards so that the ke ...

Issue with CSS margin-top not displaying properly in Firefox and Internet Explorer, but working fine in Chrome and Opera

I'm encountering difficulties with the margins on my website. It seems like margin-top isn't functioning properly in Firefox and IE, even though it is working correctly in Chrome and Opera. Despite trying various techniques and searching online f ...

In Javascript, navigate to a specific section by scrolling down

Currently, I am in the process of enhancing my portfolio website. My goal is to incorporate a CSS class once the user scrolls to a specific section on the page. (I plan to achieve this using a JavaScript event). One approach I am considering is initially ...

JQuery: Techniques for accessing the assigned font of an element

Can the assigned font of an element be retrieved using jQuery? For example, if there is this CSS: #element { font-family: blahblah,Arial; } In the above case, the Arial font will be assigned to #element. Is it possible to extract that information using ...

How to arrange messages in a chat box using Bootstrap 4 without the need for JavaScript

Is there a way to adjust my chat box so that messages appear at the bottom, like in any chat app? I am currently using Django and Bootstrap 4, here is the code I have: <div class="list-group flex-column-reverse " id="Chatt ...