How to Set a Readable Line Width for Article Text with CSS max-width
When the text of a blog post or document stretches from edge to edge on a wide desktop screen and becomes hard to read, add max-width and margin: 0 auto to the outer wrapper of the text. max-width sets an upper limit on the length of a line, and setting the left and right margin to auto divides the leftover width evenly between the two sides. On a narrow smartphone screen, the outer wrapper shrinks to the width of the screen.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. The font was Noto Sans CJK JP, and the text size was 16px. I opened a test page with the screen width set to 1280px and 375px, and measured the width and position of the outer wrapper with getBoundingClientRect(). I also counted the characters on the first line from their positions. I did not test Firefox, Safari, real smartphones, or vertical writing pages.
Add max-width and margin: 0 auto to the outer wrapper
Wrap the article text in an outer element such as article, and give that outer element the following two properties.
<main>
<article class="post">
<h1>記事の見出し</h1>
<p>本文の段落</p>
</article>
</main>
.post {
max-width: 40em;
margin: 0 auto;
}
When I measured at a screen width of 1280px, the outer wrapper was 640px wide (16px × 40), and the left and right margin were each 320px. The 640px left over from 1280px was split evenly between the two sides. The MDN page on margin also states that margin: 0 auto; can center an element horizontally within its parent.

Adding only margin: 0 auto without max-width does not center the text. The outer wrapper stays as wide as the screen, 1280px, and the left and right margin were 0px. Because the wrapper fills the whole width, there is no leftover space to split between the sides. To center it, the wrapper's width has to be set narrower than the screen.
Why use max-width instead of width
If you only want to set the width of the outer wrapper, width: 40em gives the same result as max-width: 40em at a screen width of 1280px. The difference appeared at a screen width of 375px.
| Outer wrapper setting | Outer wrapper width | Left margin |
Horizontal scroll |
|---|---|---|---|
width: 40em |
640px | 0px | Yes (page width 640px) |
max-width: 40em |
375px | 0px | No |
With width: 40em, the wrapper stays 640px even on a narrow screen. It overflowed the right edge of the screen by 265px, and the page became horizontally scrollable. With max-width: 40em, the outer wrapper shrinks to the width of the screen (375px). As described on the MDN page on max-width, max-width only ensures that the width does not exceed the specified value, so the wrapper also shrinks when the screen is narrow.
For Japanese body text, use 40em or 40ic to fit 40 characters per line
As a guideline for line length, the explanation of WCAG 2.2 Success Criterion 1.4.8 (Understanding SC 1.4.8: Visual Presentation) says that a line should be no longer than 80 characters, or 40 characters for Chinese, Japanese, and Korean (CJK) text. The reason given is that CJK characters are about twice as wide as other characters, so the same readability target means a shorter character count.
When I set the outer wrapper to max-width: 40em and put in a sentence made only of hiragana and katakana, the first line held 40 characters. On a 1280px screen with no width set, it held 80 characters, so each kana is laid out at roughly 16px (1em) wide.
If you write the line length in ch (for example max-width: 65ch) for Japanese body text, the number of characters per line becomes less than 40. As described on the MDN page on length units, ch is the width of the digit "0". Measured at 16px, 1ch was about 8.88px in Noto Sans CJK JP and about 10.18px in DejaVu Sans. With max-width: 65ch in Noto Sans CJK JP, the outer wrapper was about 577px wide, and the first line held 36 characters.
If you want to count Japanese characters, there is also the ic unit. ic is the width of the CJK ideograph meaning "water" (CSS Values and Units specification). When I measured with Noto Sans CJK JP and Noto Serif CJK JP, 1ic was 16px in both, the same as 1em. An outer wrapper with max-width: 40ic was 640px, the same as 40em, and its first line held 40 characters.
Widths set in em and ic change with the text size. When I changed the body text from 16px to 20px, the outer wrappers with max-width: 40em and max-width: 40ic both grew to about 800px, and the first line still held 40 characters. An outer wrapper with max-width: 640px stayed at 640px, and its first line dropped to 32 characters.
To keep Japanese body text to 40 characters per line or fewer, add max-width: 40em or max-width: 40ic to the outer wrapper.
Keep text off the screen edges on smartphones
With only max-width and margin: 0 auto, the outer wrapper is 375px at a screen width of 375px, filling the whole screen, and the left and right margin are 0px. The text touches the left and right edges of the screen.
There are two ways to leave space on the sides. The first is to add left and right padding to the outer wrapper.
.post {
max-width: 40em;
margin: 0 auto;
padding: 0 1rem;
}
At a screen width of 375px, the width of the text inside the outer wrapper was 343px (375px − 16px × 2), leaving 16px of space on each side. However, when box-sizing is left at its default value content-box, max-width limits the width inside the padding. At a screen width of 1280px, the outer wrapper's total width was 672px (640px + 16px × 2), and the text width was 640px. With box-sizing: border-box, the total width of the outer wrapper became 640px, the text width became 608px, and the first line dropped to 38 characters. To keep 40 characters per line, leave content-box as it is and use max-width: 40em. With border-box, the text width is narrowed by the amount of padding.
The second way is to set the outer wrapper's width to the smaller of "the screen width minus the margins" and "40em" with the min() function.
.post {
width: min(100% - 2rem, 40em);
margin-inline: auto;
}
At a screen width of 375px, the outer wrapper was 343px wide, with left and right margin of 16px each. At a screen width of 1280px, it was 640px wide, with 320px on each side. Inside min(), you can add and subtract without writing calc(). As the MDN page on min() describes, spaces are required on both sides of -.

margin: 0 auto also sets the top and bottom margins to 0
margin: 0 auto is a shorthand that sets the top and bottom margin to 0 and the left and right to auto. If you first write margin-top: 40px to add space above the outer wrapper and then write margin: 0 auto after it, the computed value of margin-top is 0px.
To keep the top and bottom margins, use margin-inline: auto, as in the min() example above. In a horizontal writing mode, margin-inline sets only the left and right margin (MDN page on margin-inline). When I changed to margin-inline: auto under the same conditions, margin-top stayed at 40px.





