Flexbox align-self在我的布局

时间:2016-02-08 02:59:06

标签: css css3 flexbox

我试图将#view-ctrls-cntnr水平居中于.menubar。当我使用align-self:center时,它似乎没有做任何事情。我做错了什么,有更好的方法吗? JSFiddle

HTML

<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<section class="analysis">
  <div class="menubar">

    <div class="dropdown" id="file-btn">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li><a href="#">Action</a></li>
        <li><a href="#">Another action</a></li>
        <li><a href="#">Something else here</a></li>
        <li role="separator" class="divider"></li>
        <li><a href="#">Separated link</a></li>
        <li role="separator" class="divider"></li>
        <li><a href="#">One more separated link</a></li>
      </ul>
    </div>

    <div id="view-ctrls-cntnr">
      <div class="btn-group" role="group">
        <button class="btn btn-default" ng-class="{'active-view': active_views[0]}" ng-click="toggleView(0)">R-Theta</button>
        <button class="btn btn-default" ng-class="{'active-view': active_views[1]}" ng-click="toggleView(1)">Cartesian</button>
        <button class="btn btn-default" ng-class="{'active-view': active_views[2]}" ng-click="toggleView(2)">Longitudinal</button>
        <button class="btn btn-default" ng-class="{'active-view': active_views[3]}" ng-click="">Console</button>
      </div>
    </div>
  </div>
  <div id="views-cntnr">
    <div id="r1" class="view-row">
      <div id="v1" class="view">V1</div>
      <div id="v2" class="view">V2</div>
      <div id="v3" class="view">V3</div>
    </div>
    <div id="r2" class="view-row">
      <div id="v4" class="view">V4</div>
    </div>
  </div>
  <div id="frame-ctrl-cntnr">
    <div id="frame-num" class="frame-ctrl"># X</div>
    <div id="frame-range-cntnr" class="frame-ctrl">
      <input type="range">
    </div>
  </div>
</section>

CSS

html,
body {
  height: 100%;
}

.analysis {
  display: flex;
  flex-direction: column;
  height: 100%;
}


/* MENUBAR */

.menubar {
  padding: 4px 0 4px;
  background-color: #eee;
  border: hsl(0, 0%, 75%) solid 1px;
  border-right: none;
  border-left: none;
  display: flex;
}

#view-ctrls-cntnr {
  align-self: center;
}

#file-btn a {
  color: black;
  text-decoration:none
}

/* menubar */


/* VIEWS */

#views-cntnr {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}


/* ROWS */


/* ROW 1 */

#r1 {
  display: flex;
  flex-grow: 4;
}

#r1 .view {
  flex-grow: 1;
  border: black 1px solid;
  border-top: none;
  border-right: none;
}

#r1 .view:last-child {
  border-right: black 1px solid;
}


/* row 1 */


/* ROW 2 */

#r2 .view {
  border: black 1px solid;
  border-top: none;
  flex-grow: 1;
}

#r2 {
  display: flex;
  flex-grow: 1;
}


/* row 2 */


/* rows */


/* views */


/* FRAME CTRL */

#frame-ctrl-cntnr {
  display: flex;
}

.frame-ctrl {
  border: black 1px solid;
  border-top: none;
  border-right: none;
}

.frame-ctrl:last-child {
  border-right: black 1px solid;
}

#frame-num {
  width: 50px;
}

#frame-range-cntnr {
  flex-grow: 1;
  padding: 4px;
}


/* frame ctrl */

1 个答案:

答案 0 :(得分:2)

您可以使用嵌套的flexbox来代替内部元素。

<强> jsFiddle

#view-ctrls-cntnr {
  flex: 1;
  display: flex;
  justify-content: center;
}

或者您可以使用保证金技巧:

<强> jsFiddle

#view-ctrls-cntnr {
  margin-left: auto;
  margin-right: auto;
}

注意,#view-ctrls-cntnr不是.view-ctrls-cntnr

编辑:将其置于整个视口宽度的中心,您可以将同级元素设置为绝对位置:#file-btn {position: absolute;}这可能会导致小视口宽度重叠。

<强> jsFiddle

除此之外,您可以为左侧兄弟提供固定宽度,即80px,并在容器上添加一个伪:after元素,并设置相同的宽度。这样左右两侧就会有相同的空间。

.menubar:after {
  content: "";
  width: 80px;
}
#file-btn {
  width: 80px;
}

<强> jsFiddle