Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-ui: write text in exactly 2 lines with ellipsis

For a reactjs app, I use Material-ui (https://material-ui.com/) for the design. And I need to write text in card that match exactly 2 lines.

What I've already done works when text can be contained in 2 or 1 lines, but for longer text, the cut becomes before word and it is not ellipsis.

enter image description here

Here what I've done

   <Box
        fontSize="h5.fontSize"
        component="div"
        overflow="hidden"
        textOverflow="ellipsis"
        height={70}
      >
        {title}
      </Box>

The result is good on the first card, for on the second one, the last word 'Floooooo' is displayed in the hidden part (the third line) but not in the second line with ellipsis. I've tried to add whiteSpace="nowrap" but the text is only 1 line height (fig 2) enter image description here

Can you help me please?

like image 850
Turvy Avatar asked Oct 12 '20 09:10

Turvy


People also ask

How do I make two lines of text in CSS?

We use the word–break property in CSS that is used to specify how a word should be broken or split when reaching the end of a line. The word–wrap property is used to split/break long words and wrap them into the next line.

How do you put two lines of text in HTML?

To create line breaks in HTML, use the <br> tag. There is no closing tag necessary. In the code above, there will be a line break between "125 N 6th St" and "Brooklyn, NY 11249" that won't have the outrageous amount of space that appears between two paragraph elements. It'll just be a nice line break!

How do you break a line in typography material UI?

To line break with Typography, set the display prop to block, which will break the line before the text inside.


Video Answer


3 Answers

You can use CSS rule -webkit-line-clamp: 2 in conjuction with word-break: break-all

const useStyles = makeStyles({
  root: {
    maxWidth: 300,
  },
  media: {
    height: 100,
  },
  customBox: {
    display: "-webkit-box",
    boxOrient: "vertical",
    lineClamp: 2,
    wordBreak: "break-all",
    overflow: "hidden"
  }
});

function App() {

  const classes = useStyles();
  const title = "pos2 test long Flooooooooooooooooooooooooooooooooooo";

  return (
    <Card className={classes.root}>
      <CardMedia
        className={classes.media}
        image="https://via.placeholder.com/300x100"
      />
      <CardContent>
        <Box
          fontSize="h5.fontSize"
          component="div"
          classes={{root: classes.customBox}}
        >
          {title}
        </Box>
      </CardContent>
    </Card>
  );
}

ReactDOM.render(<App/>, document.getElementById("root"));
<body>
  <div id="root"></div>

  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  <script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.development.js"></script>

  <script type="text/babel">
    const { Box, Card, CardContent, CardMedia, makeStyles } = MaterialUI;
  </script>
</body>
like image 64
95faf8e76605e973 Avatar answered Oct 23 '22 14:10

95faf8e76605e973


<Typography
  sx={{
    overflow: "hidden",
    textOverflow: "ellipsis",
    display: "-webkit-box",
    WebkitLineClamp: "2",
    WebkitBoxOrient: "vertical",
  }}>
  overflowing text...
</Typography>

Note

'sx' is a property that works in newer version of MUI

like image 32
Chukwuemeka Maduekwe Avatar answered Oct 23 '22 15:10

Chukwuemeka Maduekwe


Try like this:

<CardContent style={{width: "auto"}}>
          <Box
            fontSize="h5.fontSize"
            component="div" 
            overflow="hidden"            
            whiteSpace="pre-line"
            textOverflow="ellipsis"
            height={70}          
          >
            {title}
          </Box>
</CardContent>
like image 44
Rohitha Avatar answered Oct 23 '22 14:10

Rohitha