CRM Tools & Best CRM Developers To Follow
For example, let’s say we have a paragraph element with an ID of ‘my-paragraph’. We could use getComputedStyle() to get the font size of this element:
let myParagraph = document.getElementById('my-paragraph'); let fontSize = window.getComputedStyle(myParagraph).fontSize; console.log(fontSize); // "16px"
We can also modify existing styles with this method. For instance, if we wanted to change the font size of our paragraph, we could do this:
let myParagraph = document.getElementById('my-paragraph'); myParagraph.style.fontSize = '24px';
The primary limitation of using getComputedStyle() is that it only returns existing styles in a document and cannot add new ones. If you need to add a new style to an element, you will need to use the setAttribute() method instead.
For example, let’s say we wanted to add a border around our paragraph element:
let myParagraph = document.getElementById('my-paragraph'); myParagraph.setAttribute("style", "border: 1px solid #000000;");