【发布时间】:2023-03-17 16:05:02
【问题描述】:
我对 HTML/CSS 很陌生,并试图理解其中涉及的很多概念。我正在尝试将 2 个 div 垂直居中在一个较大的 div 中,并将这些 div 中的元素居中。
HTML:
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<div id="MainDiv">
<div id="SubDiv">
<form method="POST">
<input type="text" name="example1" class="Text">
<input type="text" name="example2" class="Text">
<input type="submit" value="Go"> <br>
</form> <br/>
</div>
<div id="SubDiv">
<input type="button" value="Button 1"/>
<input type="button" value="Button 2"/>
</div>
</div>
</body>
CSS:
#MainDiv
{
width: 60%;
height: 30%;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
text-align: center;
border: 1px solid black;
}
#SubDiv
{
width: 90%;
height: 45;
margin: auto;
border: 1px solid black;
}
.Text
{
width: 40%;
}
当然,我已经进行了很多搜索以试图解决此问题,但任何修复似乎要么无济于事,要么更加混乱。任何帮助将不胜感激。
【问题讨论】:
-
你看过这个网站吗:howtocenterincss.com 非常适合在 CSS 中居中
-
您忘记为其他元素添加
position属性,并且这些属性top: 0; left: 0; bottom: 0; right: 0;和属性id应该是唯一的,但您将其应用于多个元素。