Blogger Information
Blog 54
fans 6
comment 31
visits 106625
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Flex项目属性和PC端响应式页面
吾逍遥
Original
1131 people have browsed it

一、Flex项目属性

Flex属性分为容器属性和项目属性两大类,容器属性探讨见https://www.php.cn/blog/detail/24616.html

1、项目属性一:flex(项目最重要的属性)

  • 只能用在项目中, 不能用在容器中
  • 语法格式: 放大因子 收缩因子 基准宽度 ,对应的英文flex-grow flex-shrink flex-basis。
  • 默认项目不放大,可收缩,宽度以width为准,即flex:0 1 autoflex:initial
  • flex-grow放大因子: 取值范围是大于或等于0的正数,0表示不放大其它正数则表示分配剩余空间时它占的比例 。即所有项目放大之和为总的份数,它对应整个剩余空间,该项目的放大因子就是自己所占的份,除以总份数则乘以剩余空间就是它分配的大小。
  • flex-shrink缩小因子: 取值范围同放大,0也表示不缩小,其它正数则表示缩小的比例。和放大不同的是分配的整体缩小空间的比例。
  • flex-basis基准宽度: 取值可以是绝对值、相对值、百分比和auto,auto时宽度为项目的宽度。若有值则覆盖项目的width
  • min-width和max-width min-width限制了缩小的尺寸,而max-width则限制了放大的尺寸。

语法分类:
1.三值语法 就是语法中三个值都有,第一个放大比例,第二个缩小比例,第三个基准宽度。
如:flex:1 1 100px;
2.二值语法 第一个值永远是放大因子, 区别在于第二个值:第二个值是一个没有单位的,表示”收缩因子”flex-shrink,若是百分比或有单位,则表示”基准宽度” /
如:flex: 1 3; 表示放大占1份,缩小放占3份
如:flex: 0 30%; 表示不放大,基准宽度为30%
*3、单值语法
最常用的,必须掌握

  • 无单位,表示放大因子 如flex:1,flex:3
  • 有效的宽度,表示基准宽度,此时放大因子和缩小因子默认为1 如flex:100px,flex:30%
  • 关键字,表示三值语法的简称 如flex:initial等于flex:0 1 auto,flex;auto则等于flex:1 1 auto。
  1. <style>
  2. .box{
  3. /* flex:0 1 auto; */
  4. /* flex:initial */
  5. /* flex:1 1 auto; */
  6. /* flex:auto; */
  7. /* flex:1 100px; */
  8. flex:1;
  9. }
  10. </style>
  11. <div class="container">
  12. <div class="box">1</div>
  13. <div class="box">2</div>
  14. <div class="box">3</div>
  15. </div>

2、项目属性二:order(项目排序)

项目排序在flex的容器属性中flex-direction中只定义两种: 正序和逆序 。而项目的order则可以指定项目的任意排序

排序注意事项:

  • order取值是整数 ,正整数和负整数均可以,默认值为0。
  • order值越小越靠近起始线越大越靠近终止线
  • order相等时则按项目源码顺序排序
  • 起始线和终止线是随着flex-direction变化的。具体见下表
flex方向 起始线 终止线
row
row-reverse
column
column-reverse
  1. .container .box:first-child{ order:0; }
  2. .container .box:nth-child(2){ order:-1; }
  3. .container .box:last-child{ order:2; }

flex-order

3、项目属性三:align-self

align-self设置某个项目在交叉轴上的对齐方式,而align-items则设置主轴方向上一行所有项目在交叉轴的对齐方式。取值和意思同align-items。比较简单就不再细说了。

二、flex实现PC端的响应式页面

