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

时间:2016-09-01 16:56:36

标签: html css margin

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

Here是目前的实际情况示例,因为您可以看到我的用户名区域位于我的内容div的顶部。

我想要的只是让它有点让步。关于为什么或如何解决这个问题的任何想法?

CODE

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>

2 个答案:

答案 0 :(得分:2)

padding-top:1px

中设置#content

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

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