html - CSS Flex 将元素堆叠在一起

标签 html css angular flexbox angular-material

<分区>

我正在尝试放置一些 <h1></h1>文本和来自 Angular Material 的电子邮件表单,放在 <div></div> 的中心具有彩色背景的部分。这些元素堆叠在一起,就像它们是层一样。电子邮件表格需要位于 <h1></h1> 下方标签。我能让它正确对齐的唯一方法是使用 position:flex ,我怀疑这是根本原因。

这是我的 html 和 css:

<div class="top-section">

  <h1 class="top-h1">
    mytitle
  </h1>

  <md-input-container class="email-form" color="accent">
    <input mdInput placeholder="Email us" value="Your email address">
  </md-input-container>

</div>


.top-section {
  height: 350px;
  background-color: #04041b;
  align-items: center;
  display: flex;
  justify-content: center;
}

.top-h1 {
  color: #E8E8E8;
  font-size: 60px;
  position: absolute;
}

.email-form {
  color: white;
  font-size: 30px;
}

有什么想法吗?

最佳答案

您在 h1 上使用 position: absolute 将其从页面流中移除,并使其相对于最近定位的父级定位。所以其他元素不会在它周围流动。删除它。然后您的元素将并排显示,因为 flex 父项的默认 flex-directionrow。要垂直显示元素,请使用 flex-direction: column,这些元素将在一列中一个接一个地堆叠,而不是并排成一行。

.top-section {
  height: 350px;
  background-color: #04041b;
  align-items: center;
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.top-h1 {
  color: #E8E8E8;
  font-size: 60px;
}

.email-form {
  color: white;
  font-size: 30px;
}
<div class="top-section">

  <h1 class="top-h1">
    mytitle
  </h1>

  <md-input-container class="email-form" color="accent">
    <input mdInput placeholder="Email us" value="Your email address">
  </md-input-container>

</div>

关于html - CSS Flex 将元素堆叠在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44621094/

相关文章:

angular - papa 可以解析的文件大小的限制是多少?

javascript - Ionic2:Ion Infinite 滚动条在 iPhone 中不起作用

javascript - Angular2 ReactiveFormsModule 意外的模块

html - 如果单个标签如何添加类?

javascript - 修复 iOS 上的悬停行为

javascript - 如何从 HTML blob : URL? 下载 mp4 视频

css - 使用 LESS 构建选择器列表

html - wkhtmltopdf 分页后有空格

html - 基于比文本最大宽度更大的宽度居中文本

html - 我如何通过 wrapper -css grid 将所有内容对齐到中心