How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include:

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

However, the current implementation causes the cell to consume all available space:

<!DOCTYPE html>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53355c5d4723677d2b">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d4a0a3b3a2bfd2ac85e7fbad">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  <style>
.truncate {
  display: inline-block;
  /* width: 200px; */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
  </style>
</head>
<body>
  <div id="app">
<v-app>
  <v-content>
    <v-data-table :headers="headers" :items="items">
      <template v-slot:item.name="{ item }">
        <span class="truncate">{{ item.name}}</span>
      </template>
    </v-data-table>
  </v-content>
</v-app>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8afcffefcab8a4f2">[email protected]</a>/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f98f8c9c8d909f80b9cbd781">[email protected]</a>/dist/vuetify.js"></script>
  <script>
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: {
    headers: [
      { text: 'Name', value: 'name', width: "75%" },
      { text: 'Calories', value: 'calories', width: "25%" },
    ],
    items: [
      { name: 'Frozen Yogurt', calories: 159, },
      { name: 'Ice cream sandwich with a really, really, really long name that keeps going on and on and on forever so there is no space left', calories: 237, },
      { name: 'Eclair', calories: 262, },
    ], }
})
  </script>
</body>
</html>

A fixed pixel width can achieve the desired effect, but I prefer the table and columns to be responsive.

Answer №1

When you implement the truncation on the td element instead and utilize the clever workaround of setting max-width: 1px, you will achieve your intended outcome.

In the scenario provided below, to apply the class to the td element, you must use the item slot and construct the row manually.

<!DOCTYPE html>
<html>

<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6861607a4e3a2076">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bfdfeeeffe2edf2cbb9a5f3">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  <style>
    .truncate {
      max-width: 1px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  </style>
</head>

<body>
  <div id="app">
    <v-app>
      <v-content>
        <v-data-table :headers="headers" :items="items">
          <template v-slot:item="{ item }">
            <tr>
              <td class="truncate">{{ item.name}}</td>
              <td>{{ item.calories}}</td>
            </tr>
          </template>
        </v-data-table>
      </v-content>
    </v-app>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4432312104766a3c">[email protected]</a>/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="512724342538372811637f29">[email protected]</a>/dist/vuetify.js"></script>
  <script>
    new Vue({
      el: '#app',
      vuetify: new Vuetify(),
      data: {
        headers: [{
            text: 'Name',
            value: 'name',
            width: "75%"
          },
          {
            text: 'Calories',
            value: 'calories',
            width: "25%"
          },
        ],
        items: [{
            name: 'Frozen Yogurt',
            calories: 159,
          },
          {
            name: 'Ice cream sandwich with a really, really, really long name that keeps going on and on and on forever so there is no space left',
            calories: 237,
          },
          {
            name: 'Eclair',
            calories: 262,
          },
        ],
      }
    })
  </script>
</body>

</html>

Answer №2

This solution proved effective for me when I adjusted the css max-width attribute to a responsive vw value:

    .truncate {
       max-width: 50vw;
       white-space: nowrap;
       overflow: hidden;
       text-overflow: ellipsis;
    }

Answer №3

To make your code function correctly, simply replace the SPAN element with a DIV and remove the comment tags around the width attribute in the CSS.

<!DOCTYPE html>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2848d8c96a2d6cc9a">[email protected]</a>/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d6b687869747b645d2f3365">[email protected]</a>/dist/vuetify.min.css" rel="stylesheet">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  <style>
.truncate {
  display: inline-block;
  width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
  </style>
</head>
<body>
  <div id="app">
<v-app>
  <v-content>
    <v-data-table :headers="headers" :items="items">
      <template v-slot:item.name="{ item }">
        <div class="truncate">{{ item.name}}</div>
      </template>
    </v-data-table>
  </v-content>
</v-app>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e3e0f0d5a7bbed">[email protected]</a>/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e6e5f5e4f9f6e9d0a2bee8">[email protected]</a>/dist/vuetify.js"></script>
  <script>
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: {
    headers: [
      { text: 'Name', value: 'name', width: "75%" },
      { text: 'Calories', value: 'calories', width: "25%" },
    ],
    items: [
      { name: 'Frozen Yogurt', calories: 159, },
      { name: 'Ice cream sandwich with a really, really, really long name that keeps going on and on and on forever so there is no space left', calories: 237, },
      { name: 'Eclair', calories: 262, },
    ], }
})
  </script>
</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

Learn how to implement a conditional class binding in Vue 3, and discover how to apply additional classes when a specific condition is met

As a newcomer to Vue.js 3, I am wondering how to implement a conditional class binding in Vue 3. Specifically, I would like to achieve the following logic: if router.path != '/', then add the class 'nav-bar-two'; otherwise, do not apply ...

Quasar Table fails to update (version 1)

