html - Flexbox align-self 被忽略

标签 html css flexbox

align-self 的文档状态:

The align-self CSS property aligns flex items of the current flex line overriding the align-items value.

然而,这不会发生:

.example {
  display: flex;
  align-items: center;
  border: 1px solid blue;
  height: 8ex;
}

.example > div {
  border: 1px solid red;
}

#a > div:last-child {
  align-self: top;
}

#b > div:last-child {
  align-self: center;
}

#c > div:last-child {
  align-self: bottom;
}
<body>
<div id="a" class="example"><div>1</div><div>2</div></div>
<div id="b" class="example"><div>1</div><div>2</div></div>
<div id="c" class="example"><div>1</div><div>2</div></div>
</body>

文档还说:

If any of the item's cross-axis margin is set to auto, then align-self is ignored. The property doesn't apply to block-level boxes, or to table cells.

但是,这些都不起作用:

  • 向子 div 显式添加 margin:0
  • display:inline 添加到子 div
  • div 更改为 span

为什么 align-self 不起作用?

最佳答案

topbottom 不是 align-self 的有效值。使用 flex-startflex-end 代替:

.example {
  display: flex;
  align-items: center;
  border: 1px solid blue;
  height: 8ex;
}

.example > div {
  border: 1px solid red;
}

#a > div:last-child {
  align-self: flex-start;
}

#b > div:last-child {
  align-self: center;
}

#c > div:last-child {
  align-self: flex-end;
}
<body>
<div id="a" class="example"><div>1</div><div>2</div></div>
<div id="b" class="example"><div>1</div><div>2</div></div>
<div id="c" class="example"><div>1</div><div>2</div></div>
</body>

关于html - Flexbox align-self 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396753/

相关文章:

java - 如何防止单击 <h :commandLInk> 时页面刷新

javascript - 访问通过 xhr 请求获取的 HTML 源中所有脚本标签中的变量

javascript - 短 html/css 语法 : <div a b c></div> - any reasons why not?

html - 如何阻止 flex 元素离开容器

html - &lt;!DOCTYPE html> 打破 flexbox flex

html - 显示 flex 创造额外的空间 为什么

php - 是否可以使用下载属性强制下载?

html - 锚定页面?

html - 摆脱自举网格边缘

c# - 如何获取 asp.net 页面上元素的样式