实现目标:

  1. flex布局,适应PC、平板和手机
  2. 导航栏根据尺寸调整两种状态
  1. <style>
  2. /* 清除样式 */
  3. * {
  4. margin: 0;
  5. padding: 0;
  6. border: none;
  7. outline: none;
  8. box-sizing: border-box;
  9. }
  10. a {
  11. text-decoration: none;
  12. color: #666;
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. /* 定义基本样式 */
  18. .container {
  19. width: 100vw;
  20. height: 100vh;
  21. color: #666;
  22. }
  23. #header {
  24. position: fixed;
  25. width: 100%;
  26. height: 80px;
  27. line-height: 80px;
  28. color: white;
  29. background-color: rgba(0, 202, 175, 0.75);
  30. transition: height 0.3s ease-in;
  31. display: flex;
  32. justify-content: space-around;
  33. align-items: center;
  34. }
  35. .nav {
  36. min-width: 60vw;
  37. height: 100%;
  38. display: flex;
  39. justify-content: space-evenly;
  40. text-align: center;
  41. font-size: 1.1em;
  42. }
  43. .nav li a {
  44. color: white;
  45. }
  46. .topRight {
  47. height: 100%;
  48. display: flex;
  49. justify-content: space-around;
  50. align-items: center;
  51. }
  52. .topMenu {
  53. visibility: hidden;
  54. width: 36px;
  55. height: 36px;
  56. border-radius: 5px;
  57. background-color: white;
  58. margin-left: 0.5em;
  59. display: flex;
  60. flex-flow: column;
  61. justify-content: space-evenly;
  62. align-items: center;
  63. }
  64. .topMenu span {
  65. width: 80%;
  66. height: 3px;
  67. background-color: rgb(0, 202, 175);
  68. }
  69. .closeMenu {
  70. display: none;
  71. }
  72. .closeMenu span:first-child {
  73. margin-top: 3px;
  74. transform: rotate(45deg);
  75. }
  76. .closeMenu span:last-child {
  77. margin-top: -18px;
  78. transform: rotate(-45deg);
  79. }
  80. /* 主体区 */
  81. /* 图片使用vmax效果非常好 */
  82. .banner > img {
  83. width: 100%;
  84. height: 30vmax;
  85. }
  86. .content{
  87. display: flex;
  88. flex-flow: column nowrap;
  89. align-items: center;
  90. }
  91. .content h2{
  92. font-size: 2em;
  93. }
  94. .content h2::before{
  95. content:'----- ';
  96. }
  97. .content h2::after{
  98. content:' -------';
  99. }
  100. .content .goods{
  101. margin-top: 2em;
  102. width: 100%;
  103. display: flex;
  104. flex-flow: row wrap;
  105. justify-content: space-evenly;
  106. }
  107. .goods a{
  108. flex:0 1 20vw;
  109. text-align: center;
  110. padding: 1em 2em;
  111. background-color: #eee;
  112. box-shadow: 1px 1px 3px black;
  113. margin-bottom: 1em;
  114. }
  115. /* 页脚 */
  116. footer {
  117. display: flex;
  118. flex-flow: column nowrap;
  119. text-align: center;
  120. background-color:#333;
  121. }
  122. /* 媒人查询实现响应式 */
  123. @media screen and (min-width: 800px) {
  124. .nav li {
  125. flex: 1 1 auto;
  126. height: 100%;
  127. border-bottom: 1px solid transparent;
  128. }
  129. .nav li a {
  130. padding: 0.5em 1em;
  131. border-radius: 0.5em;
  132. }
  133. .nav li:hover a {
  134. background-color: whitesmoke;
  135. color: #007d20;
  136. }
  137. .nav li:hover {
  138. border-bottom-color: red;
  139. }
  140. }
  141. @media screen and (max-width: 800px) {
  142. #header {
  143. justify-content: space-between;
  144. height: 58px;
  145. line-height: 58px;
  146. padding: 0 1em;
  147. }
  148. .nav {
  149. display: none;
  150. position: fixed;
  151. left: 0;
  152. top: 58px;
  153. bottom: 0;
  154. min-width: 30vw;
  155. background-color: rgba(0, 202, 175, 0.75);
  156. transition: width 0.3s ease-in;
  157. flex-flow: column nowrap;
  158. justify-content: initial;
  159. }
  160. .nav li {
  161. flex: initial;
  162. }
  163. .nav li a {
  164. padding: initial;
  165. }
  166. .topMenu {
  167. visibility: visible;
  168. }
  169. .nav li:hover a {
  170. color: #007d20;
  171. }
  172. .nav li:hover {
  173. background-color: white;
  174. }
  175. }
  176. /* target核心代码 */
  177. #header:target .nav {
  178. display: flex;
  179. }
  180. #header:target .openMenu {
  181. display: none;
  182. }
  183. #header:target .closeMenu {
  184. display: inline-flex;
  185. }
  186. </style>
  187. <div class="container">
  188. <!-- 顶部 -->
  189. <header id="header">
  190. <h2>PC响应式页面</h2>
  191. <ul class="nav">
  192. <li><a href="">首页</a></li>
  193. <li><a href="">关于我们</a></li>
  194. <li><a href="">最新要闻</a></li>
  195. <li><a href="">加入我们</a></li>
  196. </ul>
  197. <div class="topRight">
  198. <a class="topMenu openMenu" href="#header">
  199. <span></span>
  200. <span></span>
  201. <span></span>
  202. </a>
  203. <a class="topMenu closeMenu" href="#">
  204. <span></span>
  205. <span></span>
  206. </a>
  207. </div>
  208. </header>
  209. <!-- 主体区 -->
  210. <div class="main">
  211. <div class="banner">
  212. <img src="../static/banner_img.jpg" alt="轮播图" />
  213. </div>
  214. <div class="content">
  215. <h2>热销商品</h2>
  216. <div class="goods">
  217. <a href=""><img src="../static/goods1.jpg" alt=""></a>
  218. <a href=""><img src="../static/goods2.jpg" alt=""></a>
  219. <a href=""><img src="../static/goods3.jpg" alt=""></a>
  220. </div>
  221. </div>
  222. </div>
  223. <!-- 底部 -->
  224. <footer>
  225. <p>
  226. php中文网&copy;版权所有(2015-2022) | 备案号:
  227. <a href="">皖ICP-150********</a>
  228. </p>
  229. <p>中国.合肥政务新区置地广场 | 电话: 0551-888999**</p>
  230. </footer>
  231. </div>

由于演示gif较大,上传到图床了。如果看不了演示,可自己复制上面代码进行测试
flex-pc

Codepen演示 https://codepen.io/woxiaoyao81/pen/eYzVLOG

三、学习的总结

  • flex盒子其实将所有 “子元素” 转换成BFC式行内块进行排版布局。它属于一维布局。非常适合一行或一列的排版。后面要学的grid则相当于flex和table二者结合,在二维空间进行布局。
  • 布局中为了渐变效果要使用transition,以后我准备总结,其实入门比较简单
  • 上面PC端布局中使用伪类:target和:not实现菜单点击弹出侧边栏效果,是CSS3新增加的特性,本想使用JS实现,但没学,结果在CSS3中找到方法,它同样适用于tab标签、显示隐藏圣诞框等,具体可见我的博文https://www.php.cn/blog/detail/24645.html
  • 另个布局中页面元素经常要居中,我对它进行了梳理总结,可以看我的博文https://www.php.cn/blog/detail/24646.html
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:flex布局,非常适合一维布局,就是元素朝一个方向排列时, 非常方便, grid是二维布局, 可以在行与列二个方向上同步进行, 香不香?
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
1 comments
吾逍遥 2020-10-29 18:42:22
很香,尤其是适用bootstrap后,再开发小程序或APP去适就flex还是有点难受,现在有明师引路,flex和grid掌握都不在话下
1 floor
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!