首页 > 解决方案 > flexbox 中的 align-self 属性不起作用

问题描述

我想让 item-2 居中,我无法让 align-self 工作。请问我做错了什么?我已经尝试了 align-self 的所有选项,但它似乎没有注册。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Flex Align</title>
  <style>
    #container {
      background: #555 ;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-evenly;
      height: 600px;
      align-items: baseline;
      align-content: space-between;
    }

    .item-2 {
      align-self: center;
    }  
    
    .item {
      background: #f4f4f4;
      border: #ccc solid 1px;
      padding: 1rem;
      text-align: center;
      margin: 0.5rem;
      flex-basis: 200px;
  }
  </style>
</head>
<body>
  <div id="container">
    <div class="item item-1"><h3>Item 1</h3></div>
    <div class="item item-2"><h3>Item 2</h3></div>
    <div class="item item-3"><h3>Item 3</h3></div>
  </div>
</body>
</html>

标签: cssflexboxalignment

解决方案


如果您align-content: space-between从容器中删除 并为 .item 添加高度,则您将能够选择单个项目并将align-self: center该项目垂直设置在中间。


推荐阅读