What is the best way to break a continuous string of white spaces in HTML?

I am dealing with incoming json data that is being displayed in table cells. Some of these cells contain "reserved white space" which I am able to display and highlight using various CSS properties such as pre/pre-wrap and background-color. However, a problem arises when there are excessive white spaces in certain cells, causing them to overflow out of the table. I have tried setting the width and max-width of the parent tag but it doesn't solve the issue. I have also searched for solutions on breaking strings containing spaces with pre-wrap, but they do not work if there are only white spaces without other characters. Is there any workaround for this? (I am working with angular 10.)

Here's what I'm attempting to achieve:

<tr
    mdbTableCol
    *ngFor="let item of attr.elements; let k = index"
>
    <ng-container>
        <td class="small_column_width">
            <div>
                <span [ngClass]="{
                    classA: item.status === 'A',
                    classB: item.status === 'B'
                }">
                    {{ item.value }}
                </span>
            </div>
        </td>
    </ng-container>
</tr>

Sometimes, the value of item.value consists of multiple white spaces, which can be up to 250 characters long. In such cases, displaying these values may cause the table to overflow entirely. Is there a way to effectively break the string or restrict its width?

Component.css:

.classA {
    background-color: #4caf50 !important;
    color: whitesmoke;
    max-width: 50px !important;
    white-space: pre-wrap;
    word-break: break-all;
}
.classB {
    background-color: #ffbf00 !important;
    max-width: 50px !important;
    white-space: pre-wrap;
    word-break: break-all;
}

Answer №1

My suggestion is to start by rendering it without any additional spaces

<span ...>{{
   tkt?.records[i]?.attributes[j].elements[k]
     .properties[0].value
}}</span> make sure there are no spaces between }} and <, so that no extra spaces appear.

Next, you can adjust the value using a pipe character

{{
  tkt?.records[i]?.attributes[j].elements[k]
  .properties[0].value | stripManySpaces
}}

Implement some stripping logic inside, like

return originalString.replace(/\s{4,}/g, '    ')

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

Hugo: Utilizing template variables within JavaScript files

Within my Hugo website, the baseUrl configuration is set to: website.com/variable. If an image element is present in a .html file (such as < img src="/images/image.png" >), the baseUrl is appended resulting in the final URL being: website. ...

Footer displaying page numbering

I recently installed dompdf 0.83 and have been able to generate documents successfully. However, I am struggling to create a footer that displays the page number out of the total number of pages. Both css and script methods have not been effective for me s ...

Do you have any tips on incorporating a sinking hover effect to an image or link?

Custom Arrow Icon I Want to Add Animation To I have designed an arrow icon image that functions as a link. It is positioned on top of a background image. When the user hovers over the arrow, I would like to implement a "sink" effect similar to the example ...

Prevent duplicate scrollbars by disabling the parent scrollbar

Within my setup, the parent component is set with overflow: 'auto' and I am unable to alter this setting. Meanwhile, the child component contains an iframe with its own scrolling behavior. How can I prevent the parent from scrolling while still ...

Is there an easy method for customizing scrollbars?

I'm looking to include an "overflow:scroll" in a navigation div, but the default scrollbar on windows is unattractive. Is there a simple way to customize the scrollbar without having to download extra JavaScript libraries, APIs, and so on? ...

Assistance with themed implementation for single-page web applications in JavaScript

My goal is to incorporate theme support into my single page application. The catch is that the theme change needs to be done locally through JavaScript without making any server calls, in order to work in offline mode. Since I am using angularjs, the HTML ...

Executing Multiple setTimeout jQuery in a Class: A Comprehensive Guide

Is there a way to hide elements with the class .content and the attribute .data-time using the setTimeout() function in jQuery? Here is the HTML code: <div class="content first" data-time="200"> </div> <div class="content second" data-time ...

Managing asynchronous data retrieval using rxjs

Within my service, I use an Observable to load data in the constructor. Later on, this data can be accessed using a getter, which should either return the data immediately if it's available or wait until the loading process is complete. Here is an exa ...

Create a single CSS style rather than using multiple CSS styles

I currently have multiple CSS styles applied: form#reply_form-c1r1.odgovor div#cmtforms-c1r1 input{color:red} form#reply_form-c2r1.odgovor div#cmtforms-c2r1 input{color:red} form#reply_form-c3r1.odgovor div#cmtforms-c3r1 input{color:red} form#reply_form-c4 ...

Are external JavaScript files cached in the same manner as images?

Is it possible for a single pagehandler script to be called from the browser cache when navigating between multiple pages that refer to it? ...

Reacting to the dynamic removal of spaces

Is there a way to dynamically remove the space between removed chips and older chips in Material-UI when deleting one of them? <div className="row contactsContainer"> {contacts.map((contac ...

"Jest test.each is throwing errors due to improper data types

Currently, I am utilizing Jest#test.each to execute some unit tests. Below is the code snippet: const invalidTestCases = [ [null, TypeError], [undefined, TypeError], [false, TypeError], [true, TypeError], ]; describe('normalizeNames', ...

Search the database using a single text field and then automatically fill in the remaining fields using PHP

Being a beginner in PHP and Ajax, I find myself quite lost in the process. I have set up a MySQL database and created several forms in PHP. One of the forms is supposed to display customer information with text fields, including a search field for phone nu ...

Angular.js animation for element movement

I'm currently struggling to create a simple move animation. I am aiming for a similar effect to the one shown in this example (taken from another SO question). So far, I have managed to achieve this using the .animation() method. This is essentiall ...

How can you proceed with a loop in Angular even after receiving a 404 response?

I couldn't find a solution on how to handle the 404 response within a loop in Angular, so I'm reaching out for help here. Here's the code snippet I'm working with: for(let order of orders){ this.api.getURL(order.resource_url).then ...

How can we use a :before tag element to create a hovering effect on an image that remains clickable?

My objective is to create an image that is wrapped in a link. The link will have a pseudo element :before that adds a black overlay on hover. I want the image to remain clickable, but no matter what I try, the pseudo element won't position correctly o ...

Unable to move cursor in contenteditable section

I am currently working on developing a rich text editor in React, but I have encountered an issue that has me stuck. The problem I am facing is that as I type each character, the insertion point does not update accordingly, causing the cursor to remain stu ...

What are some techniques for highlighting the significance of specific items within an unorganized list?

My goal is to utilize semantic markup to highlight the importance or priority of each item. Let me illustrate what I mean with an example. <ul itemscope itemtype="http://schema.org/ItemList"> <h2 itemprop="name">Wish List</h2><br&g ...

Generating HTML content using Node.js

I am completely new to the MEAN-stack and have been trying to develop an application on openshift. However, I am facing difficulties in rendering a new page. Despite my efforts to search for solutions online, I keep encountering this error that I can&apos ...

What is the best way to add custom styles to an Ext JS 'tabpanel' xtype using the 'style

Is there a way to change the style of a Ext.tab.Panel element using inline CSS structure like how it's done for a xtype: button element? { xtype: "button", itemId: "imageUploadButton1", text: "Uploader", style: { background : ' ...