如何使元素垂直居中?

时间:2018-01-30 01:52:27

标签: javascript html css frontend bootstrap-4

http://jsfiddle.net/m9121mxt/ 这是我的情况。我希望按钮保持在viewmorebutton元素的中心。我尝试了margin:auto;text-align: center;,但他们没有工作。如何将中间的按钮居中对齐? 谢谢!



<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!--facebook icon-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<style>
.thirdpage{
    height: 90vh;
    background-color: #101427;
}

.thirdoutter{
    position: relative;
    margin: 0 auto;
    width: 80%;
    height: 100%;
}
.viewmorebutton{
    margin:auto;
    height: 80%;
    text-align: center;
}
</style>

<div class="thirdpage">
    <div class="thirdoutter">
        <div class="viewmorebutton">
            <button type="button" class="btn btn-secondary col-sm-4">VIEW MORE</button>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

如果您只是将类d-flex align-items-center justify-content-center添加到按钮周围的div,那么您不需要任何自定义CSS,因为那些 Bootstrap 4 类将完成这项工作。毕竟,既然你正在加载Bootstrap 4,你也可以使用它。

以下是工作代码段(点击下面的“运行代码段”按钮):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<style>
.thirdpage{
    height: 90vh;
    background-color: #101427;
}

.thirdoutter{
    position: relative;
    margin: 0 auto;
    width: 80%;
    height: 100%;
}
.viewmorebutton{
    margin:auto;
    height: 100%;
    text-align: center;
}
</style>

<div class="thirdpage">
    <div class="thirdoutter">
        <div class="viewmorebutton d-flex align-items-center justify-content-center">
            <button type="button" class="btn btn-secondary col-sm-4">VIEW MORE</button>
        </div>
    </div>
</div>

答案 1 :(得分:0)

将div中的项目居中的一种方法是将显示设置为表格.thirdoutter并将表格单元格显示为.viewmorbutton

&#13;
&#13;
.thirdpage{
	height: 90vh;
	background-color: #101427;
}

.thirdoutter{
  display: table;
	position: relative;
    margin: 0 auto;
    width: 80%;
    height: 100%;
}
.viewmorebutton{
  display: table-cell;
  vertical-align: middle;
  text-align: center;
	margin:auto;
	height: 80%;
}
.largercavas{
	height: 100%;
	width: 50%;
	object-fit: cover;
}
.largerimg{
	width: 100%;
	max-height: 100%;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>


<div class="thirdpage">
  <div class="thirdoutter">
    <div class="viewmorebutton">
      <button type="button" class="btn btn-secondary col-sm-4">VIEW MORE</button>
    </div>	
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你给.thirdoutter一个相对的位置,这是使它成为.viewmorebutton

的绝对容器所需要的。
.viewmorebutton {
   position: absolute;
   top: 50%;
   transform: translate(0, -50%) }

position: absolute   will be in relation to its parent container

top: 50%    will vertically centre item

transform: translate (0, -50%)   will move the item itself up by half its own height to fully centre it