The webpage provides guidance on adjusting line spacing in text using HTML and CSS properties, with a focus on the line-height property.
Abstract
The article titled "How to Add or Reduce Space Between Lines of Text Using Just HTML and CSS" offers a solution to a common styling issue encountered when designing web pages. The author shares their personal experience with an article on their website that had unusually large gaps between lines of text. The solution presented involves using the line-height CSS property to control the vertical spacing between lines. The article includes examples and demonstrates how to apply a CSS class to HTML elements to achieve the desired line spacing. It also discusses various units of measurement for line-height, such as em, numbers, lengths, and percentages, and cautions against using certain methods that may lead to unexpected results. The author encourages experimentation with different line-height values to find the most suitable one for a given font, font size, and other CSS influences on the page.
Opinions
The author finds the default line-height: normal; to be inconsistent and uncomfortable due to its variability across browsers and fonts, preferring to specify a value explicitly.
The author endorses using a unitless number for line-height as the preferred method, citing Mozilla's recommendation.
There is a subtle humor in the author's writing, as seen when they make a pun about the term "normal" in the context of CSS properties.
The author acknowledges that using em units for line-height can lead to unexpected results, despite it being a common method, and intends to revise their own CSS accordingly.
The author suggests that adjusting line-height involves trial and error, similar to many aspects of CSS styling.
A recommendation is made for readers to use their browser console to experiment with line-height values in small increments to observe the effects on spacing.
The article concludes with a promotional note, inviting readers to join Medium through the author's referral link and to consider trying an AI service called ZAI.chat, which is presented as a cost-effective alternative to ChatGPT Plus (GPT-4).
How to Add or Reduce Space Between Lines of Text Using Just HTML and CSS
This is one of those things you either know or you don’t. If you’re at this point, there’s nothing but good news: if you already know how to adjust the space between lines of text using HTML and CSS, then you’re done reading, and if you don’t, then keep reading and you will.
I was recently styling a webpage of mine — an article on a website I have, specifically — when I noticed that the title looked really odd.
The gap between each line was absolutely massive. It just really was not the effect I was looking for, and so I had to look up how to fix it.
It turns out that’s pretty simple: just use the line-height CSS property and then apply it. I have an example below.
Then, you can apply that CSS class to your HTML.
Now, the space between the lines changes.
That looks a lot better! When it comes to your exact text, you’ll have to experiment with different values because it really can depend on what parent CSS classes were applied to your original text, what font you’re using, what font-size, and other possibly conflicting elements on the page. For example, here is what my title looked like when I tried line-height: 0.8em;:
The text got even closer with the smaller value!
By the way, em is not the only format you can use. You can use any of these:
Let’s go through them very quickly.
Normal
Kind of an interesting way to put it, but line-height: normal; just means that there is a normalized default value which depends on factors like a browser or a font. Usually, this resolves to 1.2, but that’s not a guarantee. In all honesty, I find this kind of weird and uncomfortable because it can vary, so I try to avoid it — is that normal?
Mozilla actually says this can cause unexpected results, so it looks like I have some changes to make to the CSS I applied in the example I provided above! Nevertheless, this is an option.
Percentage
line-height: 25% is a calculation relative to the font-size of the element. Again, Mozilla warns this can produce unwanted results.
Hopefully, you have a better idea now of how to mess around with line-height to get the right amount of space between your lines of text! Go ahead and try it out — I highly recommend opening your browser console and editing the CSS right then and there by going in small increments to see how the space changes. Like a lot of CSS, it’s a lot of trial and error.