Put text that has been split with commas onto the next line within a table cell

I am facing an issue where text with commas is splitting to the next line. To better understand, here's an example:

https://i.sstatic.net/85WnY.png

Instead, what I would like is for the text to move to the next line without splitting. Here's the desired result:

https://i.sstatic.net/gJD0x.png

Below is the code snippet I am working with:

table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
<table style="width:100%">
  <tr>
    <th width="50%;">Address</th>
    <th width="50%;">Email</th>
  </tr>
  <tr>
    <td style="">416, Willow Brook Lane, Suite 567, Apartment Riverview, Brookside Complex, Greenfield Eastern District, Riverside County, California, United States</td>
    <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b757a767e5b7e767a727735787476">[email protected]</a></td>
  </tr>
</table>

Any suggestions on how I can achieve the desired output? Thank you.

Answer №1

To prevent automatic word wrapping, you can utilize the white-space: nowrap; property and manually control line breaks using line-break opportunity <wbr> tags. It's possible to create a script that inserts a <wbr> after every comma for improved wrapping.

When testing this code snippet, make sure to view the page in full and adjust the browser width to observe the differences in how text wraps between the two cells.

document.querySelectorAll('.break-at-commas').forEach(e => {
  e.innerHTML = e.innerHTML.replaceAll(',',',<wbr>')
})
table {
  border-collapse: collapse;
  width: 100%;
}
th, td {
  border: 1px solid black;
  width: 50%;
}
.break-at-commas {
  white-space: nowrap;
}
<table>
  <tr>
    <th>Break At Commas</th>
    <th>Default</th>
  </tr>
  <tr>
    <td class="break-at-commas">416, Willow Brook Lane, Suite 567, Apartment Riverview, Brookside Complex, Greenfield Eastern District, Riverside County, California, United States</td>
    <td>416, Willow Brook Lane, Suite 577, Apartment Riverview, Brookside Complex, Greenfield Eastern District, Riverside County, California, United States</td>
  </tr>
</table>

Answer №2

If you're looking to manipulate the contents of your td using JavaScript, one option is to turn each segment into an element with a display property of inline-block. I have created a pen that demonstrates how this can be achieved: https://codepen.io/motionharvest/pen/bGOjZPx

The code in the pen splits the text by comma, wraps each segment in a span, and applies the style of display: inline-block to each segment. While there shouldn't be any extra space after the comma, feel free to check it out for yourself.

let splitElem = document.getElementById("split");
let splitElemText = splitElem.innerText;
let splittedText = splitElemText.split(",");
splitElem.innerText = "";

splittedText.forEach((segment, index) => {
    let segmentElement = document.createElement("span");
    segmentElement.innerText = segment;
    if(index < splittedText.length - 1) {
        segmentElement.innerText += ", "
    }
    segmentElement.style.display = "inline-block";
    splitElem.append(segmentElement);
})
table,
th,
td {
    border: 1px solid black;
    border-collapse: collapse;
}
<table style="width:100%">
  <tr>
    <th width="50%;">Address</th>
    <th width="50%;">Email</th>
  </tr>
  <tr>
    <td style="" id="split">416, Willow Brook Lane, Suite 567, Apartment Riverview, Brookside Complex, Greenfield Eastern District, Riverside County, California, United States</td>
    <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d7d8d4dcf9dcd4d8d0d597dad6d4">[email protected]</a></td>
  </tr>
</table>

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

A fixed HTML div with a relative div is like having your own

I am in search of two divs with the following styles: <div style="height:20px;" /> <div style="height:100%;" /> These divs result in one being 20px tall and the other spanning 100% of the screen height, causing a vertical scrollbar that is eq ...

What's the best way to align two elements side by side regardless of window size changes?

I am looking to keep these two elements together in one row at all times, regardless of if the window is resized horizontally. Currently, they break into two rows as shown in this screenshot: http://gyazo.com/1820dc436dba2e827b330039109dc0ee I attempted ...

Navigate to the hover image with a swift roll

I have been searching extensively on the web but unfortunately, I haven't come across any relevant links. My goal is to create a customized menu. The menu should be an area-based rollover menu that transitions based on the section of the image being ...

What steps can I take to incorporate a verification question in order to combat spam?

