Can SCSS extend Bootstrap classes without the need to import the entire Bootstrap library?

In my custom project, I've defined a unique class in the sample.scss file.

.custom-class {
    @extend .rounded-pill;
    @extend .border;
    @extend .bg-light;
}

During compilation, an error is generated stating:

The target selector was not found. Utilize "@extend .rounded-pill !optional" to prevent this error

By changing it to @extend .rounded-pill !optional, I resolve the error. However, the classes .rounded-pill, .border, and .bg-light are not imported/applied to custom-class as intended. Is there a method to extend Bootstrap CSS classes without importing them directly into my project?

The goal is to include Bootstrap's CDS and create my own extending classes from Bootstrap.

If I incorporate

@import ../node_modules/bootstrap/scss/bootstrap';
, everything functions correctly; however, this means that the Bootstrap code is brought into my files, which is not desired.

UPDATED

To provide further clarity, here is an example of what I aim to achieve:

In the custom-close.scss file, when compiled, the following SCSS code should produce a new CSS file named custom-close.css.

@import ../node_modules/bootstrap/scss/_close';
@import ../node_modules/bootstrap/scss/_buttons';

.custom-close {
    @extend .btn;
    @extend .btn-close;
    padding: 0;
}

It can be observed that I'm importing the _close.scss and _buttons.scss files from the Bootstrap module.

However, importing both _close and _buttons leads to inclusion of ALL the code within those respective files in the final output file (custom.css).

Answer №1

In order to utilize the @extend feature, it is necessary to first import the relevant code.

If you are concerned about the impact of adding Bootstrap code to your files, rest assured that there are no drawbacks to this practice. When importing Bootstrap's CSS, whether in pre-compiled form or as SCSS, you are essentially incorporating Bootstrap's code into your project.

You can refer to the official documentation on customizing Bootstrap for more guidance.

If you wish to override any default settings, make sure to define the desired values prior to importing them from Bootstrap.

To make use of Bootstrap @mixins, importing them directly from Bootstrap is essential. There is no alternative method available.


Upon further clarification, it appears that by mentioning "importing Bootstrap code into my files," you may actually be referring to including Bootstrap code in your build process. To exclude a particular dependency from your package, mark it as external. The exact syntax for this declaration will vary depending on the build tool being used (rollup, webpack, vite), although they share similarities.

To inform projects consuming your package about additional required dependencies, you should also list these dependencies under peerDependencies in your package.json file. This ensures that a warning message will be displayed in the console for any project attempting to install your package without the necessary peer dependencies present.


As a side note, consider updating your imports as follows:

@use 'bootstrap/...'

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

Update Bootstrap Vue to change the colors of the breadcrumbs

Struggling to customize the look of bootstrap-vue breadcrumbs? I've attempted various combinations of classes and styles in the breadcrumb and breadcrumb-item tags, but to no avail. Every time, I end up with blue or grey text on a light grey backgroun ...

Implemented an HTML hyperlink element precisely positioned

I have the following HTML code. I am trying to fix the position of an anchor tag called "View All" so that it stays in a specific location. However, whenever the value in the dropdown menu changes, the anchor tag moves according to the length of the new se ...

Is there a way to implement the border-box property for Internet Explorer versions 6 and

Similar Question: How to implement box-sizing in IE7 Anyone know a method or workaround to apply box-sizing:border-box; in IE6 and IE7? Even just for IE7? ...

What is wrong with my notecards that they won't flip properly?

I am currently developing a text-to-flashcard program using HTML, CSS, and JS. One feature I'm working on is the ability to flip all flashcards at once with a single button click. Currently, the program only allows flipping from the back face to the f ...

The functionality of "Body Onload" for sending "ScrollHeight" is malfunctioning in Chrome and Safari

I came across an issue where the iframe_resize function in my code was not working as expected. After investigating further, I realized that the problem did not lie with the function itself. So, I decided to post a new question. Within my index.html file ...

Change the positioning of the image to a new column within the bootstrap grid when a

My website utilizes Bootstrap 4, featuring a <container> with two individual columns. For screen sizes within the Bootstrap 4 breakpoints of md-xl, these columns are displayed side by side with equal widths: col-md-6. On smaller screens like sm-xs, ...

Is there a way to align an element to the right of a centered element?

I am trying to center two inputs ("email" and "password") horizontally on the page. However, I also want to add another item to the right of them while keeping the inputs centered. <div style={{ display: "flex", justifyContent: "center" }}> < ...

What is the process for creating a Sass mixin that declares a selector at the base level, rather than nested within another

Apologies for the unconventional terminology in the query, but I am struggling to find better words to describe it. Hopefully, this example will clarify my intention: scss-syntax .my-smug-selector { @include my-smug-mixin(30px); } desired css-output ...

What are the reasons that execCommand does not function correctly when attempting to justify text?

Why isn't justify with execCommand working properly? Take a look at the code below: $('#JustifyLeft').click(function(){ document.execCommand("JustifyLeft", false, ""); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2 ...

Collapse in Bootstrap without any Animation

How can I add animation to the Bootstrap Collapse feature on my website? I am using an icon from the Font Awesome library to trigger the collapse effect, but currently, it just pops up without any animation. What could be causing this issue? You can view ...

What is the best way to halt a CSS transition using JavaScript when dealing with an image?

I am facing an issue while attempting to create a basic CSS transition using JavaScript. The goal is for the start button to trigger the movement of an element, based on the duration of the keyframe animation. Then, clicking the end button should make the ...

A menu bar featuring dual color schemes for visual differentiation

I am currently designing a navigation bar where one category stands out with a different color (blue) compared to the rest (green). The functionality I'm aiming for is that when the cursor moves away from the blue HOME category, it should change back ...

Placing a GIF on top of an image through the use of Javascript

Welcome to my first post on this platform! I'm currently in the process of creating a portfolio website featuring long panoramic photographs displayed within scrollable divs. You can check it out here: www.edentan.co.uk/portfolio.html. My challenge ri ...

Searching for table row elements using CSS and Vue.js

I am trying to alternate row colors in a table, but for some reason all rows end up being gray. <table> <tr v-for="i in item.env_vars" :style="{'background': index % 2 === 0 ? '#eee' : '#ccc' }"> & ...

React: Issue with CSS heights in grid-container not updating when state changes

In a container div, I have three elements: headerContainer, diagramContainer, labelContainer. The container has a fixed height and I want all elements to fill it. The diagramContainer should adjust its height dynamically based on the labelContainer's ...

Troubles with caching on Amazon S3

Just starting out in web development and I'm working on updating HTML and CSS files stored on Amazon S3. Our website's backend is hosted on Heroku, while the front-end is on Amazon S3. Whenever I make changes to a CSS file, I can immediately see ...

requirements needed for Bootstrap's image slider

When using just the Carousel functionality in JavaScript, which dependencies need to be included excluding `bootstrap.js` but including necessary code for Carousel (`carousel.js`, ...)? As per the docs, the setup code is: var myCarousel = document.querySe ...

What is the best way to align text and images for list items on the same line?

HTML:- ul { list-style-image: url(https://i.sstatic.net/yGSp1.png); } <ul> <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit, consequatur?</li> <li>Lorem ipsum dolor sit amet, consectetur adipisici ...

What is the best way to remove the inner border of a submit button in Internet Explorer and Opera?

Check out this form example: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Sa ...

Is there a way to perfectly center an SVG image and text within a linked button?

After creating a button that utilizes an SVG image icon on the left and text on the right, I encountered an issue where the text could not be perfectly centered in alignment with the icon. Here is how it currently appears: https://i.sstatic.net/FGGVi.png ...