Top and bottom margins despite line-height: 1; and countermeasures

Even if CSS line-height: 1; is set, there may be margins above and below some elements. This is because the line-height property determines not only the height of the text but also the height of the entire line. Thus, line-height: 1; indicates that the height of the text matches the height of the entire line, but there is still a small margin above and below the text itself, which may not result in the expected layout.

One solution to this problem is to apply negative margins using the: :before and::after pseudo-elements.

.your-selector::before, .your-selector::after { 
 content: ""; 
 display: block; 
 height: 0; 
 width: 0; 
 margin-top: -0.1em; 
 margin-bottom: -0.1em; 
}

This code applies negative top and bottom margins to the target element and removes the margins. However, care should be taken when applying negative margins, as they may affect the layout.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top