I would like to dynamically raise or lower individual letters in my label. Currently, I am supersetting the text for the elements:
let element = document.getElementById('xxx') element.textContent = 'Label'
I tried using <sup>2</sup>
oder <sub>
.
Unfortunately this has no effect. Anyone have an idea how I can get this solved?
3
Answers
To dynamically raise or lower individual letters in a label using JavaScript and HTML, you can use HTML’s and tags to superscript and subscript specific characters. Here’s a complete solution to your problem:
In this solution, we have two functions, raiseLetters and lowerLetters, that take the id of the label element as an argument. These functions iterate through each character in the label’s text and wrap it with or tags, effectively raising or lowering individual letters.
You can call these functions as needed to dynamically modify the positioning of letters in your label. Uncomment the lowerLetters function if you want to lower the letters instead.
You could create two css classes, one to superscript and one to subscript a letter, like shown here.
Make sure to put the letters you want to change individually into separate elements that you can address individualy like:
Then you can toggle the classes for the letters you want with:
document.getElementById("letter1").classList.toggle("sub")
ordocument.getElementById("letter1").classList.toggle("sup")
.(Make sure to use your correct class names and ids)
We are in Angular, so we define variables in .ts and use this variables in the .html
Then you can use in .ts
Or in .html
And your letter in position 3 have the class "upper"
e.g. this stackblitz