The Quasar Table I am using has its data property connected to a Vuex state, which is an array of objects. However, when the state of an object (a row in the table) changes, this change is not reflected in the table itself. Even though the QTable Data prop ...

The combination of Webpack with vue-loader and postcss-modules leads to an absence of values in the $

Within my application, I am initializing a Vue app that utilizes single file .vue components. To bundle, I rely on Webpack and utilize vue-loader + postcss-modules to generate scoped classes. However, I am facing an issue where I cannot access the gener ...

Is there a way to adjust the placement of text in an HTML document?

Just starting out with web development and created some HTML code: <html> <body> {errors} <p>Input your numbers:</p> <form method="post" action="."> <p><inpu ...

What is the proper way to retrieve the Nuxt context within the fetch() hook?

Is there a way to access the props within an async fetch() function when also using async fetch(context)? I'm trying to figure out how to work with both simultaneously. ...

Utilizing data as a substitute when creating a SearchBar using Vue3

My VueJs3 application has a search bar implemented using .filter(), and it seems to be working fine. However, when I try to pass the value from my methods to the template, an error occurs. My data becomes a proxy and I am unable to use it in that format. ...

Is it impossible to effectively handle slot content in Vue 3?

Currently, I am in the process of transitioning a Vue 2 codebase to Vue 3. <card> <template #default> <card-header> <title level="3">Header</title> </card-header> <car ...

Various SVG paths make up segments

I have created an intricate SVG file containing 35 different paths resembling train tracks. My next step is to break down these paths into 16 segments, allowing another SVG path (train) to smoothly traverse along them. The ultimate goal is to grant users ...

Combining Vue components seamlessly with external HTML elements: A guide

Imagine working on a WordPress blog or a standard CMS that stores content using a wysiwyg editor like CKEditor. You might want to incorporate Vue components into your HTML, so you decide to add a wrapper div to your theme. Your HTML pages are enclosed by ...

What is the process for inputting data into a Inertia component?

I have a simple component: <script setup> import {ref} from 'vue' const labels = [] </script> <template> <div v-for="(label, index) in labels" :key="index" v-html="label"></div> </t ...

Adapting Vue.js directive based on viewport size - a guide

Having an element with the v-rellax directive, I'm utilizing it for prallax scrolling in this particular div: <div id="image" v-rellax="{ speed: -5 }"></div> Currently, there's a need to adjust the speed property ...

Ensuring the precise alignment of a singular component in Angular 8 while keeping the styles of its child elements unchanged

Is there a way to centralize div elements within a component without impacting the styles of those specific divs? How can I achieve this? I'm currently working with Angular 8. Using the text-align method affects the alignment of the text inside the d ...

Renaming file names for Bootstrap 4.5 demonstrations

Trying to utilize a Bootstrap 4.5 example, but running into issues when renaming the HTML or CSS files - it breaks the formatting link for the page. Is there a way to duplicate the original example files and modify the names (such as "my_page.html" or "m ...

Creating HTML code for a mobile responsive design

I am facing an issue with my clinic webpage where the images appear differently on PC and mobile devices. I am new to HTML and CSS, so I tried using max-width: 100% but it didn't work. The code was originally created by someone else and I need help fi ...

Utilize flexbox to make two elements on the same line

Is it feasible to have two elements on the same line when aligning a series of elements with flexbox? Consider this scenario: .outer { display: flex; flex-direction: column; margin-left: auto; margin-right: auto; width: 50px; } .outer .inner ...

Vue's bidirectional data binding presents a challenge when attempting to update the parent component from a child component

I have the following components: Parent Component: <template> <v-container> <v-row class="text-center"> <v-col cols="12" class="parent"> <p>I am the Parent component</p> ...

What's the strangest glitch in Chrome related to repainting?

I am currently facing an issue with a form that contains hidden errors. The CSS for this form, written in Stylus, is as follows: span.error display: none position: relative padding: .25em .5em width: 75% margin-top: 5px margin-bottom: 15px f ...

What exactly is the significance of "hash" in terms of Internet Explorer style

After downloading a sample CSS file, I noticed the following entry: #text-align: right; The comment beside the entry mentioned that this expression is specifically for justifying it in IE, while text-align: right; works for Safari and Chrome. My questi ...

Encountering problems with Nuxt/Vue hydration when utilizing a slotted component

Recently, in my new Nuxt project, I have been facing some peculiar hydration problems. Despite putting effort into isolating the issue and providing a demonstration (refer to the image), the hydration error seems to occur unpredictably. Is there anyone ou ...

occupying half of the screen

Having trouble displaying two videos in an ionic grid. My goal is to have both videos take up the entire screen, with each occupying 50% height and 100% width. However, when I try to achieve this, the ion-row only takes up 50% of the screen and the video i ...