Home > Article > Web Front-end > How js changes css style
How to change the css style in js: 1. Use the cssText method; 2. Use the [setProperty()] method; 3. Use the style attribute corresponding to the css attribute.
The operating environment of this tutorial: windows7 system, css3 version, DELL G3 computer.
JS method to change css style:
The first one: use cssText
div.style.cssText='width:600px;height:250px;border:1px red solid;text-align: center;line-height: 250px;';
The second one : Use setProperty()
div.style.setProperty('width','700px'); div.style.setProperty('height','300px'); div.style.setProperty('line-height','300px'); div.style.setProperty('background','#f00'); div.style.setProperty('color','#fff'); div.style.setProperty('border','1px solid blue'); div.style.setProperty('text-align','center');
Third method: Use the style attribute corresponding to the css attribute
div.style.width = "800px"; div.style.height = "250px"; div.style.lineHeight = "250px"; div.style.background = "#000"; div.style.color = "#fff"; div.style.border = "1px solid #111"; div.style.textAlign = "center";
这是一个盒子
Recommended related tutorials:CSS Video Tutorial
The above is the detailed content of How js changes css style. For more information, please follow other related articles on the PHP Chinese website!