I've been dealing with an influx of spam emails from my contact form. I'd like to implement a simple verification question, such as 'Zebras are black and...', where only a specific answer will allow the form to be submitted. How can I s ...

Event handler for the MouseLeave event is not successfully triggering when dealing with anchor

When viewing a link to a Codepen, the issue is most noticeable in Chrome: https://codepen.io/pkroupoderov/pen/jdRowv Sometimes, the mouseLeave event fails to trigger when a user quickly moves the cursor over multiple images, resulting in some images reta ...

Create "people" entities using the information provided in the input fields

I am looking for a way to automatically generate objects based on the data entered in the input fields. I have already created a constructor function, but I am unsure about how to pass the data from the inputs to the constructor. **My goal is to display ...

Achieving a Pushing Footer Design with Content

Hey guys, I'm working on a website and I'm having some trouble with the footer. It's sticking to the bottom of the page, but the content is overflowing it. Check out this screenshot for reference: . Here is my HTML/CSS code for the footer: ...

Masterful Text Pairing: Using Zen Coding Techniques for Crafting Intricate Nesting Structures

Result : <div id="header"></div> <div id="body"> <hello> <world></world> </hello> </div> Mapping to : div#header+div#body>hello>world Which command should be used to achieve the desi ...

Expanding Form Fields with jQuery: A Step-by-Step Guide

I am having trouble figuring out how to add and remove input fields dynamically on my webpage. I have three input fields with an "+Add More" button, but I can't quite grasp the logic for adding or removing these fields. Currently, I am using HTML and ...

The code that functions properly in Internet Explorer and Chrome is not functioning as expected in Firefox

I created a jQuery function that is designed to allow only numbers and one decimal point in an input field of type text. While it works perfectly fine in Internet Explorer and Chrome, I've encountered an issue with Firefox where it doesn't seem ...

Extract the chosen option from a dropdown menu, and then transfer it to another dropdown menu with a new corresponding value

Below is a select box I currently have: <td align="center"> <select id="selectW" name="type" onChange="this.form.submit()"> <option value="select...">select</option> <option value=" ...

Steps for implementing Onclick event within the <Script> tag to display a div:

I am currently working on a code that allows me to show and hide a <div> element after clicking on an advertisement from any website. This advertisement is generated by a script. How can I implement the onclick event within the <script> tag? T ...

Arrange elements in a vertical layout and align them to the left when they exceed the boundaries of their container

I am seeking a solution for laying out my design, similar to the one shown in this image Essentially, I have a container with a fixed height of 300px. I want to display a list vertically with each item taking up a width of 33%. If the list becomes too lon ...

Adjusting the div's background color according to a pre-determined choice in a select menu generated by PHP

My webpage features a select option menu generated by PHP, offering the choices 'Unverified', 'Verified', and 'Banned'. I am working on automatically changing the background color of the statusField based on the pre-selected ...

Column on the far right of the grid with a frozen position

I need to design an Angular grid where the last column remains frozen. This frozen column should display the summation of all row values, and the last row should be editable. I am able to create the Angular grid itself, but I am struggling with how to stru ...

Connecting buttons to JavaScript functions that interact with MySQL database entries

I am working on a task involving rendering a database table using XMLHttpRequest in JavaScript to a PHP page. My goal is to display each entry from the table as an HTML row/cell with two buttons within each "entry". These buttons should trigger specific Ja ...

Activate radio button exclusively for admin user

My current application includes an admin panel with functions to manage users. I use a while loop in the form creation to display 3 user types: Admin, Manager, and User, which are pulled from a database. Admin Manager User This is how my HTML Form look ...

Transfer a PHP variable between pages to retrieve the appropriate item from a button

In my project, I have two pages named content.php and pagecontent.php. In content.php, I am fetching product information from a database and displaying it in table format. The table has a grid layout with 2 rows and 3 columns. Each cell contains the descri ...

The input box fails to show larger values on the user's page

Show me the biggest number that the user enters in an input box and then display it back to them. I need some improvements to my code, ideally making it possible with just one input box instead of two. <!DOCTYPE html> <html la ...

Bootstrap Item Carousel - A Sleek Way to Showcase

I'm trying to figure out how to stop the infinite auto-scrolling and make it display per page when clicking the right arrow. Any ideas? $('.carousel.carousel-multi .item').each(function () { var next = $(this).next(); if (!next.leng ...