You must assign some required styles to the elem element you just created using JavaScript. Since these properties are absolutely obligatory and it is impossible to allow their redefinitions in any way, it was decided to use the !important directive.
elem.style.color = "red !important"; However, this does not work. Having rummaged on the Internet I found out that it is possible to set an !important property only through the cssText property (well, or through setAtribute ).
elem.style.cssText = "color: red !important;"; So, like, really works. However, I do not understand why, after such an installation, it is possible to override the value of a property:
elem.style.color = "green"; // работает! Текст действительно становится зеленым. Хотя, по идее, не должен. Moreover, it is not clear to me why even in the absence of the !important property it is not possible to override this property in the .css file.
let elem = document.querySelector("#test"); elem.style.color = "red"; #test { color: green; } <div id="test">Hello</div> Please explain.