垂直div居中

时间:2017-01-25 13:26:31

标签: html css centering

我试图垂直对齐一个div。我的目标是拥有3个高度的div:100%和宽度:33%有一个居中的内容div。有人可以帮忙吗?这是代码:

.wrapper {
  height: 100%;
  width: 100%;
}
.window {
  width: 33%;
  float: left;
  background-color: #666;
}
.windowContent {
  background-color: #000;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <div class="wrapper">
    <div class="window">
      <div class="windowContent">
      </div>
    </div>

    <div class="window">
      <div class="windowContent">
      </div>
    </div>

    <div class="window">
      <div class="windowContent">
      </div>
    </div>

  </div>

</body>

</html>

1 个答案:

答案 0 :(得分:1)

试试这个例子

<div class="wrapper">
    <div class="window">
        <div class="windowContent">
        1
        </div>
    </div>

    <div class="window">
        <div class="windowContent">
        2
        </div>
    </div>

<div class="window">
        <div class="windowContent">
        3
        </div>
    </div>

</div>

CSS

.wrapper
    {
      height: 100%;
      width: 100%;
      display: flex;
    }

    .window
    {
      width: 100%;
      min-height: 200px;
      background-color: #666;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .windowContent
    {
      background-color: #000;
      color: #fff;
    }

演示 - https://jsfiddle.net/xa3v1c3o/