关于css:align-content:中心在Chrome中不起作用

align-content: center not working in Chrome

我正在尝试通过使用align-content:center将divs与flexbox垂直对齐。 它在Firefox和IE中工作正常,但在Chrome中无法垂直对齐。 在Chrome中,div保持在最前面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>

和CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.products-wrapper {
    width: 100%;
    min-height: 100vh;
    /* FLEX */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
}

.product {
    max-width: 250px;
    padding: 10px;
}

.product img {
    display: block;
    margin: 0 auto;
    max-height: 250px;
    max-width: 250px;
}

我也一直在尝试align-items:center; 可以使div垂直居中,但是例如如果我有不同数量的文本,它就不会与图像和文本成一条直线。

有解决方案的人如何在所有浏览器中都可以使div垂直居中(直线)吗?

图片示例:

Image example

编辑:如果不需要高度是动态的,请对齐项目:居中; (例如)最大高度:400像素; 在.product div上 虽然不是我真正想要的。


为了实现所需的功能,您需要使用align-items: flex-start,这样所有项目都将对齐到其行的顶部。 要垂直居中,您需要寻找align-items: center属性,该属性沿横轴对齐flex容器中的项目,在您的情况下,您必须在外包装中使用该属性以确保products div 位于页面中心。

align-content: center属性仅在多于一行(包装的内容)时才起作用。

关于这两个属性之间差异的更好解释是对此stackoverflow问题的回答

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.outer-wrapper {
  width: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.products-wrapper {
    width: 100%;
    /* FLEX */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    background-color: gray;
    align-items: flex-start;
}

.product {
    max-width: 250px;
    padding: 10px;
    background-color: red;
    margin: 5px;
}

.product img {
    display: block;
    margin: 0 auto;
    max-height: 250px;
    max-width: 250px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  JS Bin
</head>
<body>


     
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/St._Bernard_puppy.jpg/120px-St._Bernard_puppy.jpg" alt="Temporary">
          Title
          <p><center>[wp_ad_camp_4]</center></p><p>
Text.
</p>
     


</body>
</html>


制作.product元素,伸缩容器。 这样,您就可以在其中对齐内容。

1
2
3
4
5
6
7
.product {
    max-width: 250px;
    padding: 10px;
    display: flex;
    justify-content: center;
    flex-direction: column;
}

希望这可以帮助 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
.products-wrapper {
    width: 100%;
    min-height: 100vh;
    /* FLEX */
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
}

.product {
    max-width: 250px;
    padding: 10px;
    display: flex;
    justify-content: center;
    flex-direction: column;
}

.product img {
    display: block;
    margin: 0 auto;
    max-height: 250px;
    max-width: 250px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>
     
     
          <img src="images/temporary.jpg" alt="Temporary">
          Title
          <p>
Text.
</p>