• 首页
  • css3教程
  • html5教程
  • jQuery手册
  • php7教程
  • MySQL手册
  • apache手册
  • align-self

    版本:CSS3

    CSS属性align-self会对齐当前 flex 行中的 flex 元素,并覆盖已有的align-items的值。如果任何 flex 元素的侧轴方向 margin 值设置为 auto,则会忽略align-self。In Grid, it aligns the item inside thegrid area.在Flexbox中会按照cross axis(当前flex 元素排列方向的垂直方向)进行排列.

    示例

    align-self: auto
    align-self: flex-start
    align-self: flex-end
    align-self: center
    align-self: baseline
    align-self: stretch
    
    align-self: inherit
    

    浏览器支持

    IE浏览器火狐浏览器opera浏览器chrome浏览器safari浏览器
    IE11以上版本的浏览器都支持align-self

    语法

    align-self:>auto | normal | stretch |<baseline-position>| <overflow-position>?<self-position>

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

    取值

    • auto:设置为父元素的 align-items 值。
    • normal:The effect of this keyword is dependent of the layout mode we are in:
      • In absolutely-positioned layouts, the keyword behaves like start on replaced absolutely-positioned boxes, and as stretch on all other absolutely-positioned boxes.
      • In static position of absolutely-positioned layouts, the keyword behaves as stretch.
      • For flex items, the keyword behaves as 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.
      • The property doesn't apply to block-level boxes, and to table cells.
    • 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
    计算值指定值

    例子

    .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;
    }
    

    效果图:

    initial设置该属性为它的默认值。请参考 initial

    inherit从父元素继承该属性。请参考inherit

    实例

    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;
    }
    
    <section>
      <div>Item #1</div>
      <div>Item #2</div>
      <div>Item #3</div>
    </section>
    
    

    上篇:align-items

    下篇:place-items