百科狗-知识改变命运!
--

align-self - 定义 flex 子项单独在纵轴方向上的对齐方式 - css 弹性盒子布局

是丫丫呀1年前 (2023-11-21)阅读数 45#技术干货
文章标签元素

align-self

版本:CSS3

CSS属性align-self会对齐当前 flex 行中的 flex 元素,并覆盖已有的align-items的值。如果任何 flex 元素的纵轴方向 margin 值设置为 auto,则会忽略align-self。在网格布局中,它将项目与网格区域内的项目对齐。在 Flexbox中会按照cross axis(当前flex 元素排列方向的垂直方向)进行排列。

示例

/* Keyword values */
align-self: auto;
align-self: normal;

/* Positional alignment */
/* align-self does not take left and right values */
align-self: center; /* Put the item around the center */
align-self: start;  /* Put the item at the start */
align-self: end;    /* Put the item at the end */
align-self: self-start; /* Align the item flush at the start */
align-self: self-end;   /* Align the item flush at the end */
align-self: flex-start; /* Put the flex item at the start */
align-self: flex-end;   /* Put the flex item at the end */

/* Baseline alignment */
align-self: baseline;
align-self: first baseline;
align-self: last baseline;
align-self: stretch; /* Stretch 'auto'-sized items to fit the container */

/* Overflow alignment */
align-self: safe center;
align-self: unsafe center;

/* Global values */
align-self: inherit;
align-self: initial;
align-self: unset;

浏览器支持

IE11以上版本的浏览器都支持align-self

语法

align-self:auto| normal | stretch ||?

=[ first | last ]? baseline
= unsafe | safe
= center | start | end | self-start | self-end | flex-start | flex-end

取值

  • auto:默认值。元素继承了它的父容器的align-items属性。如果没有父容器则为"stretch"。
  • normal:效果取决于当前的布局模式:
    • 绝对定位布局中,normal在绝对定位的替代元素上表现为start,在所有其他绝对定位元素上表现为stretch。
    • 在绝对定位的静态元素上表现为stretch。
    • flex布局中表现为stretch。
    • For grid items, this keyword leads to a behavior similar to the one of stretch, except for boxes with an aspect ratio or an intrinsic sizes where it behaves like start.
    • 在块级和表格单元中无效。
  • self-start:Aligns the items to be flush with the edge of the alignment container corresponding to the item's start side in the cross axis.
  • self-end:Aligns the items to be flush with the edge of the alignment container corresponding to the item's end side in the cross axis.
  • flex-start:flex 元素会对齐到 cross-axis 的首端。
  • flex-end:flex 元素会对齐到 cross-axis 的尾端。
  • center:flex 元素会对齐到 cross-axis 的中间,如果该元素的 cross-size 尺寸大于 flex 容器,将在两个方向均等溢出。
  • baselinefirst baselinelast baseline:Specifies participation in first- or last-baseline alignment: aligns the alignment baseline of the box’s first or last baseline set with the corresponding baseline in the shared first or last baseline set of all the boxes in its baseline-sharing group.The fallback alignment for first baseline is start, the one for last baseline is end.
    flex 元素将会基于容器的宽和高,按照自身 margin box 的 cross-size 拉伸。If the combined size of the items along the cross axis is less than the size of the alignment container and the item is auto-sized, its size is increased equally(not proportionally), while still respecting the constraints imposed by max-height/max-width(or equivalent functionality), so that the combined size of all auto-sized items exactly fills the alignment container along the cross axis.
  • safe:If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode were start.
  • unsafe:Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.
默认值auto
适用于多行的弹性盒模型容器
继承性
动画性visual
计算值指定值

align-self取值

.main .demo{
    align-self:auto;
}

效果图:



stretch:元素被拉伸以适应容器。如果指定侧轴大小的属性值为'auto',则其值会使项目的边距盒的尺寸尽可能接近所在行的尺寸,但同时会遵照'min/max-width/height'属性的限制。

.main .demo{
    align-self:stretch;
}

效果图:



center:元素位于容器的中心。。弹性盒子元素在该行的侧轴(纵轴)上居中放置。(如果该行的尺寸小于弹性盒子元素的尺寸,则会向两个方向溢出相同的长度)。

.main .demo{
    align-self:center;
}

效果图:



flex-start:元素位于容器的开头。弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴起始边界。

.main .demo{
    align-self:flex-start;
}

效果图:



flex-end:元素位于容器的结尾。弹性盒子元素的侧轴(纵轴)起始位置的边界紧靠住该行的侧轴结束边界。

.main .demo{
    align-self:flex-end;
}

效果图:



baseline :元素位于容器的基线上。如弹性盒子元素的行内轴与侧轴为同一条,则该值与'flex-start'等效。其它情况下,该值将参与基线对齐。

.main .demo{
    align-self:baseline;
}

效果图:



align-self - 定义 flex 子项单独在纵轴方向上的对齐方式 - css 弹性盒子布局


section {
  display: flex; align-items: center;
  height: 120px;
  background: beige;
}

div {
  height: 60px;
  background: cyan;
  margin: 5px;
}

div:nth-child(3) { align-self: flex-end;
  background: pink;
} 
Item #1
Item #2
Item #3
.demo1 section{display: flex; align-items: center; height: 120px; background: beige;}.demo1 section div{height: 60px; background: cyan; margin: 5px;}.demo1 section div:nth-child(3){align-self: flex-end; background: pink;}
Item #1
Item #2
Item #3

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)