当前位置: 移动技术网 > IT编程>网页制作>CSS > CSS3布局之多列布局columns详解

CSS3布局之多列布局columns详解

2018年10月30日  | 移动技术网IT编程  | 我要评论

css3新单位vw、vh、vmin、vmax

vh: 相对于视窗的高度, 视窗被均分为100单位的vh;
vw: 相对于视窗的宽度, 视窗被均分为100单位的vw;

vmax: 相对于视窗的宽度或高度中较大的那个。其中最大的那个被均分为100单位的vmax;
vmin: 相对于视窗的宽度或高度中较小的那个。其中最小的那个被均分为100单位的vmin;
视区所指为浏览器内部的可视区域大小,
window.innerwidth/window.innerheight大小,不包含任务栏标题栏以及底部工具栏的浏览器区域大小。

css3中calc动态计算元素长度

calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度。比如说,你可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值。

注意点:

  • 用于动态计算长度值。
  • 需要注意的是,运算符前后都需要保留一个空格,例如:width: calc(100% - 10px);
  • 任何长度值都可以使用calc()函数进行计算;
  • calc()函数支持 “+”, “-“, “*”, “/” 运算;
  • calc()函数使用标准的数学运算优先级规则;

例如:

calc(100vh - 10px)  表示整个浏览器窗口高度减去10px的大小
calc(100vw - 10px)   表示整个浏览器窗口宽度减去10px的大小

一般用来设置流式布局宽高,当然,你可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值

详细了解可参考:《css3 calc实现滚动条出现页面不跳动》。

css3布局:box-flex(伸缩盒最老版本

设置或检索伸缩盒对象的子元素如何分配其剩余空间。

<ul id="box">
    <li>a</li>
    <li>b</li>
    <li>c</li>
</ul>

css:

#box{display:box;width:240px;height:100px;margin:0;padding:10px;list-style:none;}
#box li:nth-child(1){box-flex:1;}
#box li:nth-child(2){box-flex:1;}
#box li:nth-child(3){box-flex:2;}

误区:注意box-flex只是动态分配父元素的剩余空间,而不是整个父元素的空间。

详细可参考《css3布局之box-flex的使用

css3布局:flex

flex是flexible box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

任何一个容器都可以指定为flex布局。

例如:让一个box盒子中的两个p元素在box中平分宽度,并且垂直居中。如下代码:

<div class="box">
    <p>这是一段内容1</p>
    <p>这是一段内容2</p>
</div>

css:

*{margin:0;padding:0;}
.box{
    background: #ccc;
    width:300px;
    height:200px;
    margin:30px auto;
    display: flex;
    align-items: center; /*在交叉轴上对齐方式-垂直居中*/
    justify-content: center;/*主轴上的对齐方式-水平居中*/
}
.box p{
    flex:1;
}
.box p:nth-child(1){background: red}
.box p:nth-child(2){background: blue}

效果如图:

再看一个常用的例子:移动端使用flex让内容平均分配

<nav>
  <a href="#">a</a>
  <a href="#">b</a>
  <a href="#">c</a>
</nav>

css代码:

nav{display:-webkit-flex;display:flex;}
a{-webkit-flex:1;flex:1;}

容器的属性:

1.flex-direction 决定主轴的方向(即项目的排列方向)

  • row(默认值):主轴为水平方向,起点在左端。
  • row-reverse:主轴为水平方向,起点在右端。
  • column:主轴为垂直方向,起点在上沿。
  • column-reverse:主轴为垂直方向,起点在下沿。

2.flex-wrap默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

  • nowrap(默认):不换行。
  • wrap:换行,第一行在上方。
  • wrap-reverse:换行,第一行在下方。

3.flex-flow:flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。

.box {
  flex-flow: <flex-direction> || <flex-wrap>;
}

4.justify-content:定义了项目在主轴上的对齐方式。

可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

  • flex-start(默认值):左对齐
  • flex-end:右对齐
  • center: 居中
  • space-between:两端对齐,项目之间的间隔都相等。
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

5.align-items:定义项目在交叉轴上如何对齐。

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

  • flex-start:交叉轴的起点对齐。
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

6.align-content:定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。

 项目的属性:

以下6个属性设置在项目上。

  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

1.order属性

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

.item {
  order: <integer>;
}

2.flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

.item {
  flex-grow: <number>; /* default 0 */
}

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

3.flex-shrink属性

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

.item {
  flex-shrink: <number>; /* default 1 */
}

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

负值对该属性无效。

4.flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

.item {
  flex-basis: <length> | auto; /* default auto */
}

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。

5.flex属性

flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

6.align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

详细参考:《flex 布局语法教程》

css3多列布局:columns

columns语法:columns:[ column-width ] || [ column-count ]

其中:[ column-width ]:设置或检索对象每列的宽度;[ column-count ]:设置或检索对象的列数。

 

<h1>列数及列宽固定:</h1>
<div class="test">
    <p>this module describes multi-column layout in css. by using functionality described in this document, style sheets can declare that the content of an element is to be laid out in multiple columns. </p>
    <p>on the web, tables have also been used to describe multi-column layouts. the main benefit of using css-based columns is flexibility; content can flow from one column to another, and the number of columns can vary depending on the size of the viewport. removing presentation table markup from documents allows them to more easily be presented on various output devices including speech synthesizers and small mobile devices.</p>
</div>
<h1>列宽固定,根据容器宽度液态分布列数:</h1>
<div class="test2">
    <p>this module describes multi-column layout in css. by using functionality described in this document, style sheets can declare that the content of an element is to be laid out in multiple columns. </p>
    <p>on the web, tables have also been used to describe multi-column layouts. the main benefit of using css-based columns is flexibility; content can flow from one column to another, and the number of columns can vary depending on the size of the viewport. removing presentation table markup from documents allows them to more easily be presented on various output devices including speech synthesizers and small mobile devices.</p>
</div>

 

css:

body{font:14px/1.5 georgia,serif,sans-serif;}
p{margin:0;padding:5px 10px;background:#eee;}
h1{margin:10px 0;font-size:16px;}
.test{
    width:628px;
    border:10px solid #000;
    -moz-columns:200px 3;
    -webkit-columns:200px 3;
    columns:200px 3;
}
.test2{
    border:10px solid #000;
    -moz-columns:200px;
    -webkit-columns:200px;
    columns:200px;
}

结果如图所示:

以下列出column运用的常用相关属性:

1.column-width<length> | auto 设置或检索对象每列的宽度
如:-moz-column-width:200px;

2.column-count<integer> | auto 设置或检索对象的列数
如:-webkit-column-count:3;

3.column-gap<length> | normal 设置或检索对象的列与列之间的间隙
如:column-gap:normal;column-gap:40px;

4.column-rule[ column-rule-width ] || [ column-rule-style ] || [ column-rule-color ]设置或检索对象的列与列之间的边框。复合属性。相当于border属性
如:column-rule:10px solid #090;

5.column-spannone | all 设置或检索对象元素是否横跨所有列。
如:column-span:all;

 详细参考:《css3布局之多列布局columns详解

css相关题目

<div class="red blue">123</div>
<div class="blue red">123</div>

css:

.red {
    color: red
}

.blue {
    color: blue
}

结果为什么:
答案:结果两个div的颜色值显示都为blue

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网