<br> function cc() <br> { <br> var cf=window.screen.width; <br> alert(cf ) <br> <br> if(cf<=1920) <br /> { <br /> cf=-(1920-cf)/2 <br /> } <br /> else if(cf>1920) <br> { <br> cf=(cf-1920)/2 <br> } <br> document.getElementById("m1").style.marginLeft=cf; <br> alert(cf) <br> } <br> </ script> <br> <br> </head> <br> <body onLoad="cc()"> <br> <div style=" width:100%; overflow:hidden;" id=" a"> <br> <div style="width:1920px; height:500px;" id="m1"><img src="images/bar3.png" width="1920" height="500 " text-align:center></div> <br> </div> <br> </body> <br> </html> <br> <br> The above can be used to assign a left shift to M1 :document.getElementById("m1").style.marginLeft=cf; <br> But after adding this line to the first line: <!doctype html> <br> M1 fails to assign value. Generally, HTML files have< ;!doctype html>, how to deal with it? </p> <p> </p> <br> <h2>Reply to discussion (solution)</h2> <p class="sougouAnswer"> document.getElementById("m1").style.marginLeft=cf "px"; <br> </p> <p class="sougouAnswer"> </p> <p class="yy"> document.getElementById("m1").style.marginLeft=cf "px"; </p> <br> That's it, thank you! <br> <br> <div style="width:1920px; height:500px;" id="m1">Changed to <br> <div style="width:1920px; height:500px;" class= "m1"> <br> <br> Why can't I assign a value using document.getElementsByTagName("m1").style.marginLeft=cf "px";? <p class="sougouAnswer"> 6 <br> <br> </p> <p class="yy"> </p> <p class="yy"> <br> document.getElementById("m1").style.marginLeft=cf "px"; </p> <br> Okay, thank you ! <br> <br> <div style="width:1920px; height:500px;" id="m1">Changed to <br> <div style="width:1920px; height:500px;" class= "m1"> <br> <br> Why can't I assign a value using document.getElementsByTagName("m1").style.marginLeft=cf "px";? <br> getElementsByTagName() uses the tag name to select <br> <br> uses class as <br> document.getElementsByClassName("m1")[0].style.marginLeft=cf "px"; <br> Or <br> document.querySelector(".m1").style.marginLeft=cf "px"; <br> <br> <p class="sougouAnswer"> Learned, thank you! </p>