Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line-height affecting spacing above first line and after last line

Tags:

css

I've a text in side heading with multiple lines. Want the spacing the two lines to increase, so I set a line-height. When I do this, not only does it increase space between the two lines, it also increases spacing above the first line (and maybe below the second). How can I increase spacing between the two lines only, without increasing above and below.

I know it's a behavior of Line-height. but just curious if there is any good solution for this.

This is just en example to what I'm asking.

Jsfiddle: http://jsfiddle.net/jitendravyas/V3eWV/

enter image description here

like image 419
Jitendra Vyas Avatar asked Aug 01 '12 06:08

Jitendra Vyas


People also ask

Is line-height the same as line spacing?

What is line height and letter spacing? Line height, also known as leading, is the distance between each line when you have two or more rows of text. Letter spacing, also known as tracking, is the size of the gaps between each letter of each word.

How do you calculate line spacing height?

I think the formula for line spacing is actually lineHeight / fontSize . For example, if the font size is 16 and the line height is 24 in your design program, you'd use paragraphStyle. lineSpacing = 24 / 16 or simply paragraphStyle. lineSpacing = 1.5 .

What is the difference between line-height and height?

Height is the vertical measurement of the container, for example, height of a div. Line-height is a CSS property to specify the line height i.e. the distance from the top of the first line of text to the top of the second. It is the space between the lines of two paragraphs.

What is the difference between line-height and leading?

Line height is the vertical distance between two lines of type, measured from the baseline of one line of type to the baseline of the next. Traditionally, in metal type, this was the combined measurement of the font size and the strips of lead that were inserted between each row (called “leading”) of type.


1 Answers

You can use negative margins for this, although there is something to keep in mind:

line-height is a funny thing. According to CSS2.1 it doesn't specify the line-height but the minimum height of line-blocks:

On a block container element whose content is composed of inline-level elements, 'line-height' specifies the minimal height of line boxes within the element. The minimum height consists of a minimum height above the baseline and a minimum depth below it, exactly as if each line box starts with a zero-width inline box with the element's font and line height properties. We call that imaginary box a "strut." (The name is inspired by TeX.).

A line box is defined in 9.4.2 Inline formatting contexts:

In an inline formatting context, boxes are laid out horizontally, one after the other, beginning at the top of a containing block. Horizontal margins, borders, and padding are respected between these boxes. The boxes may be aligned vertically in different ways: their bottoms or tops may be aligned, or the baselines of text within them may be aligned. The rectangular area that contains the boxes that form a line is called a line box.

This doesn't change in CSS3 very much, at least if you don't change the line-stacking. However, there is no property which targets your problem directly: you can't change the line-height of the ::first-line, it will always get applied.

That said, use negative margins for the time being. Even better, wrap your elements in a generic container. By using :first-child and :last-child you can add as many elements as you like.

Example

<div>
    <h1>I've a text in side heading with multiple lines. Want the spacing the two lines to increase, so I set a line-height. When I do this, not only does it increase space between the two lines, it also increases spacing above the first line.</h1>    
    <h1>I've a text in side heading with multiple lines. Want the spacing the two lines to increase, so I set a line-height. When I do this, not only does it increase space between the two lines, it also increases spacing above the first line.</h1>
</div>
body {padding:30px;background:yellow;border:1px solid red;margin:0}
div{background:red;margin:0;padding:0;border:1px solid green;}
h1{line-height:2em;}
div > h1:first-child{
    margin-top:-.25em;
}
div > h1:last-child{
    margin-bottom:-.25em;
}
like image 137
Zeta Avatar answered Nov 14 '22 15:11

Zeta