相关疑难解决方法(0)

在CSS Flexbox中,为什么没有"justify-items"和"justify-self"属性?

考虑flex容器的主轴和横轴:

在此输入图像描述                                                                                                                        资料来源:W3C

要沿主轴对齐flex项,有一个属性:

要沿横轴对齐flex项,有三个属性:

在上图中,主轴是水平的,横轴是垂直的.这些是Flex容器的默认方向.

但是,这些方向可以很容易地与flex-direction财产互换.

/* main axis is horizontal, cross axis is vertical */
flex-direction: row;
flex-direction: row-reverse;

/* main axis is vertical, cross axis is horizontal */    
flex-direction: column;
flex-direction: column-reverse;
Run Code Online (Sandbox Code Playgroud)

(横轴始终垂直于主轴.)

我在描述轴的工作方式时的观点是,任何一个方向似乎都没有什么特别之处.主轴,横轴,它们在重要性方面都是相同的,并且flex-direction可以方便地来回切换.

那么为什么横轴有两个额外的对齐属性呢?

为什么align-content并且align-items合并为主轴的一个属性?

为什么主轴没有justify-self属性?


这些属性有用的场景:

  • 将flex项放在flex容器的角落
    #box3 { align-self: flex-end; justify-self: flex-end; }

  • 制作一组flex项目align-right(justify-content: flex-end)但是让第一个项目对齐left(justify-self: flex-start)

    考虑带有一组导航项和徽标的标题部分.随着justify-self徽标可以左对齐,而导航项目保持最右边,整个事物平滑地调整("弯曲")到不同的屏幕尺寸.

  • 在一排三个柔性物品中,将中间物品粘贴到容器的中心(justify-content: center)并将相邻的物品对齐到容器边缘(justify-self: flex-start …

css w3c language-lawyer flexbox

602
推荐指数
3
解决办法
14万
查看次数

flex-wrap如何使用align-self,align-items和align-content?

align-self

在以下代码中,align-self使用flex-wrap: nowrap.

在此输入图像描述

flex-container {
  display: flex;
  flex-wrap: nowrap;
  align-content: flex-start;
  width: 250px;
  height: 250px;
  background-color: silver;
}
flex-item {
  flex: 0 0 50px;
  height: 50px;
  margin: 5px;
  background-color: lightgreen;
}
flex-item:last-child {
  align-self: flex-end;
  background-color: crimson;
}
Run Code Online (Sandbox Code Playgroud)
<flex-container>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
  <flex-item></flex-item>
</flex-container>
Run Code Online (Sandbox Code Playgroud)

但是当容器切换到时flex-wrap: wrap,align-self属性会失败.

在此输入图像描述

flex-container {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  width: 250px;
  height: 250px;
  background-color: silver;
}
flex-item {
  flex: 0 0 50px;
  height: 50px;
  margin: …
Run Code Online (Sandbox Code Playgroud)

css css3 flexbox

56
推荐指数
2
解决办法
2万
查看次数

将 flexbox 与 flex-direction:column 对齐到垂直中心

如何将项目对齐到垂直中心?

我同时使用flex-direction: column;align-items: center;

.box{
border: 1px solid red;
width: 100%;
height: 180px;

display: -webkit-box;
display: -ms-flexbox;
display: flex;

-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;

-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}

.item{
width: 20%;
border: 1px solid green;
}
Run Code Online (Sandbox Code Playgroud)
<div class="box">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
  <div class="item">5</div>
</div>
Run Code Online (Sandbox Code Playgroud)

css flexbox

1
推荐指数
1
解决办法
667
查看次数

标签 统计

css ×3

flexbox ×3

css3 ×1

language-lawyer ×1

w3c ×1