CSS Properties: How to set the font size for different HTML elements?
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 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 -->
</head>
<body>
<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 -->
</body>
</html><!-- Ends the HTML document -->
Explanation:
- 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
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/html-css-exercise/css-properties/font-size-answer.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics