Tips for showing text abbreviation '...' when exceeding a specific length using CSS (with variable length)

I have successfully implemented a CSS solution to display text abbreviations '...' when the text exceeds a certain length, specified in pixels. The CSS code snippet used is as follows:

  display:block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100px; //width with fixed length (px..etc)

However, I am now faced with the challenge of making this approach responsive when the width is set in percentage units instead of pixels. I aim to create a flexible and responsive table that can adapt well to varying browser window sizes.

<table id="crl-table-2">
    <col style="width : 25%" >
    <col style="width : 25%" >
    <col style="width : 25%" >
    <col style="width : 25%" >
    
    <thead>
        <th class="align-left">Head1</th>
        <th class="align-left">Head2</th>
        <th>Head3</th>
        <th>Head4</th>
    </thead>
    <tbody>
        <tr style="display : block;">
            <td class="align-left one-line-text">Data1</td>
            <td class="align-left one-line-text">Data2</td>
            <td class="one-line-text">Data3</td>
            <td class="one-line-text">Data4</td>
        </tr>
    </tbody>
</table>

The HTML structure requiring this functionality has been illustrated above.

The CSS classes used for styling are defined as follows:

.align-left {
    text-align: left;
}

.one-line-text {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

In order to optimize the layout, display: block should not be applied to the one-line-text class, as it causes each td element to be displayed as a block. I'm also seeking guidance on where the display: block property should be specifically applied in this scenario.

Answer №1

To achieve the desired layout, simply set the max-width to a specific value like 100px, no need for display: block;.

If you want your table to expand to fill the entire screen width, use width: 100%.

#crl-table-2 {  /* UPDATE THIS SECTION */
  width: 100%;
}

.align-left {
  text-align: left;
}

