Once you master whitespace on websites, you can freely adjust character spacing or add line breaks only on mobile devices.
I have also compiled the causes and solutions for mysterious gaps or unintended line breaks that occur when building a website.
Read this page and you too can become a whitespace master starting today!
Why copied text doesn't display the same way as in design tools
When entering text in HTML, you can't just copy and paste from Figma or XD and be done with it.
For example, if you copy and paste text into a p tag, it will likely look like this.
シモーヌ・ド・ボーヴォワール: 彼女は、消費がジェンダーによって異なる役割や期待に影響されることを指摘しました。この観点から、消費行動は個々人の性別に基づく社会的な役割や期待を満たすために行われます。
<p>シモーヌ・ド・ボーヴォワール: 彼女は、消費がジェンダーによって異なる役割や期待に影響されることを指摘しました。この観点から、消費行動は個々人の性別に基づく社会的な役割や期待を満たすために行われます。</p>
Even though you have confirmed that there are line breaks in both the code editor and the design tool, the result will look like this.
This is because in HTML, normal line breaks (newline characters, \n) are ignored and treated as whitespace characters.
If you actually look at the browser, you can see that a whitespace character has been inserted where the line break was.
To correctly add spaces or line breaks in HTML, you need to insert character codes or line break tags.
Detailed methods are introduced below.
How to add spaces or whitespace in HTML
There are several ways to add spaces between characters or lines in HTML. For example, the following methods:
Insert character codes to add spaces between characters
Insert HTML line break tags
Create margins with CSS
Articles that appear at the top when searching for "HTML whitespace" often explain these three, but in actual web production, more detailed knowledge is required.
In this article, I will introduce how to add spaces and line breaks using the following methods:
Insert half-width character codes to create spaces
Insert full-width character codes to create spaces
Adding line breaks to create spacing
Adding line breaks only on mobile
None of these are difficult methods, so let's learn them all at once.
Creating spaces by inserting half-width character codesFor example, you might be using Roman characters in headings, or you might want to adjust spacing with half-width spaces even in Japanese text.
To insert a half-width space in HTML, you don't use an HTML tag, but rather insert the following character code.
山田 太朗
Regarding half-width spaces, inserting them in a code editor will reflect them the same way, but this method is useful when, for example, you want to insert two or three half-width spaces.
For example, it looks like this. Let's try inserting three half-width spaces between "dolor" and "sit".
<p>Lorem ipsum dolor sit amet consectetur.</p> <p>Lorem ipsum dolor sit amet consectetur.</p>
The result is as follows. Up to one half-width space is the same as in the code editor, but since two or more are ignored, a character code is required.
Creating spaces by inserting full-width character codesIf you want to create a space using a full-width character, it is the same as the half-width one. The character code is .
<p>山田 太朗</p> <p>山田 太朗</p>
The result looks like this. Just like with half-width spaces, you can insert multiple ones.
Adding line breaks to create spacingNext is the method for adding line breaks to create spacing. This uses an HTML tag called <br>.
<p>シモーヌ・ド・ボーヴォワール:<br> 彼女は、消費がジェンダーによって異なる役割や期待に影響されることを指摘しました。この観点から、消費行動は個々人の性別に基づく社会的な役割や期待を満たすために行われます。</p>
The result looks like this. Unlike the beginning, it is indeed broken into a new line, but the heading and the text are stuck together.
If you want to add space between the heading and the text as you entered in the code editor, add another <br> tag.
<p>シモーヌ・ド・ボーヴォワール:<br> <br> 彼女は、消費がジェンダーによって異なる役割や期待に影響されることを指摘しました。この観点から、消費行動は個々人の性別に基づく社会的な役割や期待を満たすために行われます。</p>
Now it looks just as designed.
You can also add more line breaks by adding more line break tags.
Adding line breaks only on mobileThis is a slightly more advanced technique. It is common to want to break a line at a good position because while one line is fine on a PC screen, it gets cut off at the edge of the screen on a smartphone.
If you insert a line break tag, it will break on both PC and smartphone, so you probably want to break only on smartphone.
The way to do this is to use CSS to enable line breaks only on PC screens.
@media (min-width: 768px)
.br-mobile
display: none;
<p>シモーヌ・ド・<br class="br-mobile">ボーヴォワール:<br> <br> 彼女は、消費がジェンダーによって異なる役割や期待に影響されることを指摘しました。この観点から、消費行動は個々人の性別に基づく社会的な役割や期待を満たすために行われます。</p>
The result will look like this. On a PC screen, it looks the same as before, and on a smartphone, it breaks where the br tag was inserted.
Causes of mysterious spaces and line breaks in HTML and how to remove them
When coding in HTML, you may find spaces or gaps appearing where you didn't put them.
Most of the causes are as follows.
Browser default styles are applied
Line breaks or spaces are included in the code
Influence of padding or margin
Influence of font-size or line-height
If browser default styles are applied, gaps may appear between content.
Since it is difficult to remove browser default styles one by one manually, let's use a reset CSS.
Line breaks or spaces are included in the codeAs written in the section on how to add margins, if you insert spaces or line breaks in the code editor, half-width spaces will appear in the browser.
If there is no problem with the CSS, there might be a problem with the text input, so be sure to check it.
Influence of padding or marginIf there is no problem on the HTML side, the cause is CSS. Sometimes padding or margin is applied, creating unnecessary whitespace.
If you hover your mouse cursor over an HTML tag in Chrome's Developer Tools, you can check how padding and margin are being applied, as shown in the image above.
Influence of font-size or line-heightIf there are no issues with padding or margin, try checking the font-size and line-height.
For example, if the line-height is set higher only for an a tag as shown below, the spacing will become more noticeable compared to menus that do not contain an a tag.
It is important to be careful, as not only menus but also h1 tags can sometimes have excessively large vertical spacing due to line-height.
Summary
In this article, we introduced how to add spacing and line breaks in HTML, as well as the causes of and solutions for unintended spacing. Here is a summary.
Add spacing in HTML by inserting character codes
Use the br tag to insert a line break in HTML
When unintended spacing appears, check the CSS using developer tools
It is common for strange spacing to appear, so being able to check and fix everything using developer tools will make your work much more efficient!