Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  150] [ 5]  / answers: 1 / hits: 6321  / 4 Years ago, fri, may 8, 2020, 12:00:00

How can I properly add dots the the title in my Cardheader if it exceeds the parents width (Card width). So far I have done this:



  card: {
width: 275,
display: flex
},

overflowWithDots: {
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
overflow: 'hidden',
width: '100px'
}

<Card className={classes.card}>
<CardHeader
title={
<Typography gutterBottom variant=h6 component=h4 className={classes.overflowWithDots}>
{movie.title}
</Typography>
}
/>


This works in a way, but I have to tell the class to have a width of 100px until it adds the dots. I need to add the dots if it exceeds the parents width.


More From » css

 Answers
4

Problem



As I understand it you are limiting the size of the Card, in this case, you are not being able to place the ellipsis due to the way the CardHeader is rendered in the html.



The CardHeader component is rendered with a root element and a content element. See below:



CardHeader



Typography has a built-in prop to adding dots noWrap. For the noWrap property to work correctly, we have the following approaches.



Solution 1



CardHeader's default behavior is to use flex. If you don't need it use flex:



...

cardHeader: {
display: block,
overflow: hidden
}

...

<CardHeader
className={classes.cardHeader}
title={
<Typography noWrap gutterBottom variant=h6 component=h4>
A world wide web - the revolution
</Typography>
}
/>
...


Solution 2



If you need to keep CardHeader with flex behavior, in this case, the overflow needs to be applied to the root and content. To reach the elements use the CardHeader classes property passing the generated class to the content prop.




...

cardHeaderRoot: {
overflow: hidden
},
cardHeaderContent: {
overflow: hidden
}

...

<CardHeader
classes={{
root: classes.cardHeaderRoot,
content: classes.cardHeaderContent
}}
title={
<Typography noWrap gutterBottom variant=h6 component=h4>
A world wide web - the revolution
</Typography>
}
/>

...






Here's an example in the sandbox.



Edit






Atention



Be aware that by modifying the default behavior of how components are rendered, some side effects may occur in your entire component tree.



Anyway



If you still have any problems let us know.


[#3883] Tuesday, May 5, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
davion

Total Points: 458
Total Questions: 109
Total Answers: 100

Location: Taiwan
Member since Mon, Sep 6, 2021
3 Years ago
davion questions
;