我试图将一个按钮水平居中在一个 div 中,但没有运气:
var x = document.createElement("div"); // the div container
x.style.backgroundColor = "red";
x.style.width = "200px";
x.style.height = "200px";
document.body.appendChild(x);
var y = document.createElement("input"); // the button to be centered inside div "x"
y.type = "button";
y.value = "test button";
y.style.marginLeft = "auto";
y.style.marginRight = "auto";
x.appendChild(y);
我试着做y.style.textAlign = "center"
,"y.style.margin = "auto 0"
。我究竟做错了什么?