无法使用 margin-top 使我的 div 标签向下移动?

Cannot make my div tag move down using margin-top?

我在这里尝试做的是给我的用户名 div 一个 margin-top:10%; 就那么简单。

Here 是它当前看起来像直播的一个示例,您可以看到我的用户名区域挤在我的内容 div.

的顶部

我只想稍微降低一点。关于为什么或如何解决这个问题的任何想法?

代码

input {
  outline: none;
}
body {
  font-family: sans-serif;
  background-color: #222222;
}
#header {
  color: #fff;
  font-size: 25px;
  text-align: center;
}
#content {
  width: 60%;
  height: 250px;
  background-color: #303640;
  margin-left: 20%;
}
#username {
  width: 80%;
  height: 40px;
  background-color: #373e49;
  margin-top: 10%;
  margin-left: 10%;
  border: 1px solid #373e49;
  border-radius: 5px;
}
#usernameIcon {
  width: 12%;
  float: left;
  margin-top: 10px;
}
.usernameIcon {
  width: 20px;
  height: 20px;
}
#usernameTextbox {
  width: 88%;
  float: right;
  margin-top: 10px;
}
.tbUsername {
  background-color: #373e49;
  border-left: 2px solid #434a54;
  border-right: none;
  border-top: none;
  border-bottom: none;
  padding-left: 10px;
  color: #fff;
}
<div id="header">
  <p>Welcome</p>
</div>
<div id="content">
  <form method="post" action="index.php">
    <div id="username">
      <div id="usernameIcon">
        <img src="IMAGES/UsernameIcon.svg" class="usernameIcon">
      </div>
      <div id="usernameTextbox">
        <input type="text" name="username" class="tbUsername" placeholder="Username">
      </div>

    </div>
    <div id="password">

    </div>
    <div id="btnLogin">

    </div>
  </form>
</div>

在您的 #content

中设置一个 padding-top:1px

input {
  outline: none;
}
body {
  font-family: sans-serif;
  background-color: #222222;
}
#header {
  color: #fff;
  font-size: 25px;
  text-align: center;
}
#content {
  width: 60%;
  height: 250px;
  background-color: #303640;
  margin-left: 20%;
  padding-top:1px /* NEW */
}
#username {
  width: 80%;
  height: 40px;
  background-color: #373e49;
  margin-top: 10%;
  margin-left: 10%;
  border: 1px solid #373e49;
  border-radius: 5px;
}
#usernameIcon {
  width: 12%;
  float: left;
  margin-top: 10px;
}
.usernameIcon {
  width: 20px;
  height: 20px;
}
#usernameTextbox {
  width: 88%;
  float: right;
  margin-top: 10px;
}
.tbUsername {
  background-color: #373e49;
  border-left: 2px solid #434a54;
  border-right: none;
  border-top: none;
  border-bottom: none;
  padding-left: 10px;
  color: #fff;
}
<div id="header">
  <p>Welcome</p>
</div>
<div id="content">
  <form method="post" action="index.php">
    <div id="username">
      <div id="usernameIcon">
        <img src="IMAGES/UsernameIcon.svg" class="usernameIcon">
      </div>
      <div id="usernameTextbox">
        <input type="text" name="username" class="tbUsername" placeholder="Username">
      </div>

    </div>
    <div id="password">

    </div>
    <div id="btnLogin">

    </div>
  </form>
</div>

尝试将 position 更改为 absolute。很简单,把它放在有 margin-top:10%; 的元素上,你会看到它起作用了!