.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 居中,但表单本身并未完全居中。
按钮和输入应完全位于页面中央,但它们不会触及 DIV 的左侧,并且会在右侧溢出,包括边框。
全部内容将位于页面中间。由于表单元素偏离中心,左侧的空间略多,很明显它们没有居中。
修复或指针将不胜感激。
提前致谢:)
最佳答案
这是因为 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>
关于html - 对齐整个页面内容使表格稍微偏离中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51827866/