tips for implementing word-breaking in css

I encountered a problem on my support ticket page where the message text was overflowing out of the designated grey container. Despite trying to utilize the "word-wrap" CSS class, the issue persisted. Here's the code snippet with placeholders for variables in my Blazor framework:

<style>
    .card-wrapper {
        min-width: 320px;
    }
    .login-card {
        position: relative;
        background-color: #eaeaea;
        padding: 2rem 1.5rem;
        max-width: 700px;
        margin: auto;
        border-radius: 3rem;
        box-shadow: rgba(0, 0, 0, 0.09) 2px 6px 15px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .login-header {
        margin: 1rem;
        width: 100%;
        text-align: center;
        text-transform: capitalize;
    }
    .login-info {
        width: 100%;
    }
</style>

<div class="card-wrapper">
        <div class="login-card">
            <div class="login-header">
                <h1 class="header-one">Support ticket # @STicket.Id</h1>
            </div>
            <div class="login-info">
                <div style="word-wrap: break-word; overflow-wrap: break-word;">
                    <p>Date: @STicket.DateSent.ToShortDateString() : @STicket.DateSent.ToShortTimeString()</p>
                    <h4>Issue:</h4>
                    <label style="word-wrap: break-word; overflow-wrap: break-word;">@STicket.Message</label>
            
                    <h4>Messages:</h4>
                    <div class="ticket-comments">
                        @foreach (var msg in Messages)
                        {
                            <div class="ticket-comment" style="word-wrap: break-word; overflow-wrap: break-word;">
                                <span>Date: @msg.Date.ToShortDateString() @msg.Date.ToShortTimeString()</span>
                                <label style="word-wrap: break-word; overflow-wrap: break-word;">@msg.Message</label>
                            </div>
                        }
                    </div>
                <h4>Add new response</h4>
                </div>
                
                <form @onsubmit="Submit">
                    <div class="reg-form">
                        <label for="message">Your message</label>
                        <textarea class="input" id="message" @bind="NewMessage.Message" rows="3"></textarea>
                    </div>
                    <div class="login-reset">
                        <button type="submit" class="reset-btn">Send</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

https://i.sstatic.net/1u7k8.png

The separate file containing the defined style classes has also been included.

Answer №1

Here is a code snippet for you to try:

<div style="white-space: -moz-pre-wrap !important; white-space: -pre-wrap;  white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word; white-space: -webkit-pre-wrap; word-break: break-all; white-space: normal;">
                        <p>Date: @STicket.DateSent.ToShortDateString() : @STicket.DateSent.ToShortTimeString()</p>
                        <h4>Issue:</h4>
                        <label style="word-wrap: break-word">@STicket.Message</label>
                
                        <h4>Messages:</h4>
                        <div class="ticket-comments">
                            @foreach (var msg in Messages)
                            {
                                <div class="ticket-comment" >
                                    <span>Date: @msg.Date.ToShortDateString() @msg.Date.ToShortTimeString()</span>
                                    <label style="word-wrap: break-word">@msg.Message</label>
                                </div>
                            }
                        </div>
                    <h4>Add new response</h4>
                    </div>
                    
                    <form @onsubmit="Submit">
                        <div class="reg-form">
                            <label for="message">Your message</label>
                            <textarea class="input" id="message" @bind="NewMessage.Message" rows="3"></textarea>
                        </div>
                        <div class="login-reset">
                            <button type="submit" class="reset-btn">Send</button>
                        </div>
                    </form>

Answer №2

To prevent words from exceeding the width of their parent container in CSS, include word-wrap: break-word;

<div class="box" style="width: 380px; background: #f9f9f9; padding: 10px;">
<h2>A title</h2>
<p style="word-wrap: break-word;">adfdsafsdfasdfsdfdsafdsafsdafasdfasdfasdfsadfsadfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf</p>
  </div>

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

Having trouble with applying a custom class to React Bootstrap tabs?

When utilizing the react bootstrap tabs component, I encountered an issue with implementing a custom CSS style for the nav-link element within it using a customized parent class indicator. <Tabs defaultActiveKey="signup_renter" i ...

Avoid making the check mark disappear in an SVG animation

After perfecting my CSS animation, I have the circle looping around to complete its shape while a check mark fills in the middle with a slight delay. Once both shapes are completed simultaneously, the check mark disappears. The reason for the disappearing ...

Issue with Django JQuery datatable ajax refresh: sorting buttons not displaying and function calls not working

Currently, I am utilizing Django along with JQuery and jquery-datatables-bs3 to generate a table for my models. The functionality allows users to add or remove table rows, which in turn creates or deletes model instances within the database. Each row featu ...

What is the best method for saving a chosen radio button into an array?

I am currently developing an online examination system where questions are retrieved from a database using PHP and displayed through AJAX. I am facing an issue where I am unable to capture the selected radio button value and store it in an array. Despite e ...

Strange formatting in the internet explorer browser (IE)

I have encountered an issue with CSS animation elements on the home page. The animations appear correctly in Google Chrome, but there is a strange indentation problem when viewed in IE. I have tried several solutions without success. Although I am not a w ...

Is there a definitive way to distinguish between scrollTop and scrollHeight in web development?

For instance, function checkingScroll(){ var newHeight = element.scrollHeight; var scrollTopValue = element.scrollTop; alert("The scrollHeight property is: " + newHeight + "px"); alert("The scrollTop property is: " + scrollTopValue ...

What is the process for inserting a scroll bar within a div element?

   I have recently created a webpage using some divs, along with a bit of CSS and JavaScript. I am struggling to figure out how to add a scrollbar to one of my divs. The code is not overly complex, as it includes both CSS and JavaScript. <html> & ...

Unable to update innerHTML of asp.net literal tag using JavaScript

Here is a snippet of my JavaScript code: <script type="text/javascript">function CountCharacters(idTxtBox, idCharCount) { var maxLimit = 500; var remainingChars = maxLimit - document.getElementById(idTxtBox).value.length; do ...

Tips for adding a numerical prefix to each line when the textarea is in editable mode

After conducting extensive research on Google, I have been unable to find a satisfactory solution. What I am aiming for is similar to the concept illustrated in this image: https://i.sstatic.net/iq6DE.png I should mention that my current technology stack ...

What is the best way to make an Ajax request to verify a registered user input if I am not using jQuery?

On my basic registration form, there is a field for email: <input id="Email" name="Email" placeholder="Email" required="required " type="text" value="" /> <button id="register" type="button" ...

Are there any design techniques for websites that can help make CSS more stable and less unpredictable?

What inspires me to write this question is my passion for creating visually appealing websites, although I struggle with CSS. I believe that the key is trial and error. It's all about experimenting, testing in different browsers, and refining the des ...

The local HTTP server is having trouble establishing a connection with the Firebase user

Utilizing Visual Studio Code editor and a node js HTTP server, I have developed a simple login page and created a user in firebase. However, I encountered an issue with logging in using the username. The code for the webpage was written in HTML and bootstr ...

Play button and directional indicators missing from Bootstrap video carousel

I'm facing an issue with my carousel that contains 4 videos, and I can't seem to figure out why. The problem is that I can only switch between videos by using the circles at the bottom of the screen; clicking on the left or right arrow buttons or ...

Increase the padding and add a border to separate the list items in bootstrap 4

I have a header with some links and I want to create space and add a border between them. I attempted using ::before pseudo-content, but it's not displaying the content where I intended. .header_links_ul { list-style: none; margin: 0; paddin ...

Gathering information from an HTML form and displaying it through JavaScript by utilizing Bootstrap's card component

I've been working on integrating form data from my index.html page into a card component using Bootstrap. I've successfully collected the data in the backend, but now I'm struggling to display it on the frontend as a card component. Any help ...

Updating image attributes using jQuery within an .each loop

My challenge involves a textarea where I paste HTML code, followed by a button that should display all the images from that code along with their respective alt text and titles. The goal is to easily update the alt text and titles for each image individual ...

Capturing all subdomains dynamically using simple HTML/jQuery techniques

Currently, I am working on revamping an older web page with a monolithic structure. In my code, I have implemented a switch statement that evaluates the URL's pathname and then executes various functions to display or hide elements on the page. switc ...

Issues with loading SASS or source maps arise when dealing with nested structures

After reorganizing my SASS files into a nested structure, I encountered an issue where inspecting elements in Chrome led me to incorrect source maps. For instance, when trying to inspect a button, instead of being directed to the _buttons partial as expect ...

Tips for toggling password visibility by clicking outside a password field

As I work on creating a password field that allows users to toggle the visibility of their password by clicking an eye icon, I am facing a challenge. I want the password to be hidden automatically when the user clicks outside the field, which requires a co ...

What is the best way to conceal a specific class of DIV within a container, and how can I also hide another DIV within the same container by

I have a DIV class containing around 10 elements within a container. I am looking to implement a feature where these elements are hidden and shown one by one every 15 seconds using jQuery with a smooth fadeOut() effect. Your assistance in achieving this wo ...