• css-解决Flex布局下居中溢出滚动截断问题


    1.出现的问题

    在页面布局中,我们经常会遇到/使用列表内容水平居中于容器中,一般使用flex布局:

    //html
        <ul>
          <li>1</li>
          <li>2</li>
          <li>3</li>
        </ul>
        //css
              ul,
          li {
            list-style: none;
            margin: 0;
            padding: 0;
          }
    
          /* 问题的出现 */
          ul {
            width: 500px;
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content:center;
            align-items: center;
            gap: 10px;
            padding: 10px 0;
            background-color: lightcoral;
          }
          li {
            width: 150px;
            height: 200px;
            background-color: lightgrey;
            text-align: center;
            line-height: 200px;
            flex-shrink: 0;
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    ul和单个li是定宽的,当li数目较少时,页面正常
    在这里插入图片描述
    但是当页面li内容较多时,就会出现问题了
    在这里插入图片描述
    可以看到li的宽度超出了ul容器的宽度且第一个li的显示不完全,这显然不是想要的结果。给ul加个样式overflow: auto;呢:
    在这里插入图片描述
    第一个li还是展示不出来

    2.解决方法

    2.1 Flex 布局下关键字 safe、unsafe

        ul {
            width: 500px;
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content:safe center;
            align-items: center;
            gap: 10px;
            padding: 10px 0;
            background-color: lightcoral;
            overflow: auto;
          }
          li {
            width: 150px;
            height: 200px;
            background-color: lightgrey;
            text-align: center;
            line-height: 200px;
            flex-shrink: 0;
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.2 使用 margin: auto 替代 justify-content: center

           ul {
            width: 500px;
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;
            gap: 10px;
            padding: 10px;
            background-color: lightcoral;
            overflow: auto;
          }
          li {
            width: 150px;
            height: 200px;
            background-color: lightgrey;
            text-align: center;
            line-height: 200px;
            flex-shrink: 0;
            margin: auto;
          } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.3 额外嵌套一层

         <ul class="g-contaner">
          <ul class="g-wrap">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
          </ul>
        </ul> 
           .g-contaner {
            width: 500px;
            display: flex;
            flex-wrap: nowrap;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            overflow: auto;
            padding: 10px;
            background-color: lightcoral;
          }
    
          .g-wrap {
            display: flex;
            gap: 10px;
            max-width: 100%;
          }
          li {
            width: 150px;
            height: 200px;
            background-color: lightgrey;
            text-align: center;
            line-height: 200px;
            flex-shrink: 0;
          } 
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    上面三种方法都可以达到效果
    在这里插入图片描述
    记录一下

  • 相关阅读:
    如何优♂雅地学习GDB调试
    Python异常
    UE里的Sync Groups
    自动驾驶:2022 apollo day 观后感(二)
    ThinkPHP5小语种学习平台
    云计算时代前端如何保证开源代码的安全性
    java计算机毕业设计海南自贸港知识学习与测试源码+mysql数据库+系统+lw文档+部署
    [云原生] 初识Kubernetes
    mysql源码编译安装
    数字孪生与GIS:优化公共交通的未来
  • 原文地址:https://blog.csdn.net/qq_45030898/article/details/136342149