【问题标题】:How to align elements to the center?如何将元素对齐到中心?
【发布时间】:2018-05-16 16:00:10
【问题描述】:

我有两个问题。

首先,

我有一个表单,我正在尝试将所有内容置于其中心,同时将文本与字段的左侧对齐。

我尝试将 justify-center 和 align-items 赋予“container”类和“form-style”类,但没有任何效果。

部分代码和Codepen链接

.form-style {
  width: 50%;
  position: relative;

   display: flex;
  flex-flow: column wrap;
  justify-content: center;
  align-items: center;

还有,

.container {
  display: flex;
  flex-flow: column wrap;
  justify-content: center;
  align-items: center;
  }

第二,

我正在尝试让我的提交按钮获得 100% 的宽度并且什么都没有 - 两侧有空间,

input[type="submit"] {
  display: block;

  background-color: #08ABD9;
  font-family: 'jura', sans-serif;
  font-size: 16pt;
  font-weight: bold;

  height: 40px;
  border: none;
  margin-top: 40px;
  margin-bottom: 0px;
  width: 100%;

    }

【问题讨论】:

  • 您是否尝试将表单标签对齐到表单输入的左侧和顶部?
  • 如果你看看我的 codepen,现在它是最接近的(只需要去掉右侧的多余空间)。但必须有更好的方法来做到这一点......
  • 是的,标签、图例等'

标签: html forms css flexbox


【解决方案1】:

从这里开始:您的代码中存在语法错误。由于存在不可见字符,无法识别容器上的 display: flex。所有的 flex 属性都不起作用。

所以首先解决这个问题。只需完全删除该行并重新编写display: flex。您会注意到布局发生了重大变化。

其次,您的标签/输入容器上的flex-direction 设置为column。这会垂直堆叠您的表单元素。

#fname, #email-a, #phone, #dropdown, #mrole, #age, #textbox {
    display: flex;
    flex-flow: column wrap;
    width: 100%;
    margin-left: 40px;
}

如果您希望文本和输入连续排列,请使用flex-direction: row

最后,关于您的提交按钮,您的容器具有左右填充,可防止按钮到达边缘。

.container {
    width: 100%;
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}

删除填充规则后,按钮将从边缘延伸到边缘。

【讨论】:

  • 如果我放行,它不会看起来像
  • ...将文本与字段的左侧对齐。
  • 另外,我发现了另一个问题。这可能是主要问题。答案已修改。
  • 1.你是怎么检查的?我想学习。 2.我怎么看那个错误,我不明白.... 3.现在所有的字段都在中间,但标签在左侧而不是在字段的左侧
  • 搜索“Chrome 开发者工具”或“Firefox Inspector”。
猜你喜欢
  • 2013-01-08
  • 2011-12-04
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多