页面中间的完美居中按钮

Perfectly center button in the middle of the page

代码

<!DOCTYPE html>
<html>
<head>
<title>Google Maps</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<style type="text/css">
#button {
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
height: 30px;
width: 200px;
left: 50%;
top: 50%;
position: absolute;
}
</style>
</head>
<body>
<input type="button" id="button" value="Open Map" />
</body>
</html>

我不确定如何将按钮完美地居中放置在页面中间。我尝试使用左和前 50%;但这并不能使按钮完美地居中于页面中间。谢谢

下面是演示工作代码。

Demo code Check Out

给按钮一个margin-left:-width-of-the-button/2。 你的情况:

margin-left:-100px;
margin-top:-15px;

除了一些更改外,您已完成所有操作:

margin-top: -15px;   /* = -height / 2   */
margin-left: -100px; /* = -width / 2    */
position: fixed;     /* Fixed is better */

片段

#button {
  appearance: none;
  -moz-appearance: none;
  -webkit-appearance: none;
  height: 30px;
  width: 200px;
  left: 50%;
  top: 50%;
  margin-top: -15px;   /* = -height / 2   */
  margin-left: -100px; /* = -width / 2    */
  position: fixed;     /* Fixed is better */
}
<input type="button" id="button" value="Open Map" />

如果按钮是 body 中唯一要设置为居中的元素。然后将输入字段添加到 div/span.. 然后在下面应用 cssspan/div { text-align:center; } #button { position:absolute; top:50%; }

如果整个 body 需要居中,则 body { text-align:center; }

您可以在周围使用容器以使其更易于操作。

这是我做的:

#button {
    appearance: none;
    -moz-appearance: none;
    -webkit-appearance: none;
    height: 30px;
    width: 200px;

    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}
.container {
    width:100%;
    height:100%;
    background-color:yellow;
    position:absolute;
    text-align:center;
}

Here is the JSFiddle demo