HTML DOM 样式 alignSelf 属性

htmlweb developmentfront end technology更新于 2024/5/26 15:08:17

HTML DOM alignSelf 属性用于指定灵活容器内的特定项目对齐方式。alignSelf 属性用于覆盖元素网格或弹性显示布局中指定的 align-items 值。

语法

以下是语法 −

设置 alignSelf 属性 −

object.style.alignSelf = "auto|stretch|center|flex-start|flex-end|baseline|initial|inherit"

值

以下是 alignSelf 属性的值 −

值描述
Stretch这是默认值,用于拉伸项目以适应容器。
Center用于将项目定位在容器的中心。
flex-start将项目定位在容器的开头。
flex-end将项目定位在容器的末尾容器。
baseline将项目定位到容器基线。
initial将此属性设置为初始值。
Inherit继承父级属性值。

示例

让我们看一下 Style alignSelf 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   #container {
      width: 180px;
      height: 220px;
      padding: 10px;
      border: 1px solid #333;
      display: flex;
      align-items:baseline;
      flex-flow: row wrap;
   }
   .ele {
      width: 60px;
      height: 60px;
      background-color: skyblue;
   }
   .ele:nth-child(2n) {
      background-color: orange;
   }
</style>
<script>
   function changeAlign(){
      document.getElementsByClassName("ele")[1].style.alignSelf="flex-end";
   }
</script>
</head>
<body>
<h2>Demo Heading</h2>
<div id="container">
<div class="ele"></div>
<div class="ele"></div>
<div class="ele"></div>
</div>
<p>Change the align Self property of the above div by clicking the below button</p>
<button onclick="changeAlign()">CHANGE</button>
</body>
</html>

输出

这将产生以下输出 −

点击"更改"按钮 −


相关文章


有用资源