I’m using ReactJS and the components library called MaterialUI. I’m having an issue with the Typography component.
What happens is that if I write a long text it exceed its container and doesn’t go on a new line:
JavaScript
x
25
25
1
import React from "react";
2
import { Redirect } from "react-router";
3
import { withRouter } from "react-router-dom";
4
5
import Container from "@material-ui/core/Container";
6
import CssBaseline from "@material-ui/core/CssBaseline";
7
import Typography from "@material-ui/core/Typography";
8
9
function Homepage() {
10
return (
11
<div>
12
<React.Fragment>
13
<CssBaseline />
14
<Container fixed>
15
<Typography variant="h1" component="h2" align="center">
16
123 456 789 qwertyuiopasdfghjklzxcvbnm
17
</Typography>
18
</Container>
19
</React.Fragment>
20
</div>
21
);
22
}
23
24
export default withRouter(Homepage);
25
below an image:
This happens in the mobile mode and also in desktop mode.
Do you know how to fix this behavior? I would like the long words will be split on a new line if the maximum width of the container has been reached.
Advertisement
Answer
Solution
Use word-wrap, it works for Material-UI’s Typography.
JavaScript
1
2
1
wordWrap: "break-word"
2
Ref: QA: wrap long string without any blank
Demo
JavaScript
1
9
1
<Typography
2
variant="h1"
3
component="h2"
4
align="center"
5
style={{ wordWrap: "break-word" }}
6
>
7
123 456 789 qwertyuiopasdfghjklzxcvbnmdfsafasfasfadfaf
8
</Typography>
9