经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
overflow:auto的用法详解_CSS教程_CSS
来源:jb51  时间:2020/11/23 11:51:19  对本文有异议

在开始正文前,我介绍一下overflow和flex布局的某些用法。
overflow:auto;如果内容被修剪,则浏览器会显示滚动条,以便查看其余内容。
flex中的属性
display: flex;
flex-direction: column; 主轴为垂直方向,起点在上沿。
overflow和flex布局搭配使用

代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>overflow:auto的用法</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  7. <link rel="stylesheet" type="text/css" href="css/reset.css" />
  8.  
  9. <style type="text/css">
  10. html,body{
  11. width: 100%;
  12. height: 100%;
  13. }
  14. .container{
  15. width: 100%;
  16. height: 100%;
  17. display: flex;
  18. flex-direction: column;
  19. }
  20. .header{
  21. width: 100%;
  22. height: 100px;
  23. background: #f99;
  24. }
  25. .content{
  26. width: 100%;
  27. height: 100%;
  28. overflow: auto;
  29. background: yellow;
  30. flex: 1;
  31. }
  32. .footer{
  33. width: 100%;
  34. height: 100px;
  35. background: #99f;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="container">
  41. <div class="header">
  42.  
  43. </div>
  44. <div class="content">
  45. <ul>
  46. <li>111111</li>
  47. <li>111111</li>
  48. <li>111111</li>
  49. <li>111111</li>
  50. <li>111111</li>
  51. <li>111111</li>
  52. <li>111111</li>
  53. <li>111111</li>
  54. 这里的li要多写一些,这样才会显示效果,我这里为了省篇幅。
  55. </ul>
  56. </div>
  57.  
  58. <div class="footer">
  59. </div>
  60. </div>
  61. </body>
  62. </html>

要实现overflow: auto;这个效果,首先布局,再写样式。
在样式中要在最外边的父盒子container,加入以下样式:

  1. .container{
  2. width: 100%;
  3. height: 100%;
  4. display: flex;
  5. flex-direction: column;
  6. }

还有就是一定要给html和body给宽度和高度100%;

  1. html,body{
  2. width: 100%;
  3. height: 100%;
  4. }

头部和底部都给固定的高度,一般的app的头部和底部都是固定的,像微信聊天记录。

  1. .header{
  2. width: 100%;
  3. height: 100px;
  4. background: #f99;
  5. }
  1. .footer{
  2. width: 100%;
  3. height: 100px;
  4. background: #99f;
  5. }

中间的content给定flex:1,并且加上我们的主角overflow:auto;超出的内容自动裁剪。

  1. .content{
  2. width: 100%;
  3. height: 100%;
  4. overflow: auto;
  5. background: yellow;
  6. flex: 1;
  7. }

效果图如下:

这里写图片描述 

中间的内容区可以上下滑动,超出的部分自动裁剪了。
万变不离其宗,如果在项目中实现某些功能有困难的话,可以先敲一个小demo,比如上文中这个demo,也许有人说so easy,但让你用react写一个类似微信的聊天窗口的布局时,你该如何实现?
下面这个是我用react写的类似于微信聊天窗口的小项目。

这里写图片描述

到此这篇关于overflow:auto的用法详解的文章就介绍到这了,更多相关overflow:auto用法内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号