对齐整个页面内容,使表格稍微偏离中心

时间:2018-08-13 17:44:14

标签: html css

.formholder {
width: inherit;
}

.formposition
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}


input[type=text], select {
    width: 100%;
    padding: 2vh 4vh;
    margin: 1vh;
    display: inline-block;
    border: 0.5vh #ffffff;
    border-radius: 1vh;
    box-sizing: border-box;
    border: solid 0.5vh #ffffff;
}

.formposition {
position: absolute;
width: auto;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

input[type=text], select {
    width: 100%;
    padding: 2vh 4vh;
    margin: 1vh;
    display: inline-block;
    border: 0.5vh #ffffff;
    border-radius: 1vh;
    box-sizing: border-box;
    border: solid 0.5vh #ffffff;
}

input[type=submit] {
width: 100%;
    padding: 2vh 4vh;
    margin: 1vh;
    display: inline-block;
    border-radius: 1vh;
    box-sizing: border-box;
    color: #ffffff;
    background-color: #000000;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    border: solid 0.5vh #ffffff;
}

@media only screen and (max-width: 500px) {
input[type=submit] {
width: 100%;
    padding: 2vh 4vh;
    margin: 1vh;
    display: inline-block;
    border-radius: 1vh;
    box-sizing: border-box;
    color: #ffffff;
    background-color: #000000;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    border: solid 0.5vh #ffffff;
}
}

form {
    width: auto;
    margin-left: 0vh;
    margin-right: 0vh;
    text-align: center;
}
<div class="formholder">
<div class="formposition">
<form action="responses.php" method="post">
<input type="text" placeholder= "Test Field" name="TestField" required><br>
<input type="submit">
</form>
</div>
</div>

DIV可以居中,但表单在自身中不能完全居中。

Alignment Highlighted 按钮和输入应在页面上完全居中,但不要触摸DIV的左侧,并且在右侧(包括边框)溢出。

全部内容位于页面中间。由于表单元素偏离中心,因此左侧的空间稍多,并且很明显它们没有居中。

修复程序或指针将不胜感激。

预先感谢:)

2 个答案:

答案 0 :(得分:1)

这是因为input[type=submit]的左右边距。将其更改为margin: 1vh 0即可解决此问题,因为您希望宽度达到100%。

您输入的实际宽度为100%+ 1vh(右边距)+ 1vh(左边距)。保证金不会计算为宽度。

.formholder {
width: inherit;
}

.formposition
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}


input[type=text], select {
    width: 100%;
    padding: 2vh 4vh;
    margin: 1vh 0;
    display: inline-block;
    border: 0.5vh #ffffff;
    border-radius: 1vh;
    box-sizing: border-box;
    border: solid 0.5vh #ffffff;
}

.formposition {
position: absolute;
width: auto;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

input[type=text], select {
    width: 100%;
    padding: 2vh 4vh;
    margin: 1vh 0;
    display: inline-block;
    border: 0.5vh #ffffff;
    border-radius: 1vh;
    box-sizing: border-box;
    border: solid 0.5vh #ffffff;
}

input[type=submit] {
width: 100%;
    padding: 2vh 4vh;
    margin: 1vh 0;
    display: inline-block;
    border-radius: 1vh;
    box-sizing: border-box;
    color: #ffffff;
    background-color: #000000;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    border: solid 0.5vh #ffffff;
}

@media only screen and (max-width: 500px) {
input[type=submit] {
width: 100%;
    padding: 2vh 4vh;
    margin: 1vh 0;
    display: inline-block;
    border-radius: 1vh;
    box-sizing: border-box;
    color: #ffffff;
    background-color: #000000;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    border: solid 0.5vh #ffffff;
}
}

form {
    width: auto;
    margin-left: 0vh;
    margin-right: 0vh;
    text-align: center;
}
<div class="formholder">
<div class="formposition">
<form action="responses.php" method="post">
<input type="text" placeholder= "Test Field" name="TestField" required><br>
<input type="submit">
</form>
</div>
</div>

答案 1 :(得分:0)

似乎输入的左右边距将输入推到包含表单/ div的外面。尝试使用“ margin:1vh 0;”输入而不是“ margin:1vh;”。