• 首页
  • css3教程
  • html5教程
  • jQuery手册
  • php7教程
  • MySQL手册
  • apache手册
  • justify-content

    版本:CSS3

    CSSjustify-content属性定义了浏览器如何分配顺着父容器主轴的弹性元素之间及其周围的空间。

    示例

    /* positional alignment */
    justify-content: center;     /* 居中排列 */
    justify-content: start;      /* pack items from the start */
    justify-content: end;        /* pack items from the end */
    justify-content: flex-start; /* 从行首起始位置开始排列 */
    justify-content: flex-end;   /* 从行尾位置开始排列 */
    justify-content: left;       /* pack items from the left */
    justify-content: right;      /* pack items from the right */
    
    /* baseline alignment */
    justify-content: baseline;
    justify-content: first baseline;
    justify-content: last baseline;
    
    /* distributed alignment */
    justify-content: space-between;  /* 均匀排列每个元素
                                       首个元素放置于起点,末尾元素放置于终点 */
    justify-content: space-around;  /* 均匀排列每个元素
                                       每个元素周围分配相同的空间 */
    justify-content: space-evenly;  /* 均匀排列每个元素
                                       每个元素之间的间隔相等 */
    justify-content: stretch;       /* 均匀排列每个元素
                                       'auto'-sized 的元素会被拉伸以适应容器的大小 */
    
    /* overflow alignment */
    justify-content: safe center;
    justify-content: unsafe center;
    
    /* global values */
    justify-content: inherit;
    justify-content: initial;
    justify-content: unset;
    

    浏览器支持

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

    语法

    justify-content:>normal |<content-distribution>|<overflow-position>?[<content-position>| left | right ]

    <content-distribution>= space-between | space-around | space-evenly | stretch
    <overflow-position>= unsafe | safe
    <content-position>= center | start | end | flex-start | flex-end

    取值

    • start从行首开始排列。每行第一个元素与行首对齐,同时所有后续的元素与前一个对齐。
    • flex-start从行首开始排列。每行第一个弹性元素与行首对齐,同时所有后续的弹性元素与前一个对齐。
    • flex-end从行尾开始排列。每行最后一个弹性元素与行尾对齐,其他元素将与后一个对齐。
    • center伸缩元素向每行中点排列。每行第一个元素到行首的距离将与每行最后一个元素到行尾的距离相同。
    • left伸缩元素一个挨一个在对齐容器得左边缘,如果属性的轴与内联轴不平行,则left的行为类似于start
    • right元素以容器右边缘为基准,一个挨着一个对齐,如果属性轴与内联轴不平行,则right的行为类似于start.baseline
      first baseline

      last baselineSpecifies 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 forfirst baselineisstart, the one forlast baselineisend.
    • space-between在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素与行首对齐,每行最后一个元素与行尾对齐。
    • space-around在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素到行首的距离和每行最后一个元素到行尾的距离将会是相邻元素之间距离的一半。
    • space-evenlyflex项都沿着主轴均匀分布在指定的对齐容器中。相邻flex项之间的间距,主轴起始位置到第一个flex项的间距,,主轴结束位置到最后一个flex项的间距,都完全一样。
    • stretchIf the combined size of the items is less than the size of the alignment container, anyauto-sized items have their size increased equally(not proportionally), while still respecting the constraints imposed bymax-height/max-width(or equivalent functionality), so that the combined size exactly fills the alignment container along the main axis.
    • safeIf the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode werestart.
    • unsafeRegardless of the relative sizes of the item and alignment container, the given alignment value is honored.
    默认值normal
    适用于多行的弹性盒模型容器
    继承性
    动画性visual
    计算值指定值

    例子

    //CSS
    #container {
      display: flex;
      justify-content: space-between; /* can be changed in the live sample */
    }
    
    #container > div {
      width: 100px;
      height: 100px;
      background: linear-gradient(-45deg, #788cff, #b4c8ff);
    }
    
    //HTML
    
    <div id="container">
      <div></div>
      <div></div>
      <div></div>
    </div>
    <select id="justifycontent">
      <option value="start">start</option>
      <option value="end">end</option>
      <option value="flex-start">flex-start</option>
      <option value="flex-end">flex-end</option>
      <option value="center">center</option>
      <option value="left">left</option>
      <option value="right">right</option>
      <option value="baseline">baseline</option>
      <option value="first baseline">first baseline</option>
      <option value="last baseline">last baseline</option>
      <option value="space-between" selected>space-between</option>
      <option value="space-around">space-around</option>
      <option value="space-evenly">space-evenly</option>
      <option value="stretch">stretch</option>
    </select>
    
    //JS
    var justifycontent = document.getelementbyid("justifycontent");
    justifycontent.addeventlistener("change", function (evt) {
      document.getelementbyid("container").style.justifycontent =
          evt.target.value;
    }
    );
    
    #container {
      display: flex;
      justify-content: space-between; /* Can be changed in the live sample */
    }
    
    #container > div {
      width: 100px;
      height: 100px;
      background: linear-gradient(-45deg, #788cff, #b4c8ff);
    }
    

    上篇:align-self

    下篇:place-items