javascript如何设置图片居中
javascript设置图片居中的方法:
搭建一个HTML框架
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> div { width: 500px; height: 200px; border: green solid 1px; } </style> </head> <body> <div id="app"> <span id="img"> <img src="img/1.jpg" width="200" /> </span> </div> </body> </html>
使用setAttribute()方法添加图片居中样式
var div=document.getElementById("app"); var img=document.getElementById("img"); div.setAttribute("style","display:table;text-align: center;"); img.setAttribute("style","display:table-cell;vertical-align: middle;");
【相关推荐:javascript学习教程】