您知道为什么项目没有在中心垂直对齐吗?

时间:2018-11-23 12:32:16

标签: css twitter-bootstrap

我想要一种具有页眉和照片的布局,并且在页眉和页脚之间有一些带有帖子的列表项。我正在使用引导程序,我希望标题看起来像这样:

enter image description here

但它不起作用,其显示如下:https://jsfiddle.net/83vnj5ru/2/

搜索表单和过滤器没有在中心垂直对齐吗?例如,搜索表单上方的空间大于搜索表单下方的空间。你知道为什么吗?

另外,我是一个初学者,我不知道代码是否正确或是否可以改进。因为这种布局看起来并不复杂,但是我在另一个.row中有一个.row,而在其他.col中也有.col,所以实现这种布局的代码看起来似乎并不复杂。

HTML:

<div class="container py-5">
  <div class="row">
    <div class="col">
      <ul class="list-group forum">
        <li class="list-group-item bg-custom-light2 py-4">
          <div class="row">
            <form class="col-5">
              <div class="form-group">
                <div class="input-group">
                  <div class="input-group-prepend">
                    <span class="input-group-text"><i class="fa fa-search"></i></span>
                  </div>
                  <input type="email" class="form-control" id="exampleInputEmail1"
                         aria-describedby="emailHelp" placeholder="Search">
                </div>
              </div>
            </form>
          </div>
          <div class="col-7 text-right">
            <div class="dropdown">
              <a class="dropdown-toggle"  id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Filter 1
              </a>
              <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" href="#">item1</a>
              </div>
            </div>
            <div class="dropdown">
              <a class="dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Filter 2
              </a>
              <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" href="#">item1</a>
              </div>
            </div>
          </div>
        </li>
        <li class="list-group-item">list item 1</li>
        <li class="list-group-item">list item 2</li>

        <li class="list-group-item bg-custom-light2 py-4">container footer</li>
      </ul>
    </div>
  </div>
</div>

CSS

.bg-custom-light2{
  background-color: #fafafa;
}
.input-group-text, .input-group-prepend{
  background-color:none;
}
.input-group-text{
  color:gray;
}

3 个答案:

答案 0 :(得分:2)

因为默认的引导程序类.form-group带有margin-bottom的默认1em

也可以使用align-items:center垂直对齐项目

.form-group {
    margin-bottom: 0!important;
}
.row{
  align-items:center;
  }

js小提琴:https://jsfiddle.net/83vnj5ru/3/

答案 1 :(得分:1)

            <div class="container py-5">
              <div class="row">
                <div class="col">
                  <ul class="list-group forum">
                    <li class="list-group-item bg-custom-light2 py-4">
                      <div class="row align-items-center">
                        <form class="col-5">
                          <div class="form-group mb-0">
                            <div class="input-group">
                              <div class="input-group-prepend">
                                <span class="input-group-text search-icon"><i class="fa fa-search"></i></span>
                              </div>
                              <input type="email" class="form-control search-input" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Search">
                            </div>
                          </div>
                        </form>
                      <div class="col-7 text-right">
                        <div class="dropdown filters">
                          <a class="dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Filter 1
                          </a>
                          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                            <a class="dropdown-item" href="#">item1</a>
                          </div>
                        </div>
                        <div class="dropdown filters">
                          <a class="dropdown-toggle" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                          Filter 2
                          </a>
                          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                            <a class="dropdown-item" href="#">item1</a>
                          </div>
                        </div>
                      </div>
                    </div>
                    </li>
                    <li class="list-group-item">list item 1</li>
                    <li class="list-group-item">list item 2</li>

                    <li class="list-group-item bg-custom-light2 py-4">container footer</li>
                  </ul>
                </div>
              </div>
            </div>

我将align-items-center用于搜索和过滤器行。和form-group具有我已被mb-0删除的默认边距。现在它们可以按您想要的中心对齐。

答案 2 :(得分:0)

pls在下面的附加类下面添加:

    additional class - align-items-center - vertically center
        <div class="row align-items-center">

                  </div>
additional class - mb-0 - margin remove
<div class="col-5"><div class="form-group mb-0"></div></div>

更新后的答案

you should apply below style : form vertically center
    <style>
    body{
    display: flex;
    align-items: center;
    height: 100vh;
    }
    </style>