CSS Properties: How to set the font size for different HTML elements?

Go to Exercise page


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 font size for different HTML elements</title><!-- Sets the title of the document -->
<style type="text/css"> /* Starts CSS styling */
  p { /* Targets paragraph elements */
    font-size: 100%; /* Sets the font size of paragraphs to 100% of the parent element's font size */

h1 { /* Targets h1 elements */
    font-size: 250%; /* Sets the font size of h1 elements to 250% of the parent element's font size */

h2 { /* Targets h2 elements */
    font-size: 200%; /* Sets the font size of h2 elements to 200% of the parent element's font size */

</style><!-- Ends CSS styling -->
<p><strong>w3resource Tutorial</strong></p><!-- Paragraph element with strong (bold) text -->
<p>This is a paragraph.</p><!-- Paragraph element -->
<h1>This is heading 1</h1><!-- h1 heading element -->
<h2>This is heading 2</h2><!-- h2 heading element -->
</html><!-- Ends the HTML document -->


  • This HTML document demonstrates how to set different font sizes for different HTML elements using CSS.
  • Comments are added to both HTML and CSS to explain each section of the code.
  • In the CSS <style> block, three CSS rules are defined to target different HTML elements (p, h1, h2).
  • The font-size property is used to specify the font size for each targeted element.
  • Each element's font size is defined relative to its parent element's font size.

Live Demo:

See the Pen font-size-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.