当前位置:Gxlcms > css > CSS怎么实现底部对齐?css实现底部对齐的三种方法

CSS怎么实现底部对齐?css实现底部对齐的三种方法

时间:2021-07-01 10:21:17 帮助过:102人阅读

本篇文章给大家带来的内容是关于CSS怎么实现底部对齐?css实现底部对齐的三种方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

CSS实现底部对齐效果

因公司业务要求需要实现如下图中红色区域的效果:
1153976964-5ba078d29a6ac_articlex.png

671446208-5ba078dd8ce68_articlex.png

效果说明:

1、红色区域数据需要倒排(即从底部开始数,数字为1、2、3、4、5),并且显示在最底部
2、当数据过多时需要显示滚动条,**并且滚动条需要拉到最底部**
3、数据从websocket中推送过来,推送间隔为几十毫秒
4、需要兼容ie10及以上浏览器

使用flex布局实现

  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .container{
  8. position: relative;
  9. width: 300px;
  10. height: 500px;
  11. margin: 10px auto;
  12. border: 1px solid #f60;
  13. color: #fff;
  14. }
  15. .top,
  16. .bottom{
  17. height: 50%;
  18. padding: 20px;
  19. }
  20. .top{
  21. background-color: #da2e22;
  22. }
  23. .top>ul{
  24. width: 100%;
  25. height: 100%;
  26. overflow: auto;
  27. }
  28. .bottom{
  29. overflow: auto;
  30. background-color: #1e1e1e;
  31. }
  32. </style>
  1. <div class="container">
  2. <div class="top">
  3. <ul style="padding-top: 104px;">
  4. <li>我是第1个li元素</li>
  5. <li>我是第2个li元素</li>
  6. <li>我是第3个li元素</li>
  7. <li>我是第4个li元素</li>
  8. <li>我是第5个li元素</li>
  9. </ul>
  10. </div>
  11. <div class="bottom">
  12. <ul>
  13. <li>我是第1个li元素</li>
  14. <li>我是第2个li元素</li>
  15. <li>我是第3个li元素</li>
  16. <li>我是第4个li元素</li>
  17. <li>我是第5个li元素</li>
  18. </ul>
  19. </div>
  20. </div>

使用flex布局是目前最好的解决办法,子元素布局还是按照1、2、3、4、5这样的顺序进行布局,浏览器器在渲染时会自动反转过来,并且滚动条也会反转过来,即自动定位到最底部。但是IE10目前为止还不支持~,所以在我做的这个项目中是用不了了,只能另辟蹊径。

使用padding-top实现

  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .container{
  8. position: relative;
  9. width: 300px;
  10. height: 500px;
  11. margin: 10px auto;
  12. border: 1px solid #f60;
  13. color: #fff;
  14. }
  15. .top,
  16. .bottom{
  17. height: 50%;
  18. padding: 20px;
  19. }
  20. .top{
  21. background-color: #da2e22;
  22. }
  23. .top>ul{
  24. width: 100%;
  25. height: 100%;
  26. overflow: auto;
  27. }
  28. .bottom{
  29. overflow: auto;
  30. background-color: #1e1e1e;
  31. }
  32. </style>
  1. <div class="container">
  2. <div class="top">
  3. <ul style="padding-top: 104px;">
  4. <li>我是第1个li元素</li>
  5. <li>我是第2个li元素</li>
  6. <li>我是第3个li元素</li>
  7. <li>我是第4个li元素</li>
  8. <li>我是第5个li元素</li>
  9. </ul>
  10. </div>
  11. <div class="bottom">
  12. <ul>
  13. <li>我是第1个li元素</li>
  14. <li>我是第2个li元素</li>
  15. <li>我是第3个li元素</li>
  16. <li>我是第4个li元素</li>
  17. <li>我是第5个li元素</li>
  18. </ul>
  19. </div>
  20. </div>

使用padding-top是最容易想到的一种实现方式,但它无法用纯css实现,它还必须使用js进行计算才可以。我在项目中刚开始就是padding-top+js计算来实现的,这种方式实现起来就是感觉不爽, websocket每推送一条数据过来就要进行计算。那么还有没有更好的办法呢?答案是肯定有的,在css世界中总有意想不到的惊喜,关键是内功要强。

使用table-cell来实现

  1. <style>
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. .container{
  8. position: relative;
  9. width: 300px;
  10. height: 500px;
  11. margin: 10px auto;
  12. border: 1px solid #f60;
  13. color: #fff;
  14. }
  15. .top,
  16. .bottom{
  17. height: 50%;
  18. padding: 20px;
  19. overflow: auto;
  20. }
  21. .top{
  22. background-color: #da2e22;
  23. }
  24. .top-container{
  25. display: table;
  26. width: 100%;
  27. height: 100%;
  28. }
  29. .top-container>ul{
  30. display: table-cell;
  31. vertical-align: bottom;
  32. width: 100%;
  33. height: 100%;
  34. }
  35. .bottom{
  36. background-color: #1e1e1e;
  37. }
  38. </style>
  1. <div class="container">
  2. <div class="top">
  3. <div class="top-container">
  4. <ul>
  5. <li>我是第1个li元素</li>
  6. <li>我是第2个li元素</li>
  7. <li>我是第3个li元素</li>
  8. <li>我是第4个li元素</li>
  9. <li>我是第5个li元素</li>
  10. </ul>
  11. </div>
  12. </div>
  13. <div class="bottom">
  14. <ul>
  15. <li>我是第1个li元素</li>
  16. <li>我是第2个li元素</li>
  17. <li>我是第3个li元素</li>
  18. <li>我是第4个li元素</li>
  19. <li>我是第5个li元素</li>
  20. </ul>
  21. </div>
  22. </div>

使用table-cell来实现底部对齐目前是最后的解决方案了,并且它还兼容ie8。底部对齐问题解决了,"滚动条需要拉到最底部"这个问题使用table-cell是无法实现的,没办法最后只有使用js去控制了,不知道有哪位大神有其他办法没~
css的table、table-cell布局可以实现很多特殊效果。

以上就是CSS怎么实现底部对齐?css实现底部对齐的三种方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行