Modifying the CSS font-variant property
The font-variant
CSS property allows us to specify variations in the font’s appearance, including the use of small caps. We can dynamically modify the font-variant
property of an HTML element using JavaScript to apply small caps to the text.
Syntax:
element.style.fontVariant = "small-caps";
Example: This example shows the use of the above explained approach.
HTML
<!DOCTYPE html> < html > < body style = "text-align: center" > < h1 style = "color: green" >w3wiki</ h1 > < h2 > How to set the font of< br />a text in small letters using JavaScript? </ h2 > < p id = "sudo" >WELCOME TO w3wiki</ p > < br /> < script > const element = document.getElementById("sudo"); element.style.fontVariant = "small-caps"; </ script > </ body > </ html > |
Output:
JavaScript Program to set the Font to be Displayed in Small Capital Letters
In this article, we will set the font to be displayed in small capital letters with JavaScript. To change the font to be displayed in small capital letters dynamically, we use HTML DOM Style font-variant property. CSS provides a straightforward way to apply small caps to text, there may be situations where you need to dynamically set the font to small caps using JavaScript. Let’s discuss two approaches to achieve this.
Contact Us