What are some ways to restrict the number of words per line?

I'm currently developing an online store using NextJS and Material UI.
The issue I encountered is related to the long product names that are causing my Card component to stretch. As shown in this image:
https://i.sstatic.net/07qNm.png

If you observe closely, you'll notice that the prices of some products are not aligned properly.

Here's a snippet of code from my index.js and data.js files where I manage the product data:

index.js

<Grid container spacing={3}>
                  {data.products.map((product) => (
                    <Grid item md={2} key={product.name}>
                      <Card style={{ border: 'none', boxShadow: 'none' }}>
                        <NextLink href={`/product/${product.slug}`} passHref>
                          <CardActionArea>
                            <CardMedia
                              component="img"
                              image={product.image}
                              title={product.name}
                            ></CardMedia>
                            <CardContent>
                              <Typography>{product.name}</Typography>  ///// NAME IS HERE
                            </CardContent>
                          </CardActionArea>
                        </NextLink>

                        <CardActions className={classes.cardActionsProducts}>
                          <div style={{ textAlign: 'center' }}>
                            <Typography
                              style={{
                                fontSize: '12px',
                                color: 'black',
                                fontWeight: 400,
                                marginTop: '10px',
                              }}
                            >
                              {product.numReviews} reviews | sold {product.sold}
                            </Typography>
                          </div>
                        </CardActions>
                        <CardActions className={classes.cardActionsProducts}>
                          <Typography
                            style={{
                              fontWeight: 'bold',
                              color: 'red',
                              margin: '0px',
                            }}
                          >
                            {product.price}đ  
                          </Typography>
                        </CardActions>
                      </Card>
                    </Grid>
                  ))}
                </Grid>

data.js

var l = 9000;
console.log(a.toLocaleString());
const data = {
products: [
{
      name: 'Ridiculously long name that cannot fit and needs to be shorten',
      slug: 'ridiculously-long-name-that-cannot-fit-and-needs-to-be-shorten',
      category: 'stuff',
      image: '/product-images/example.webp',
      price: l.toLocaleString(),
      shop: 'VSeat',
      rating: 5,
      numReviews: 101,
      sold: 1000,
    },
    {
      name: 'Another very very very long name that I need to shorten',
      slug: 'another-very-very-very-long-name-that-I-need-to-shorten',
      category: 'stuff',
      image: '/product-images/example.webp',
      price: l.toLocaleString(),
      shop: 'VSeat',
      rating: 5,
      numReviews: 101,
      sold: 1000,
    },
]
}
export default data;

I'm seeking assistance on how to truncate long product names to display only 2 lines and replace the rest with ellipsis (...)

For instance:
A very very very long name
Should appear as:
A very very
very long...

This issue has been bothering me for quite some time now, so any insights would be highly appreciated. Thank you

Edit: I believe I've made progress towards achieving my goal but encountered another hurdle along the way.
Recently, I stumbled upon a detailed explanation on limiting text to 2 lines only in this answered question. However, I have encountered a problem.
This is how it was mentioned in the provided link:

overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;

And here is how I implemented it in my styles.js file:

twoLines: {
    whiteSpace: 'preline',
    width: '400px',
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    display: '-webkit-box',
    WebkitLineClamp: '2',
    WebkitBoxOrient: 'vertical',
    border: '1px solid #FFFFFF',
  },

Unfortunately, it seems that Webkit box, WebkitLineClamp, and WebkitBoxOrient are not functioning as expected. Is there a mistake in how I wrote them or any other reason for their malfunctioning?
It's slightly embarrassing to get stuck on such a minor detail, but any guidance would be greatly appreciated.

Answer №1

It appears that there is a CSS issue related to Typography, specifically with the Product name. The solution involves removing the hyphens from 'WebKit-box' and instead using 'webkitbox'.

Index.js

<Typography
                              style={{
                                fontSize: '12px',
                                color: 'black',
                                fontWeight: 400,
                                marginTop: '10px',
                                text-overflow: 'ellipsis',

                              }}
                            >

Here's a simple example to illustrate this problem:

div.b {
  white-space: nowrap; 
  width: 50px; 
  overflow: hidden;
  text-overflow: ellipsis; 
  border: 1px solid #000000;
  background-color: pink;
}
<h1>text-overflow Property</h1>

<h2>text-overflow: ellipsis:</h2>
<div class="b">Hello world!</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

Tips for hiding the initial value in an HTML Select tag

My HTML Select Program is very basic. By default, it displays "Volvo" as the first value. Is there a way to not display any initial value and allow the user to make a selection? <!DOCTYPE html> <html> <body> <form action="demo_form ...

Is it possible to execute a function upon exiting my Node application?

Currently, I'm developing a project for my school using Node.js on a Raspberry Pi. The script I have created will be running for extended periods of time to control LEDs. Is there a method that allows me to execute a function upon exiting the program ...

Layering digital sheets of paper and rearranging them with the help of CSS

I want to create a visual representation of a stack of paper sheets as a metaphor. The idea is to neatly stack the sheets on top of each other, with only the header of each sheet visible and the rest of the content covered. !-------------- | Sheet 1 +--- ...

Accessing a key from an object dynamically in a React list and resolving key error issues in React

I encountered two challenges: I am currently retrieving JSON data from APIs. [ { "title": "Basic Structures & Algoritums", "lesson_id": 3, "topics": { "Title": &q ...

Retrieve the value of a related object based on the user's click

Check out this sample code on JSFiddle <a href="#"></a> <a href="#"></a> <a href="#"></a> <a href="#"></a> <div></div> JavaScript: function Product(name, price){ this.name = name; this. ...

Mentioning a component that is loaded dynamically

Here is the code snippet: <html> <head> <script type="text/javascript" src="jquery.js"></script> </head> <body> <div id="Block"></div> <script type="text/javascript"> function Request(M ...

Is there a solution or workaround for the issue of fake anti-aliasing in Microsoft Edge when it comes to the border of sticky headers using Bootstrap 4?

In my current project, I came across a Pen shared by Balaji731 on Bootstrap 4 table with the scrollable body and header fixed. While testing the Pen in Microsoft Edge, I noticed that when <th> borders are enabled, they disappear while scrolling, cre ...

Toggle the on and off functionality of a button using ajax response for content display

I'm struggling to figure out why the button isn't working for me. I am familiar with how to enable and disable buttons using attr and prop. $(document).on("click", "#btn", function(){ $(this).html("Sending..."); $(this).prop("disabl ...

What is the best way to display API response data in a tabular format using React.js?

My goal is to display the data from an API response in a table format. I've written some code using Axios for making the API request, but the response is not showing up in the table as expected and there are no errors displayed. I suspect that I may h ...

Choosing an option on a WordPress admin page may vary depending on the selection of another

I am facing a challenge with my code that involves two selections: category and type. The type selection should be dependent on the category selected from the database. Here is a snippet of my code: HTML $ps_type_table_name = $wpdb->prefix . 'ps_ ...

Incorporate JSON information into a sleek Bootstrap modal interface

I am looking to load a JSON file to generate a list within a Bootstrap Modal. The setup I have allows for the modal to appear when clicking on a person's image. <li class="project span3" data-type="pfa"> <a data-toggle="modal" data-targe ...

How to center an image within a viewport without worrying about the width of the viewport

I'm working on centering an image slideshow within the viewport, regardless of its width. It's easy to accomplish when the viewport is wider than the image by using margin: auto;. However, when the viewport is narrower than the image, such as on ...

Return a response from the controller method without using the express response object

When attempting to handle the scenario where a fetch for an item does not return anything from another method that lacks the express response, I encounter an issue. I invoke this method from another one that has the express response: const updateItem = asy ...

Using jQuery to create an array variable and Passing the array to a different PHP page

Although I have managed to create a function that works perfectly for me, I am facing an issue with the jQuery part at the bottom. Currently, I am only able to trigger an alert message and not store the data as a real variable. The function is functionin ...

Having trouble accessing the value of an <asp:HiddenField> using javascript

Currently, I am setting a value to a hidden field and attempting to retrieve that value in my JavaScript code. In the declarative part of my code: <asp:HiddenField ID="chkImages" runat="server" /> <div id="main" runat="server" style="display:non ...

Is there a way to generate random numbers that will create a chart with a general upward trend?

Are you looking for a solution to generate random numbers for charts that trend upwards and to the right? I am currently utilizing a JavaScript charting engine, so I will require numbers in JSON format eventually. However, I am open to alternative methods ...

Guide to inserting an index on Vue list templates in HTML

Can you review this code snippet below? It's embedded directly in an html page, no isolated components here. This code is designed for a Vue.js application. <div v-for="(screw, index) in form.screws " class="screw-module"> <input type ...

What causes the source code of a website to change when accessed from various browsers?

When analyzing the source code of bartzmall.pk using various browsers, it reveals different classes being added to the html tag for each browser. For Firefox: <html class="firefox firefox53 otherClasses"> For Chrome: <html class="webkit chrome ...

A method to implement an If Loop on a cube using Threejs in order to generate an interactive on-hover feature that enlarges the cube's size

Having just completed my first tutorial in threejs as a novice, I am now facing a challenge. I am trying to create a hover effect on a basic cube shape where it grows in size when the mouse pointer hovers over it and shrinks back to its original size when ...

What is the reason for webpack adding "-es5" and "es2015" to my TypeScript files?

When Webpack converts my typescript files into Javascript files, it appends -es5 to the name. For instance, I expect the file to be named background.js, but it actually generates 4 separate files: background-es5.js background-es5.js.map background-es2015. ...