无法使用flexbox垂直对齐div

时间:2016-12-31 16:39:21

标签: html css flexbox

这是我的基本HTML:

 <div class="col-lg-12">
                <div class="col-lg-4 center-block main-text center-text marg-small boxed">
                <div class="random">Some Text</div>  
                    <div class="col-lg-6 marg-small ">
                    <div  id="pick_channel" class="boxed orange">
                    <div class="square vertical">Channel</div>
                    </div>
                      <div id="pick_objective" class="boxed orange">
                      <div class="square vertical">Objective</div>
                      </div>
                </div>
                </div>
                </div>

相关的CSS:

.boxed{
display:flex;
flex-direction: column;
flex-wrap:nowrap;
align-items: center;
}

.square {
height:100px;
width:100%;
display:flex;
align-self:center;
margin-bottom: 10px;
}
.vertical{
}

我试图将id s pick_channelpick_objective作为矩形与文本&#34;频道&#34;和&#34;目标&#34;垂直和水平居中,各自为divs。我已经尝试了很多组合,但无法弄明白。

2 个答案:

答案 0 :(得分:1)

你去吧

.boxed {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  height: 100px;
  width: 100%;
  margin: 10px 0;
}
.square {
  margin-bottom: 10px;
  width: 100%;
  text-align: center;
}
.orange {
  background: orange;
}
.center_content {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}
<div class="col-lg-4 center_content">
  <div class="col-lg-6 marg-small ">
    <div id="pick_channel" class="boxed orange">
      <div class="square vertical">Channel</div>
    </div>
    <div id="pick_objective" class="boxed orange">
      <div class="square vertical">Objective</div>
    </div>
  </div>
</div>

答案 1 :(得分:1)

这样的事情:

&#13;
&#13;
.boxed {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: center;
}
.square {
  height: 100px;
  width: 100px;
  /* display: flex; */
  /* align-self: center; */
  margin-bottom: 10px;
  border: 1px solid gray;
  text-align: center;
  line-height: 100px;
}
&#13;
<div class="col-lg-12">
  <div class="col-lg-4 center-block main-text center-text marg-small boxed">
    <div class="random">Some Text</div>
    <div class="col-lg-6 marg-small ">
      <div id="pick_channel" class="boxed orange">
        <div class="square vertical">Channel</div>
      </div>
      <div id="pick_objective" class="boxed orange">
        <div class="square vertical">Objective</div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;