【问题标题】:Center form content vertically垂直居中表单内容
【发布时间】:2017-12-13 04:19:10
【问题描述】:

我有the following landing page form(我已经删除了所有我认为无关紧要的不必要的东西,只留下了最少的样式)。这是代码(我使用的是 Bootstrap 3.3.5):

<div class="col-md-6 app-door">
  <form class="image-hover text-center">
    <h3>Application name</h3>
    <div class="landing-page-form-group">
      <label class="landing-page-label">Label: </label>
      <input class="form-control landing-page-input">
    </div>
    <button class="btn">Submit</button>
  </form>
  <div class="center-block app-link some-img"/>
</div>

..和css

.image-hover {
  width: 300px;
  height: 300px;
  background-color: rgba(0, 0, 0, 0.7) !important;
  position: absolute;
  z-index: 100;
  color: #cccccc;
  margin: 0 auto;
  left: 0;
  right: 0;
  border-radius: 25px;
}

.landing-page-form-group {
  padding-top: 16px;
  padding-bottom: 16px;
}

.landing-page-label {
  width: 100px;
  vertical-align: middle;
}

.landing-page-input {
  width: 150px;
  display: inline-block;
}

.app-link {
  width: 300px;
  height: 300px;
  background-size: 100% 100%;
  border-radius: 25px;
}

.some-img {
  background-image: url("http://dl.hiapphere.com/data/icon/201511/HiAppHere_com_com.ludicside.mrsquare.png");
}

我想将所有表单输入等垂直居中。我已经尝试过诸如使用位置(相对于父级,绝对在子级)、垂直对齐、顶部、也许我现在不记得的其他东西,因为我花了很多时间尝试。

我不是前端开发(我的意思是 html/css)方面的出色专家,所以我会非常高兴看到理解,尽管问题似乎足够基本。

感谢您的任何想法!

【问题讨论】:

  • 附上它现在的样子可能会有所帮助,如果你能做一个小提琴就更好了。
  • @Bemmu 请看the following landing page form文字下的第一个链接

标签: html css twitter-bootstrap alignment vertical-alignment


【解决方案1】:

更新 检查没有flex demo的解决方案here

试试这个

查看演示here

将以下样式添加到您现有的 .image-hover 类中

CSS:

.image-hover {
    align-items: center;
    display: flex;
    flex-direction: column;
    justify-content: center; 
}

【讨论】:

  • 我完全忘了说我必须支持 IE 11。还有其他解决方案吗?我会更新答案
  • @DmitrySenkovich flex 运行良好,直到 IE11 检查 here
  • 太棒了!非常感谢!
  • @DmitrySenkovich 我已经更新了没有 flex 的解决方案。请检查答案中更新的演示链接
  • 哇,它有效!看起来很..怎么说呢..庄严:)
【解决方案2】:

这是我的解决方案: https://jsfiddle.net/0x9epxah/1/

添加 div .inner-form:

<div class="col-md-6 app-door">
  <form class="image-hover text-center">
    <div class="inner-form">
      <h3>Application name</h3>
      <div class="landing-page-form-group">
        <label class="landing-page-label">Label: </label>
        <input class="form-control landing-page-input">
      </div>
      <button class="btn">Submit</button>
    </div>
  </form>
  <div class="center-block app-link some-img"/>
</div>

还有这个 css 类:

.inner-form {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
}

【讨论】:

    【解决方案3】:

    最简单的方法是使用

    .image-hover {
        display: flex;
        align-items : center;
        justify-content: center;
    }
    

    align-items 负责垂直居中,justify-contents 负责水平居中。

    【讨论】:

    • 其他人是第一个,我能做的就是你的回答,谢谢!
    • 啊,没问题。无论如何,这是解决方案,@dmitry-senkovich — jsfiddle.net/0x9epxah/2
    • 不过好像有点破o_0
    • 试试这个? jsfiddle.net/0x9epxah/4如果字段在中心,它会覆盖笑脸,只是一个观察。
    • 这很有趣,但是按钮需要放在所有输入组的下方
    猜你喜欢
    • 1970-01-01
    • 2014-06-28
    • 2016-09-10
    • 2017-08-16
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 2014-11-28
    相关资源
    最近更新 更多