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

Why does the for loop function correctly with console.log but not with innerHTML?

Hello! I'm completely new to JavaScript, so please excuse any oversight... Below is the code that runs when a button on the page is clicked: function getNumbers() { var firstValue = document.getElementById("time_one").value; var ...

The active class consistently adds new menu options without removing the previous ones

I am facing an issue with my menu where the active class does not get added to the new menu item when clicked, despite being removed from the previous item. Below is the code snippet: <div class="row text-center" id="side-menu"> <nav> ...

Styling a webpage with a two-row layout using just CSS

I am looking to design a 2-row layout for a webpage where one row will contain filters and the other row will display results. The layout should meet the following criteria: The page height should not exceed 100% The first row's height will vary bas ...

Which design pattern should I implement to update all table rows except the initial one, while incorporating an AJAX insertion mode?

I am working with a table structure that is dynamic based on search results. The table consists of different rows including titles for categories like Organization, Category, and File. <table class="table-striped col-lg-12" id="results"> <tr& ...

JavaScript code for opening and closing buttons

I created a button that successfully opens, but I am struggling to figure out how to close it. Can someone assist me with this? Additionally, I have one more query: How can I remove the border when the button is clicked? document.getElementById("arrowb ...

Using a declared const in a separate React file

I've been searching for a solution, but haven't found anything that helps. I'm trying to retrieve data from an API and pass it to another page. The information is currently defined as "drink.strDrink", including the name and image. However ...

Break down and simplify the code into individual components

<input type='button' class="myButton" onclick="document.body.style.cssText+=';background-image: url(img01.jpg);background-repeat: no-repeat; -moz-background-size: cover; -o-background-size: cover; background-size: cover;';" value="S ...

What is the method for retrieving the value of a checkbox when clicked?

Managing a dynamically created HTML table with checkbox values can be tricky, especially when the value needs to reflect changes in real-time. If you are struggling to update the status of the checkboxes based on database values and pass that information t ...

Creating a Delicious Earth Progress Pie

I'm looking to incorporate a unique progress bar design on my website, similar to this one: The progress pie Can anyone guide me on how to create a progress pie with an image inside it that changes color as it moves? ...

Please provide the text enclosed within the h1 tags

Is there a way to extract the content between <h2> </h2> tags in PHP or jQuery from each page and use it as the title of the pages to ensure uniqueness? Example: <section class="sub_header"> <h2>Contact</h2> < ...

What is the best way to utilize AJAX to send a value from a foreach loop in Laravel?

I'm encountering an issue where all forms are sending the value as if it's from the first form, however, my intention is for each form to send the data inside it when I press the send button. Blade @foreach($car_lists as $car_list) <li class= ...

Discovering the positions of HTML elements rendered with WebKit (or Gecko) technology

I am seeking a way to retrieve the dimensions (coordinates) of all HTML elements on a webpage as they appear in a browser, including their rendered positions. Specifically, I need to obtain values like (top-left, top-right, bottom-left, bottom-right) Afte ...

Gaps separating frames

<!Doctype html> <html> <frameset rows="26%,24%,*" noresize border="0" frameborder="no" framespacing="0"> <frame src="frame_a.html" target="_self" name="logo" scrolling="auto"> <frame src="frame_b.html" target="_self" name="menu" ...

Expanding Images with JQuery Accordion

I'm facing a problem where I need to include accordions on my website with images, but whenever the accordion is opened, the image ends up stretched. Is there a solution to prevent this from happening? Below is the code snippet I am using: [Fiddle] ...

Issue with :hover pseudo-class in IE8

If you're unsure about my explanation, check out the video for a visual demonstration. Pay close attention to the cursor movements while I attempt to optimize my website for IE8. Hopefully there is a solution to this issue. Thank you in advance! http ...

Looking to reposition the control buttons above the slider in easySlider 1.7?

I am thoroughly enjoying the easySlider 1.7 and found it incredibly simple to implement. The only modification I wish to make is to relocate the next and previous buttons above the slider instead of below it. Does anyone have any suggestions on how to achi ...

What is the preferred approach in JavaScript: having a single large file or multiple smaller files?

Having a multitude of JavaScript files loaded on a single page can result in decreased performance. My inquiry is this: Is it preferable to have individual files or combine them into one JavaScript file? If consolidating all scripts into one file is the ...

Ways to conceal a grid item in Material UI framework

My goal is to hide a specific grid item for a product and smoothly slide the others in its place. Currently, I am using the display:none property but it hides the item instantly. I have already filtered the products and now I want to animate the hiding of ...

The error message encountered is "Uncaught (in promise) Error: Unable to access attributes of an undefined object (reading 'launch')."

I am currently learning electron.js by developing a basic application that extracts information from a website. However, I am encountering a frustrating and annoying error. Here is the folder structure of my project The following code snippet represents ...

Getting the css property scaleX with JQuery is as simple as executing the

I am struggling to print out the properties of a specific div element using jQuery. My goal is to have the different css properties displayed in an information panel on the screen. Currently, I am facing difficulties trying to show scaleX. Here is my curr ...