Style alignSelf 属性
居中对齐灵活元素内的某个项目:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>蜜蜂教程(mifengjc.com)</title>
<style>
#main {
width: 220px;
height: 300px;
border: 1px solid black;
display: flex;
align-items: flex-start;
}
#main div {
flex: 1;
}
#myBlueDiv {
align-self: center;
}
</style>
</head>
<body>
<div id="main">
<div style="background-color:coral;">RED</div>
<div style="background-color:lightblue;" id="myBlueDiv">BLUE</div>
<div style="background-color:lightgreen;">Green div with more content.</div>
</div>
<p>点击“尝试一下”按钮设置 alignSelf 属性的值为 "stretch"。</p>
<button onclick="myFunction()">尝试一下</button>
<script>
function myFunction() {
document.getElementById("myBlueDiv").style.alignSelf = "stretch";
}
</script>
<p><b>注意:</b>Internet Explorer 和 Safari 不支持 alignSelf 属性。</p>
</body>
</html>
定义和用法
alignSelf 属性规定灵活容器内被选中项目的对齐方式。
注意:alignSelf 属性可重写灵活容器的 alignItems 属性。
浏览器支持
![]()
![]()
![]()
![]()
![]()
Firefox、Opera 和 Chrome 支持 alignSelf 属性。
语法
返回 alignSelf 属性:
object.style.alignSelf
设置 alignSelf 属性:
object.style.alignSelf = "auto|stretch|center|flex-start|flex-end|baseline|initial|inherit"
属性值
| 值 | 描述 |
|---|---|
| auto | 默认值。元素继承了它的父容器的 align-items 属性。如果没有父容器则为 "stretch"。 |
| stretch | 元素被拉伸以适应容器。 |
| center | 元素位于容器的中心。 |
| flex-start | 元素位于容器的开头。 |
| flex-end | 元素位于容器的结尾。 |
| baseline | 元素位于容器的基线上。 |
| initial | 设置该属性为它的默认值。请参阅 initial。 |
| inherit | 从父元素继承该属性。请参阅 inherit。 |
技术细节
| 默认值: | auto |
|---|---|
| 返回值: | 字符串,表示元素的 align-self 属性。 |
| CSS 版本 | CSS3 |
相关文章
CSS 参考手册:align-self 属性
HTML DOM STYLE 参考手册:alignContent 属性
HTML DOM STYLE 参考手册:alignItems 属性
Style 对象
Style 对象