Material-UI : Long Text Wrapped in Grid
Asked Answered
I

3

7

In the docs of Material-UI, in the section Grid: white-space: nowrap; there is an exemple of the text wrapped in codesandbox.

In this exemple I replace const message ="" by a long text without space:

const message ="AsuperLongTextWithNoSpaceItIsVeryAnnoyingIWantToWrapThisSentence"

The result:

enter image description here

As you can see the message exceeds the grid. I want that the message wraps the line.

I tried to add style={{'overflowWrap': 'break-word'}} like this:

<Paper className={classes.paper}>
  <Grid container wrap="nowrap" spacing={2}>
    <Grid item>
      <Avatar>W</Avatar>
    </Grid>
    <Grid item xs>
      <Typography style={{'overflowWrap': 'break-word'}}>{message}</Typography> {/* style added */}
    </Grid>
  </Grid>
</Paper>

The result:

enter image description here

As you can see it's better, but the text exceeds the grid too.

How to do this correctly without going over the grid?

EDIT (1)

I have a grid in a grid:

<Grid container spacing={2}>
    <Grid item>
        <Avatar>
            <ImageIcon />
        </Avatar>
    </Grid>
    <Grid item xs={12} sm container >
        <Grid item xs container direction="column" spacing={2} wrap="nowrap">
            <Grid item xs>
                <Typography gutterBottom variant="subtitle1">
                    {`${props.comment.username}`}
                </Typography>
            </Grid>
            <Grid item xs zeroMinWidth>
                <Typography gutterBottom variant="subtitle1" style={{'overflowWrap': 'break-word'}}>
                    {props.comment.comment}
                </Typography>
            </Grid>
            <Grid item container>
                <Grid item>
                        <IconButton onClick={handleMenuClick}>
                            <ThumbUp />
                        </IconButton>
                </Grid>
            </Grid>
        </Grid>
    </Grid>
    <Grid item>
        <IconButton onClick={handleMenuClick}>
            <MoreVertIcon />
        </IconButton>
    </Grid>
</Grid>

My container has wrap="nowrap", my item has zeroMinWidth, but the result is:

enter image description here

Instead of this:

enter image description here

EDIT (2)

I found the solution:

It must to write zeroMinWidth for each <Grid item> !

Ingvar answered 30/4, 2020 at 14:31 Comment(0)
N
13

This works (note zeroMinWidth):

      <Paper className={classes.paper}>
        <Grid container wrap="nowrap" spacing={2}>
          <Grid item>
            <Avatar>W</Avatar>
          </Grid>
          <Grid item xs zeroMinWidth>
            <Typography style={{overflowWrap: 'break-word'}}>{message}</Typography>
          </Grid>
        </Grid>
      </Paper>

Long text not overflowing container

Norland answered 30/4, 2020 at 14:44 Comment(4)
Yes, but I want to display all the text, I want a break lineIngvar
Answer updated, just add overflowWrap and keep zeroMinWidth.Norland
Thank you, It works, but I want to put this grid in the other grid, I edited my question, could you help me ?Ingvar
Can you create a codesandbox for this? It's hard to figure these things out without looking/manipulating the dom.Norland
E
0

My problem was more with maxWidth on small screens. Solved with [theme.breakpoints.down('md')]: { maxWidth: 250 }.

Elude answered 4/4, 2021 at 9:17 Comment(0)
W
0

Found this unswer in a gitgub thread. Helped for me. Enjoy!

enter image description here

Waadt answered 19/7, 2024 at 5:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.