• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • justify-content

    版本:CSS3

    CSS justify-content属性定义了浏览器之间,如何分配顺着弹性容器主轴(或者网格行轴)的元素之间及其周围的空间。

    当 length 属性和自动外边距属性(margin: 0 auto)生效之后,对齐已经完成了。也就是说,如果存在至少一个弹性元素,而且这个元素的 flex-grow 属性不等于 0,那么对齐方式不会生效,就像没有多余空间的情况。
    当同时给 flex 元素设置了以下两个属性时,主轴方向上的长度width(或 height)、margin: 0 autojustify-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

    取值

    • flex-start:默认值。从行首开始排列。每行第一个弹性元素与行首对齐,同时所有后续的弹性元素与前一个对齐。
    • flex-end:从行尾开始排列。每行最后一个弹性元素与行尾对齐,其他元素将与后一个对齐。
    • start:从行首开始排列。每行第一个元素与行首对齐,同时所有后续的元素与前一个对齐。
    • center:伸缩元素向每行中点排列。每行第一个元素到行首的距离将与每行最后一个元素到行尾的距离相同。
    • left:伸缩元素一个挨一个在对齐容器得左边缘,如果属性的轴与内联轴不平行,则left的行为类似于start
    • right:元素以容器右边缘为基准,一个挨着一个对齐,如果属性轴与内联轴不平行,则right的行为类似于start
    • 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 forfirst baselineisstart, the one forlast baselineisend.
    • space-between:在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素与行首对齐,每行最后一个元素与行尾对齐。
    • space-around:在每行上均匀分配弹性元素。相邻元素间距离相同。每行第一个元素到行首的距离和每行最后一个元素到行尾的距离将会是相邻元素之间距离的一半。
    • space-evenly:flex项都沿着主轴均匀分布在指定的对齐容器中。相邻flex项之间的间距,主轴起始位置到第一个flex项的间距,,主轴结束位置到最后一个flex项的间距,都完全一样。
    • stretch:If 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.
    • safe:If the size of the item overflows the alignment container, the item is instead aligned as if the alignment mode werestart.
    • unsafe:Regardless of the relative sizes of the item and alignment container, the given alignment value is honored.
    初始值normal
    适用于多行的弹性盒模型容器
    继承性
    动画性visual
    计算值指定值


    例子

    //CSS
    #container {
      padding:20px;
      background:#ccc;
      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>
    
    //jQuery
    $(document).on("change","#justifycontent",function(e){
    let value=$(this).val();
    $("#container").css("justify-content",value);
    });
    
    1
    2
    3

    上篇:align-self

    下篇:justify-items