Blogger Information
Blog 40
fans 0
comment 1
visits 34273
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
初识flex(常用属性的介绍)PS:附三种实现水平和垂直居中的方式
景云
Original
1323 people have browsed it

flex

1.简述

  1. a.任何元素都可以通过添加`display:flex`属性,转为弹性盒元素
  2. b.转为flex元素后,它的内部的‘子元素’就支持flex布局了
  3. c.容器中的项目会自动转为“行内块元素”(不管之前是什么类型的。)

2.术语

  1. a.容器:具有`display:flex`属性的元素
  2. b.项目:flex容器的子元素
  3. c.主轴:项目排列的轴线
  4. d.交叉轴:与主轴垂直的轴线

3.容器的属性

序号 属性 描述
1 flex-flow 主轴方向与换行方式
2 justify-content 项目在主轴上的对齐方式
3 align-item 项目在交叉中上的对齐方式
4 align-content 项目在多行容器中的对其方式

4.项目属性

序号 属性 描述
1 flex 项目的缩放比例与基准宽度
2 align-self 单个项目在交叉轴上的对其方式
3 order 项目在主轴上的排列顺序
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>初识flex(常用属性的介绍)PS:附三种实现水平和垂直居中的方式</title>
  7. <style>
  8. /* 将flex容器与项目的常用属性全部进行实例演示,并理解属性的常用值的显示效果 */
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. /* 附:三种实现水平和垂直居中的方式 */
  15. /* 1.传统方式实现水平和垂直居中 */
  16. /*
  17. header{
  18. height: 3em;
  19. background-color: cadetblue;
  20. }
  21. footer{
  22. height: 2em;
  23. background-color: darkkhaki;
  24. }
  25. .container{
  26. position: relative;
  27. min-height: 40em;
  28. margin: 0.5em 0;
  29. background-color: cornsilk;
  30. }
  31. aside{
  32. width: 15em;
  33. min-height: inherit;
  34. background-color: cyan;
  35. }
  36. .container aside:first-of-type{
  37. position: absolute;
  38. left: 0;
  39. }
  40. .container aside:last-of-type{
  41. position: absolute;
  42. right: 0;
  43. }
  44. .container main{
  45. min-height: inherit;
  46. position: absolute;
  47. left: 15.5em;
  48. right: 15.5em;
  49. background-color: darkseagreen;
  50. } */
  51. /* 2.用flex快速实现水平和垂直居中 */
  52. /* body中除了container这个类,设置全部元素的属性 */
  53. /* body *:not(.container){
  54. background-color: khaki;
  55. }
  56. header,footer{
  57. height: 8vh;
  58. }
  59. .container{
  60. height: calc(84vh - 2em);
  61. display: flex;
  62. margin: 1em 0;
  63. }
  64. aside{
  65. min-width: 15em;
  66. }
  67. main{
  68. min-width: calc(100% - 30em - 1em);
  69. margin: 0 0.5em;
  70. } */
  71. /* 3.用grid快速实现水平和垂直居中 */
  72. /* body{
  73. height: 100vh;
  74. display: grid;
  75. 整个板块为三行三列
  76. grid-template-rows: 8vh 1fr 8vh;行,高度:上面8vh 中间自适应 下面8vh
  77. grid-template-columns: 15em 1fr 15em;列,宽度:左侧15em 中间自适应 右侧15em
  78. gap:0.5em;间隙
  79. }
  80. 页眉和页脚应该是通栏的,所以应该设置占据三列
  81. header,footer{
  82. grid-column: span 3;
  83. }
  84. body > *{
  85. background-color: khaki;
  86. }
  87. grid虽然强大无比 很简洁,但是不代表到处都要用到它,它更适合整天(宏观)布局,而flex更适合细节处理。 */
  88. .container{
  89. display: flex;
  90. height: 15em;
  91. }
  92. .container .item{
  93. width: 5em;
  94. /* height: 3em; */
  95. }
  96. /* 工作中会存在大量的flex容器的嵌套布局 */
  97. .container > .item:nth-child(9){
  98. display: flex;
  99. }
  100. .container > .item:nth-child(9) > div{
  101. background-color: lawngreen;
  102. border: 1px solid #000;
  103. }
  104. .container{
  105. /* 1.弹性项目在主轴的排列方式 */
  106. /* a.单行容器:当一行放不下的时候会压缩到内容宽度,不会换行 */
  107. /* flex-direction: row;主轴方向(默认为行模式) */
  108. /* flex-wrap: nowrap;项目的换行(默认不换行) */
  109. /* flex-flow: row nowrap; 上面两个的简写*/
  110. /* b.多行容器:一行显示不下的时候,允许沿着交叉轴换行显示 */
  111. /* flex-flow: row wrap; 默认为行显示*/
  112. /* flex-flow: column nowrap; 修改为列显示、不换行*/
  113. /* flex-flow: column wrap;列显示、换行 */
  114. /* 2.弹性项目在主轴的对齐方式 (设置项目在单行容器中对齐方式的前提是主轴上存在剩余空间)
  115. 空间分配的两种方案:a.将所有项目视为一个整体,在项目组两边进行分配;b.将项目视为一个个主体,在项目之间进行分配。
  116. */
  117. /* 默认为左对齐
  118. justify-content: flex-start; */
  119. /* 右对齐
  120. justify-content: flex-end; */
  121. /* 居中对齐
  122. justify-content: center; */
  123. /* 两端对齐(在除了首尾处,其他项目之前平均分配)
  124. justify-content: space-between; */
  125. /* 分散对齐(每个项目两边的空间一致)
  126. justify-content: space-around; */
  127. /* 平均对齐(剩余空间在每个项目中间平均分配)
  128. justify-content: space-evenly; */
  129. /* 3.单行容器中交叉轴上的对齐方式 */
  130. /* 默认拉伸(没设置高度的时候会将项目拉伸至容器的高度)
  131. align-items: stretch;*/
  132. /* 顶部对齐
  133. align-items: flex-start;*/
  134. /* 底部对齐
  135. align-items: flex-end;*/
  136. /* 居中对齐
  137. align-items: center;*/
  138. /* 3.多行容器中主轴上的对齐方式 */
  139. /* flex-flow: row wrap;沿主轴方向换行
  140. align-content: stretch;
  141. align-content: flex-start;
  142. align-content: flex-end;
  143. align-content: center;
  144. align-content: space-between;
  145. align-content: space-around;
  146. align-content: space-evenly; */
  147. }
  148. .container .item{
  149. /* 4.项目属性flex */
  150. /* flex:flex-grow flex-shrink flex-basis */
  151. /* flex:放大因子 收缩因子 项目在主轴上的基准宽度 */
  152. /* 默认值:不放大 自动收缩 取容器中的width属性 */
  153. /* flex: 0 1 auto;
  154. flex:initial;
  155. */
  156. /* 允许放大和收缩 */
  157. flex:1 1 auto;
  158. flex:auto;
  159. /* 禁止放大和收缩 */
  160. flex: 0 0 auto;
  161. flex: none;
  162. /* flex: 1或者2或者3;等于flex:1 1 auto; */
  163. /* flex通常不会用来设置所有项目的默认选项,通常用来设置某一个项目的特征 */
  164. }
  165. /* 第二个项目和第三个项目是第一个项目和第四个项目的二倍 */
  166. /* .container .item:first-of-type,.container .item:last-of-type{
  167. flex: 1;
  168. }
  169. .container .item:nth-of-type(2), .container .item:nth-of-type(2)+ *{
  170. flex: 2;
  171. } */
  172. /* 让第二个项目和其他项目的对其方式不一样 */
  173. /* .container>.item:nth-of-type(2){
  174. align-self: stretch;
  175. align-self: flex-start;
  176. align-self: flex-end;
  177. align-self: center;
  178. position: relative;定位也可以用
  179. left: -2em;
  180. } */
  181. /* 5.设置项目在主轴上的显示顺序
  182. 显示顺序默认按照书写的源码顺序,默认序号越小越靠前,序号学大学靠后
  183. */
  184. .container .item:first-of-type{
  185. order: 2;
  186. background-color: lawngreen;
  187. }
  188. .container .item:nth-of-type(2){
  189. order: 3;
  190. background-color: lightseagreen;
  191. }
  192. .container .item:nth-of-type(3){
  193. order: -1; /* 支持负值 */
  194. background-color: magenta;
  195. }
  196. .container .item:last-of-type{
  197. background-color: lightcoral;
  198. }
  199. </style>
  200. </head>
  201. <body>
  202. <!-- 1.flex简介:弹性盒布局,这是第一个针对布局的技术;在flex眼中,所有的元素都是行内块;flex中,页面存在两个布局的参考轴,一个叫主轴(控制元素的排列方向),一个是交叉轴(控制元素的换行方向)。 -->
  203. <div class="container">
  204. <div class="item">item1</div>
  205. <div class="item">item2</div>
  206. <div class="item">item3</div>
  207. <div class="item">item4</div>
  208. <!-- <div class="item">item5</div>
  209. <div class="item">item6</div>
  210. <div class="item">item7</div>
  211. <div class="item">item8</div>
  212. <div class="item">item9</div>
  213. <div class="item">item10</div>
  214. <div class="item">item11</div>
  215. <div class="item">item12</div>
  216. <div class="item">item13</div>
  217. <div class="item">item14
  218. <div>1</div>
  219. <div>2</div>
  220. <div>3</div>
  221. </div> -->
  222. </div>
  223. <!-- 附:三种实现水平和垂直居中的方式 -->
  224. <!-- 常规和flex布置版快
  225. <header>页眉</header>
  226. <div class="container">
  227. <aside>左侧</aside>
  228. <main>主体</main>
  229. <aside>右侧</aside>
  230. </div> -->
  231. <!-- grid布置板块
  232. <header>页眉</header>
  233. <aside>左侧</aside>
  234. <main>主体</main>
  235. <aside>右侧</aside>
  236. <footer>页脚</footer> -->
  237. </body>
  238. </html>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!