html - 文本对齐 : center and align-items: center not working horizontally

标签 html css responsive-design flexbox

我以前从未遇到过这种情况。我在各种地方尝试了 text-align: center 但它们都不起作用。它们垂直工作,但不水平工作。我试图让它在每个盒子的水平和垂直方向上工作。

这是我的代码:

.boxes {
  height:100%;
}
.box {
  width: 33%;
  height: 20%;
  display: -webkit-flex;
}
.box p {
  display: flex;
  align-items: center;
}
.box1 {
  background: magenta; 
}
.box2 {
  background: cyan;
}
.box3 {
  background: yellow;
}
.box4 {
  background: orange;
}
.box5 {
  background: purple;
}
* { 
  margin:0;
  padding:0;
}
html, body {
  height: 100%; 
}
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="tabletest.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div class="boxes">
      <div class="box box1"><p>Box 1</p></div>
      <div class="box box2"><p>Box 2</p></div>
      <div class="box box3"><p>Box 3</p></div>
      <div class="box box4"><p>Box 4</p></div>
      <div class="box box5"><p>Box 5</p></div>
    </div>
  </body>
</html>

我也在尝试坚持使用百分比来进行响应式设计。

编辑:这似乎与另一篇文章重复,但我的问题是如何让文本直接居中对齐(垂直和水平),同时保持框的顺序。

最佳答案

您可以通过 flexbox 使用此解决方案。

改变了什么?

* { 
  margin: 0;
  padding: 0;
}
html, body {
  height: 100%; 
}

.boxes {
  height: 100%;
}
.box {
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 20%;
  justify-content: center;
  width: 33%;
}
.box1 {
  background: magenta; 
}
.box2 { 
  background: cyan;
}
.box3 {
  background: yellow;
}
.box4 {
  background: orange;
}
.box5 { 
  background: purple;
}
<div class="boxes">
  <div class="box box1"><p>Box 1</p></div>
  <div class="box box2"><p>Box 2</p></div>
  <div class="box box3"><p>Box 3</p></div>
  <div class="box box4"><p>Box 4</p></div>
  <div class="box box5"><p>Box 5</p></div>
</div>

关于html - 文本对齐 : center and align-items: center not working horizontally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438699/

相关文章:

javascript - 使用 Bootstrap 3 和 Bootstrap 2

html - Bootstrap 填充 cols 之间的空白

javascript - 使用 margin-top 定位图像

html - Visual Composer 文本对齐

responsive-design - WooRank 触摸屏就绪失败

html - 在 3 个 Bootstrap 列上包含具有相同高度的 div

c# - 如何从 HttpModule 中检索响应 html?

html - 导航菜单问题

html - Bootstrap : responsive div in between two fixed div elements

css - 响应式选择框