17

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

Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

1 Answers1

14

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;
}
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Thanks for understanding my question and giving good references. Yes I think negative margin is the only solution for this, but even it's doesn't solve the problem fully http://jsfiddle.net/jitendravyas/V3eWV/11/ specially when we have multiple block one after another. What do you think? – Jitendra Vyas Aug 01 '12 at 07:27
  • @JitendraVyas: In a case of emergency, use a wrapper (see edit). – Zeta Aug 01 '12 at 07:40
  • "you can't change the line-height of the ::first-line, it will always get applied." - you can actually, problem is that it will again be applied both above and below the letters. – Mr Love Jun 29 '14 at 23:32