The Wayback Machine - https://web.archive.org/web/20250109022605/https://www.geeksforgeeks.org/css-line-height-property/
Open In App

CSS line-height Property

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS line height property determines the height of each line of text within an element. It can be set as a specific length, percentage of the font size, or unitless number, affecting spacing between lines for improved readability and aesthetics.

Syntax:

line-height: normal|number|length|percentage|initial|inherit;

Property values:

ValueDescription
normalRepresents the default line-height of the element.
initialSets the line height property to its default value.
numberUnitless numbers are multiplied by the element’s font size to determine line height.
lengthSpecifies a fixed line height using a length unit (e.g., px, em).
percentageSets the line height as a percentage of the element’s font size.

Here we see example of each Property values:

line-height: normal; property

The line-height: normal; property sets the default line height for text, typically ensuring optimal readability and spacing within the element.

Example: In this example, we are using the line-height: normal; property.

html
<!DOCTYPE html>
<html>

<head>
    <title>CSS line-height Property</title>
    <style>
        .geek {
            line-height: normal;
            background: green;
            color: white;
        }
    </style>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        CSS line-height Property
    </h2>

    <div class="geek">
        A computer science portal for geeks.<br>
        This div has line-height: normal;
    </div>
</body>

</html>

Output: lineheight

line-height: number; property

The line-height: number; property sets the line height to a unitless number multiplied by the current font size, influencing text spacing and readability effectively.

Example: In this example, we use the line-height: number; property.

html
<!DOCTYPE html>
<html>

<head>
    <title>CSS line-height Property</title>
    <style>
        .geek {
            line-height: 2.5;
            background: green;
            color: white;
        }
    </style>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        CSS line-height Property
    </h2>

    <div class="geek">
        A computer science portal for geeks.<br>
        This div has line-height: 2.5;
    </div>
</body>

</html>

Output: 

lineheight

line-height: length; property

The line-height: length; property sets a specific fixed line height using a length unit (e.g., pixels, em), adjusting text spacing within the element accordingly.

Example: In this example, we are using line-height: length property.

html
<!DOCTYPE html>
<html>

<head>
    <title>CSS line-height Property</title>
    <style>
        .geek {
            line-height: 2em;
            background: green;
            color: white;
        }
    </style>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        CSS line-height Property
    </h2>

    <div class="geek">
        A computer science portal for geeks.<br>
        This div has line-height: 2em;
    </div>
</body>

</html>

Output: lineheight

line-height: percentage; property

The `line-height: percentage;` property sets the line height as a percentage of the current font size, adjusting text spacing proportionally within the element.

Example: In this example, we are using line-height: percentage property.

html
<!DOCTYPE html>
<html>
  
<head>
    <title>CSS line-height Property</title>
    <style>
        .geek {
            line-height: 150%;
            background: green;
            color: white;
        }
    </style>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        CSS line-height Property
    </h2>

    <div class="geek">
        A computer science portal for geeks.<br>
        This div has line-height: 150%;
    </div>
</body>
  
</html>

Output: lineheight

Supported Browsers: The browser supported by line-height property are listed below:

CSS line-height Property – FAQs

What is the line-height property in CSS?

The line-height property defines the amount of space between lines of text, which can affect readability and layout appearance.

What are the common values used with line-height?

Common values include unitless numbers (e.g., 1.5), length units (e.g., 20px), percentages, and keywords like normal.

How does a unitless line-height value work?

A unitless value, like line-height: 1.5;, multiplies the element’s font size to determine the line spacing, making it adaptable to different font sizes.

Does line-height apply to inline elements?

Yes, line-height affects the spacing in inline elements, including text and inline-level boxes, controlling how they align vertically.

How can I ensure consistent line spacing across different text sizes?

Using a unitless value for line-height is recommended, as it scales proportionally with font size changes, ensuring consistent spacing.



Next Article

Similar Reads

three90RightbarBannerImg