简介
flexee 是一款 npm 包,封装了 flex 布局相关的 CSS 属性及其浏览器前缀,可以帮助开发者更方便地使用 flex 布局。
使用方法
安装 flexee:
npm install flexee
在需要使用 flex 布局的样式中引入 flexee:
@import "~flexee";
在需要使用 flex 布局的元素上添加
display: flex;
属性,然后使用常见的 flex 布局属性即可。
支持的属性
flexee 封装了常见的 flex 布局属性以及浏览器前缀,具体如下:
display: flex;
flex-direction: row;
,flex-direction: row-reverse;
,flex-direction: column;
,flex-direction: column-reverse;
flex-wrap: nowrap;
,flex-wrap: wrap;
,flex-wrap: wrap-reverse;
flex-flow: row nowrap;
,flex-flow: row wrap;
,flex-flow: column nowrap;
,flex-flow: column wrap;
,flex-flow: row-reverse nowrap;
,flex-flow: row-reverse wrap;
,flex-flow: column-reverse nowrap;
,flex-flow: column-reverse wrap;
justify-content: flex-start;
,justify-content: flex-end;
,justify-content: center;
,justify-content: space-between;
,justify-content: space-around;
,justify-content: space-evenly;
align-items: stretch;
,align-items: flex-start;
,align-items: flex-end;
,align-items: center;
,align-items: baseline;
align-content: stretch;
,align-content: flex-start;
,align-content: flex-end;
,align-content: center;
,align-content: space-between;
,align-content: space-around;
,align-content: space-evenly;
flex-grow: 0;
,flex-grow: 1;
flex-shrink: 0;
,flex-shrink: 1;
flex-basis: auto;
示例代码
水平居中
<div class="container"> <div class="box">Content</div> </div>
.container { display: flex; justify-content: center; }
垂直居中
<div class="container"> <div class="box">Content</div> </div>
.container { display: flex; align-items: center; }
水平居中和垂直居中
<div class="container"> <div class="box">Content</div> </div>
.container { display: flex; justify-content: center; align-items: center; }
等分宽度
<div class="container"> <div class="box">Content 1</div> <div class="box">Content 2</div> <div class="box">Content 3</div> </div>
.container { display: flex; justify-content: space-between; }
等分空间
<div class="container"> <div class="box">Content 1</div> <div class="box">Content 2</div> <div class="box">Content 3</div> </div>
.container { display: flex; justify-content: space-around; }
总结
flexee 简化了使用 flex 布局的代码量,提高了开发效率,尤其是在需要支持多种浏览器的情况下。但是,开发者需要熟练掌握 flex 布局相关的属性及其使用方法,才能更好地使用 flexee。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005738681e8991b448e97a3