.one-line-text {
  max-width: 100px; /* MODIFY HERE */
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<!DOCTYPE html>
<html>

<body>

  <table id="crl-table-2">
    <colgroup>
      <col style="width: 25%">
      <col style="width: 25%">
      <col style="width: 25%">
      <col style="width: 25%">
    </colgroup>

    <thead>
      <th class="align-left">Head1</th>
      <th class="align-left">Head2</th>
      <th>Head3</th>
      <th>Head4</th>
    </thead>
    <tbody>
      <tr>
        <td class="align-left one-line-text">Data111111111111111111111111111111111</td>
        <td class="align-left one-line-text">Data2</td>
        <td class="one-line-text">Data3</td>
        <td class="one-line-text">Data4</td>
      </tr>
    </tbody>
  </table>

</body>

</html>

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

FontAwesome symbols are not displaying on any chromium-based web browsers

I have been updating a theme from fontawesome 4 to 5, and everything seems to be working fine except for one specific icon that is displayed as a css pseudo-element. Even though I followed the guidelines in the FA docs, the icon still does not appear. It ...

What is the best way to center align my webpage horizontally for both mobile and desktop display?

I am having an issue with the alignment of the section on my website. How can I center align it horizontally for both mobile and desktop views? I attempted to use the center-align block elements by setting the left and right margins to auto, but that did n ...

First child CSS selector, excluding siblings

Is there a way to target only the initial occurrence of an element without affecting any nested elements? I specifically want to single out the first ul element within the #menu container, without selecting any children or descendants. For instance, using ...

How to centrally align header elements in Bootstrap 4 on mobile devices?

In this setup using Bootstrap 4, I have a header structure as follows: <div class="container"> <div class="row"> <div class="col-md-6"> <div class="navbar-brand"><img src="..."></div> </div> <div class="col-m ...

Error: The selectBox function is not defined for $(...)

Currently, I am encountering an issue with the code snippet below when attempting to select options from a dropdown: Uncaught TypeError: $(...).selectBox is not a function. This error message appears in the console. In order to resolve this problem, I ha ...

Retrieve the id of the anchor tag that was selected upon clicking, and then dynamically change the content of another div

Seeking guidance on how to dynamically change the content of a div element based on which anchor tag is clicked. Below is the HTML structure and JavaScript function I have attempted: HTML: <div> <a id="a1" href="javascript: changeDiv();">tag1 ...

How to access a shadowroot element using R Selenium

Currently, I'm dealing with web scraping where one of the websites presents a 'Accept cookies' button within a shadow root. To address this issue, I sought assistance on how to click this button in discussions found here: Click on accept co ...

The Iframe is malfunctioning and not displaying properly

Thank you for the insights on header issues. I have a follow-up question: Is it possible to identify header problems that prevent me from displaying content in an iframe just by looking at the link? If so, can I display a different page for those problemat ...

What is the best way to decrease the font size of every element in the DOM?

Is there a way to decrease the size of all DOM elements by a specific amount? Currently, I am using Bootstrap styles where h5 is set at 14px and I need it to be 12px, and h1 is at 36px when I want it to be 34px, and so forth. I have considered two option ...

"Learn the step-by-step process of retrieving the height and width of a user's

Functional Fiddle: http://jsfiddle.net/nbv8mb4a/ Tested Fiddle: http://jsfiddle.net/3m0zekyj/ The working Fiddle I created allows users to preview an image before uploading it. My next goal is to determine the height and width of the image in order to sc ...

The height auto transition in CSS3 begins by shrinking to a height of 0 before expanding to

My CSS code looks like this: .foo { height:100px; overflow:hidden; -webkit-transition: height 200ms; -moz-transition: height 200ms; -o-transition: height 200ms; transition: height 200ms; } .foo.open { height:auto; } When the ...

Is it considered the most appropriate method to implement a dynamic web page refresh by utilizing PHP, HTML, and AJAX in order to read a

My PHP page currently displays data from a database in tables, but the information is frequently changing. This means that the entire page has to be reloaded every few seconds. I am looking for a way to query the database and update only the section of HT ...

React application: Issue with second container not adjusting to the full height of the first container

My application consists of two containers. One container is a ReactPrismEditor, while the other is simply a container displaying text. However, I am facing an issue where the height of the second container does not adjust according to the content inside t ...

How can you trigger multiple jQuery Ajax calls simultaneously from various events?

Currently, I am facing a challenge with the development of a webpage that relies on multiple Ajax calls using jQuery to populate its contents. The issue lies in the fact that each call is waiting for the previous one to finish, causing slow loading times. ...

Improving loading time for pages requiring jQuery to render

Currently, I am utilizing the Google PageSpeed service to enhance the loading time of my website. It is hosted on GAE/P, but that detail may not be crucial in this context. There are several resources my page relies on: Bootstrap (css and js) jQuery (js ...

Is there a way to use JQuery to make one button trigger distinct actions in two different divs?

For instance: Press a button - one div flies off the screen while another div flies in. Press the button again - Same div flies out, other div returns. I'm completely new to Javascript/JQuery so any assistance would be highly appreciated! Thank you ...

Using jQuery in Javascript, obtain the chosen radio option and place it within a label tag

I have a radio button for gender selection: <div class="label-inputs" name="userFieldDiv" id="genderUserFieldDiv"> <label class="required">Gender</label> <input type ...

Unable to trigger onClick event

The button I have with the id "jump-button" is not functioning at all. When clicked, nothing happens. I have added an alert(); to the onclick attribute to test if the button is working. However, the other two buttons (next and prev) are working perfectly ...

Mobile view not displaying navbar menus

My website is responsive and functions properly on desktop, but I'm encountering issues with the menus in the navbar not displaying correctly on mobile and tablet views. To see a screenshot of the problem, you can click on this link: You can visit m ...

Modify PHP script to extract the Document Object Model (DOM) from a specified URL

Hey there, I'm currently using this code to extract and display the HREFs of all "A" tags from a URL. However, my output includes the entire link when I only want the name portion after http://twitter.com/namehere So, I'm looking to clean up my ...