Incorporate Bootstrap and SCSS by utilizing class names within the SCSS code rather than directly in the HTML files

When working with Bootstrap 4, I have discovered the text-truncate class that can be used in an element. For example:

<span class="text-truncate">Any very long text</span>

Now, my goal is to apply this text-truncate class to multiple objects by defining it in a .scss file rather than directly in .html files.

Is there a way to achieve this?

Can I implement something like the following:

@import "text-truncate" from "bootstrap.scss";

.myBeautifulDiv {
  use text-truncate;
}

This would be incredibly useful! Is it feasible?

Answer №1

Utilize scss to create placeholder classes,

The names of these placeholder classes must commence with %. The classes themselves will not appear in the final css files.

However, they can be incorporated into other classes as demonstrated below.

Ensure that you load your bootstrap css beforehand.

 /*Inside the bootstrap file*/
    .text-truncate{
      text-overflow:ellipsis;
      ...
      ...
    }

 /*Within your scss file*/

%truncatedtext {  /*This serves as a placeholder class*/
 @extend .text-truncate;  /*This imports/includes the actual bootstrap code*/
}

.class-a {
  @extend %truncatedtext;
  color: #000000;
}

.class-b {
  @extend %truncatedtext;
  color: red;
}

The resulting output would be

.text-truncate, .class-a, .class-b {
  /*Truncate css code*/
}

.class-a {
  color: #000000;
}

.class-b {
  color: red;
}

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

Navigate to the specified text in the dropdown menu based on the ul li selection

Is it possible to achieve a functionality similar to that of a select box, where typing a keyword that matches an option in the select box will automatically move the pointer to that option? I am looking to implement a similar feature in my HTML code: < ...

Apply CSS3 to target all td elements within the main table that contains nested tables

Today, I delved into the world of CSS3 and HTML5 through Codecademy. HTML: <table class="main"> <tr class="questioncontainer"> <td class="serialnumber">1.</td> <td class="question" colspan="2">What is the ...

Sort an HTML table based on concealed data

I've encountered a challenge with my HTML table that utilizes the Bootstrap table library for sorting data by column values. While it works smoothly for float or int data types, I have a specific column dedicated to displaying times in a string format ...

Updating the UpdatePanel causes duplication of CssClass

In my ASP.NET Webforms project, I have created a user control that inherits from the LinkButton. This user control has a property to adjust the size by adding predefined CSS classes to the control. Protected Overrides Sub CreateChildControls() Dim Siz ...

Rearranging the stacking order of CSS pseudo-element

In the midst of creating a 3-stage progress bar using only CSS. The current design is displayed below: #progBar { background-color:#bdbdbd; padding:1.5vw; posi ...

Stylish navigation bars with CSS and Jquery for seamless browsing experience

I am attempting to create a simplistic horizontal list menu similar to the one displayed in this example. I have copied the list and CSS to jsFiddle (New Example Update), but it does not function as desired. I have searched for similar examples to guide me ...

Using JavaFX to create a StackedAreaChart and implementing the setLowerBound method

I am encountering some issues with the Class StackedAreaChart and setLowerBound function. When I enable the autoRangingProperty to true, everything works fine. However, when I disable autoRanging and apply certain parameters, I encounter some bugs. imp ...

The functionality of changing the checkbox to "checked" by clicking on the span is not

How can I create a toggle button with a checkbox using css and jquery? Clicking on the span representing the toggle button should change the checked property of the checkbox. Currently, the span does not change the property, even though it triggers the c ...

Enhance the aesthetic appeal of the Search and Filters component in ASP.NET Core using Bootstrap styling

I am struggling to enhance the style and layout of multiple search filters on the page so that the User Experience is improved. I'm unsure how to effectively design this search component. <section class="search-sec"> <div class=&qu ...

Alignment of Bootstrap 5 card text and buttons

I've been diving into the BootStrap 5 Crash Course from this YouTube link: https://www.youtube.com/watch?v=4sosXZsdy-s. The final website from the tutorial can be found here: One specific part focuses on using Cards, but I'm facing an issue wher ...

The HTML button renders all JavaScript functions as null and void

After adding a button line to enable a clock in HTML and the corresponding function in a <script> tag (which may or may not work), suddenly all functions on the page become undefined. There is only one other function on that page, so it could be caus ...

Troubleshooting problems with Z-Index div hover overlay on [windows] and Webkit browsers (Safari, Opera, etc)

Note: This issue is specific to Windows Webkit and does not affect Macs I am attempting to create a grid with fixed-size cells containing background images. When hovered over, the background image changes and the cell's height increases, overlaying t ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

The design of the button appears unappealing when viewed in

Hi there! I am currently working on creating a button for Outlook 2010, but I am running into an issue. The button image is not aligning vertically in the center, and the padding and margin are not displaying properly. It works perfectly fine on most ema ...

Position the icon to the left side of the button using Bootstrap

Need help with centering text on a bootstrap 3 button while aligning the font awesome icon to the left side. <button type="button" class="btn btn-default btn-lg btn-block"> <i class="fa fa-home pull-left"></i> Home </button> Usi ...

Using PHP Hover should apply CSS styles exclusively to the specified element

I have a piece of PHP code that displays cards. What I need: When a user hovers over the entire element, I want to make the title bolder, move the arrow to the left, and zoom in on the image of that particular hovered element. <div class="posti-class ...

Creating a sleek and responsive data grid using a combination of Bootstrap and

My Telerik RadGrid works perfectly on a normal page, but as soon as I add Bootstrap's CSS, it turns ugly! How can I fix this? Prior to Adding Bootstrap: After Adding Bootstrap: Code for Normal Page <%@ Page Language="C#" AutoEventWireup="true" ...

I am attempting to change a "+" symbol to a "-" symbol using a Bootstrap toggle feature

Click on the text to reveal more information that drops down below. I am using Bootstrap icons and attempting to show a "+" icon when the toggle is collapsed, and a "-" icon when it's open. I've been trying to use display properties, but haven&ap ...

stopping the current menu selection from altering its color

How can I stop the color change when hovering over the selected menu item? Here's my code: <html> <head> <title> Another Page </title> <link rel="stylesheet" href="style4.css" type="text/css" /> & ...

How can I insert a item into an Array using JavaScript code?

My instructor set up an array in my JavaScript file that is off limits for me to modify. My task is to add new objects to it through code without directly manipulating the existing array. Here's a snapshot of what my array contains: const words = [{ ...