Ensure that the <td> element remains within the confines of the window

When creating a table with some content, I encountered an issue where long strings would extend beyond the window. I attempted to set max-width: 80%; to limit the length, and also tried setting td, th = width: 240px;, but the problem persisted.

Next, I experimented with adding word-wrap: break-word;, however this did not solve the issue either.

.tableTheme {
    background: #0d5dd4;
    color: white;
}

.tableWidth {
    max-width: 80%;
}

.tableWidth td,
th {
    width: 240px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row justify-content-center">
    <div class="tableWidth">
        <table class="table table-bordered">
            <thead class="tableTheme">
                <tr>
                    <th scope="col">Test Suite Collection ID</th>
                    <th scope="col">Test Suite Collection Name</th>
                    <th scope="col">Test Suite ID</th>
                    <th scope="col">Test Suite Name</th>
                    <th scope="col">Test Case ID</th>
                    <th scope="col">Test Case Name</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>TSC</td>
                    <td>2</td>
                    <td>TS</td>
                    <td>3</td>
                    <td>TC</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE</td>
                    <td>2</td>
                    <td>TS</td>
                    <td>3</td>
                    <td>TC</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Answer №1

Simply include this code:

td {
  word-break: break-all;
}

and it should function as expected.

.tableTheme {
  background: #0d5dd4;
  color: white;
}

.tableWidth {
  max-width: 80%;
}

.tableWidth td,
th {
  width: 240px;
}
td {
  word-break: break-all;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
  integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row justify-content-center">
  <div class="tableWidth">
    <table class="table table-bordered">
      <thead class="tableTheme">
        <tr>
          <th scope="col">Test Suite Collection ID</th>
          <th scope="col">Test Suite Collection Name</th>
          <th scope="col">Test Suite ID</th>
          <th scope="col">Test Suite Name</th>
          <th scope="col">Test Case ID</th>
          <th scope="col">Test Case Name</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>TSC</td>
          <td>2</td>
          <td>TS</td>
          <td>3</td>
          <td>TC</td>
        </tr>
        <tr>
          <td>1</td>
          <td>
            WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE
          </td>
          <td>2</td>
          <td>TS</td>
          <td>3</td>
          <td>TC</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

Answer №2

Typically, long strings have spaces between the words, causing them to break naturally. However, if there are no spaces present, you can use word-break: break-all; to achieve the desired effect:

.tableTheme {
    background: #0d5dd4;
    color: white;
}

.tableWidth {
    max-width: 80%;
}

.tableWidth td,
th {
    width: 240px;
}
td {
  word-break: break-all;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row justify-content-center">
    <div class="tableWidth">
        <table class="table table-bordered">
            <thead class="tableTheme">
                <tr>
                    <th scope="col">Test Suite Collection ID</th>
                    <th scope="col">Test Suite Collection Name</th>
                    <th scope="col">Test Suite ID</th>
                    <th scope="col">Test Suite Name</th>
                    <th scope="col">Test Case ID</th>
                    <th scope="col">Test Case Name</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>TSC</td>
                    <td>2</td>
                    <td>TS</td>
                    <td>3</td>
                    <td>TC</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE_WHEN_I_HAVE_A_VERY_LONG_STRING_IT_DOESN'T_FIT_ONE_WINDW_ANYMORE</td>
                    <td>2</td>
                    <td>TS</td>
                    <td>3</td>
                    <td>TC</td>
                </tr>
            </tbody>
        </table>
    </div>
</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

Problem occurring with Bulma CDN version 0.8.0 failing to identify the spacing helper

Recently revisited an older project I had started some time ago using the Bulma framework. However, for some reason, the 0.8.0 CDN is not being imported or recognized correctly by the spacing classes. Here's the code snippet I had initially: <link ...

How can we stop every tab pane in (bootstrap five) from sharing the same scroll position? Each tab is currently synchronized with the scroll position of the others

In Bootstrap 5, the scroll position remains the same when switching between tabs in the tab pane. Whether moving from tab #1 to tab #2, both tabs are at the identical scroll position, failing to preserve each tab's specific location. <div class=&qu ...

When using jQuery, you can utilize the mouse over event to display elements with the same class within an each loop

I am struggling with a hover effect on my webpage. I have three visible elements and three hidden elements, and I want each visible element to display only one hidden element when hovered over. However, the issue is that currently, hovering over a visible ...

Troubleshooting problem with Angular Materials' md-datepicker not displaying correctly in wide browser windows

While using the md-datepicker component in my application, I noticed that it does not display properly on larger browser widths. It only seems to work as expected on small and x-small viewports. Please refer to the attached screenshot for reference. This ...

Experimenting with combining a CSS class alongside the Nth-child selector

Hello, I've been researching various sources including Stackoverflow on how to effectively use an Nth child selector and a Class together but have not had much success so far. In essence, my menu consists of Main categories (class = cat) and subcateg ...

Dynamic React animation with sliding elements

When jumping between the second and third "page" of content, I am unable to determine the reason why it is happening. The content is displayed as an absolute position. Check out the sandbox here: https://codesandbox.io/s/nostalgic-bhabha-d1dloy?file=/src ...

Using a custom font in a Django project without relying on the Google Fonts API

I've hit a roadblock in my programming journey and I'm seeking help. As a newcomer to both programming and Django, I've been following a helpful tutorial up until part 4. Everything was going well until I stumbled upon a new font online cal ...

Media queries in CSS not functioning as intended

Trying to scale media requests with an image (logo) for various mobile devices like iPhone 5, 6, 6+, iPads, and large screens. Need to specify styles for portrait and landscape orientation as well. The code seems to be functioning only for iPhone 6, iPads ...

Utilizing Flask to gather information from a leaflet map

I am currently working on creating a webpage using Flask. The webpage features a leaflet map where users can click to create a marker that opens a popup window with a link. The link's purpose is to open a new page displaying the longitude and latitude ...

Attempting to align two distinct divisions next to each other

Here is the updated code snippet: <Grid container justify="space-between" alignItems="flex-start" direction="column" xs spacing={5} style={{ padding: 10, }}> <ParamSelection/> { state.select ...

jQuery Alert for Double-Checking Email Entry

Is it possible to create a simple alert pop-up using jQuery if the user does not enter the correct email address in the second email input form? Below is my current code: <div id="reservation-request"> <form method="post" action="test.php ...

Customize Your AJAX POST URL with Radio Buttons - A Step-by-Step Guide

I'm looking for some guidance on how to use AJAX to change the post URL based on a radio button selection. Do I need to use an if statement or is there another way? var barray = []; function cbutton() { $('input:radio[name="cheking"]:checke ...

Why dashes are incompatible with inner <span> elements?

Is there a way to make hyphens work on text with <span> elements for highlighting without breaking the algorithm? I don't want a workaround like &shy;. The Code (sandbox: https://codepen.io/anon/pen/ayzxpM): .limit { max-width: 50px; ...

Node replication including a drop-down menu

Is there a way to clone a dropdown menu and text box with their values, then append them to the next line when clicking a button? Check out my HTML code snippet: <div class="container"> <div class="mynode"> <span class=& ...

.htaccess rule cannot process dots in the file names

I've encountered an issue with my htaccess rules. Here is the rule I have in my htaccess file: RewriteEngine On RewriteRule ^u/([a-z-0-9-_]+)$ user.php?id=$1 Everything works smoothly, except when I include a dot (.) in the username. The browser the ...

Unforeseen Issues Arising from Media Queries

Trying out some new styling for different screen sizes based on the bootstrap grid system dimensions. Facing an issue where only certain styles apply while others don't seem to take effect. Here's a snippet of the CSS: @media only screen and ( ...

Some websites are not displaying the CSS code for the inspector

In my experience, I have always relied on Firefox Inspector to troubleshoot CSS issues without any problems. However, when I attempted to analyze the CSS of a specific webpage (only encountering difficulties while logged in), I noticed that the inspector ...

Incorporate spacing between columns in Bootstrap 5 by adding gutters

I've been exploring ways to incorporate spacing between my columns in Bootstrap 5. I diligently followed the guidelines provided in the documentation, but I'm facing difficulties in adding gutters between my columns. Currently, I am utilizing g- ...

Ways to obtain jquery object for replaced DOM element

Is there a way to select the new content after using the replaceWith function in my plugin? I want to chain my plugin for the new content. $.fn.customPlugin = function() { this.replaceWith('<div>New Content</div>'); }; So, ideal ...

Ways to initiate JavaScript event upon clearing input form field

I'm working on creating a dynamic search feature. As the user types in the search box, JavaScript is triggered to hide the blog posts (#home) and display search results instead (the specific script for this is not shown below). However, when the user ...