javascript - 如何将一个css div框直接放在另一个css div框下面

标签 javascript html css

我需要直接位于“usernamealert”div 下的“usernamevalidation”div。

.usernamealert {
  background-color: #262626;
  text-align: center;
  margin: auto;
  height: 60px;
  width: 20%;
}

.usernamevalidation {
  background-color: #262626;
  text-align: left;
  margin: auto;
  height: 150px;
  width: 30%;
}
<div class="usernamealert">
  <p style="font-family:Impact; color:Red; font-size:28px;">ALERT</p>
</div>
<div class="usernamevalidation">
  <p style="font-family:Impact; color:White; font-size:28px;">Username should contain:
  </p>
  <p style="font-family:Impact; color:White; font-size:24px;">- At least 5 characters
  </p>
  <p style="font-family:Impact; color:White; font-size:24px;">- Letters and integers only</p>
</div>

它的意思是看起来像这样: enter image description here

最佳答案

每个<p>默认情况下,元素在顶部有一些边距。第一个<p>你的第二个 div 就是造成这个差距的那个。

.usernamealert {
  background-color: #262626;
  text-align: center;
  margin: auto;
  height: 60px;
  width: 20%;
}

.usernamevalidation {
  background-color: #262626;
  text-align: left;
  margin: auto;
  height: 150px;
  width: 30%;
}

<div class="usernamealert">
  <p style="font-family:Impact; color:Red; font-size:28px;">ALERT</p>
</div>
<div class="usernamevalidation">
  <p style="font-family:Impact; color:White; font-size:28px; margin-top:0">Username should contain:
  </p>
  <p style="font-family:Impact; color:White; font-size:24px;">- At least 5 characters
  </p>
  <p style="font-family:Impact; color:White; font-size:24px;">- Letters and integers only</p>
</div>

关于javascript - 如何将一个css div框直接放在另一个css div框下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53109374/

相关文章:

Javascript:Textarea到html,如何在html中保存精确的空格数?

jquery - 使用 jquery 控制 div 大小

javascript - 如何将自动完成的选定值 id 传递给 jquery 中的数据属性

javascript - 为什么我在 node.js 中使用 parseInt 会得到奇怪的结果? (与 chrome js 控制台的结果不同)

javascript - 我怎样才能使我的功能更有效率?

javascript - 来自多个来源的 jQuery 动画 - 影响多个对象

javascript - 向 javascript 数组元素添加属性

c# - 无法对测试运行 'xxxx 2011-10-20 13:00:00' 进行排队。您没有执行该操作的适当权限

css - 悬停后渐变动画在循环中保持颜色

使用 IE9 仿真模式在 IE9 中加载 CSS 而不是 IE11