w3resource

CSS Properties: How to set the line height in percent?

Go to Exercise page

Solution:

HTML Code:

<!DOCTYPE html><!-- Declares the document type and version of HTML -->
<html><!-- Begins the HTML document -->
<head><!-- Contains metadata and links to external resources -->
<title>How to set the line height in percent</title><!-- Sets the title of the document -->
<style type="text/css"> /* Starts CSS styling */
.small { /* Defines a CSS class named small */
    line-height: 80%; /* Sets the line height to 80% of the font size for elements with this class */
}

.big { /* Defines a CSS class named big */
    line-height: 200%; /* Sets the line height to 200% of the font size for elements with this class */
}
</style><!-- Ends CSS styling -->
</head><!-- Ends the head section -->
<body><!-- Begins the body of the document -->
<p><strong>w3resource Tutorial</strong></p><!-- Paragraph element with strong (bold) text -->
<p>Standard Line-height</p><!-- Paragraph element with text indicating standard line height -->
<p>CSS, stands for Cascading Style Sheet is a computer language to describe presentation (for example width, height, color, background color, alignment etc.) of HTML and XML (and XML based languages like XHTML, SVG) web documents. In all the examples of our tutorials, we have used HTML for implementing CSS.</p><!-- Paragraph element with standard line height -->
<p>Smaller Line-height</p><!-- Paragraph element with text indicating smaller line height -->
<p class="small"><!-- Paragraph element with class small -->
CSS, stands for Cascading Style Sheet is a computer language to describe presentation (for example width, height, color, background color, alignment etc.) of HTML and XML (and XML based languages like XHTML, SVG) web documents. In all the examples of our tutorials, we have used HTML for implementing CSS.
</p><!-- Ends the paragraph element with smaller line height -->
<p>Bigger Line-height</p><!-- Paragraph element with text indicating bigger line height -->
<p class="big"><!-- Paragraph element with class big -->
CSS, stands for Cascading Style Sheet is a computer language to describe presentation (for example width, height, color, background color, alignment etc.) of HTML and XML (and XML based languages like XHTML, SVG) web documents. In all the examples of our tutorials, we have used HTML for implementing CSS.
</p><!-- Ends the paragraph element with bigger line height -->
</body><!-- Ends the body section -->
</html><!-- Ends the HTML document -->

Explanation:

  • This HTML document demonstrates how to set line heights using percentages in CSS.
  • CSS comments are added to explain each section of the code.
  • The .small class sets the line height to 80% of the font size for elements with this class, resulting in smaller line spacing.
  • The .big class sets the line height to 200% of the font size for elements with this class, resulting in bigger line spacing.

Live Demo:

See the Pen line-height-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
Yes Yes Yes Yes Yes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.