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

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

  • 1.出现的问题
  • 2.解决方法
    • [2.1 Flex 布局下关键字 safe、unsafe](#2.1 Flex 布局下关键字 safe、unsafe)
    • [2.2 使用 margin: auto 替代 justify-content: center](#2.2 使用 margin: auto 替代 justify-content: center)
    • [2.3 额外嵌套一层](#2.3 额外嵌套一层)

1.出现的问题

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

javascript 复制代码
//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;
      }

ul和单个li是定宽的,当li数目较少时,页面正常

但是当页面li内容较多时,就会出现问题了

可以看到li的宽度超出了ul容器的宽度且第一个li的显示不完全,这显然不是想要的结果。给ul加个样式overflow: auto;呢:

第一个li还是展示不出来

2.解决方法

2.1 Flex 布局下关键字 safe、unsafe

javascript 复制代码
    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;
      }

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

javascript 复制代码
       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;
      } 

2.3 额外嵌套一层

javascript 复制代码
     <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;
      } 
    

上面三种方法都可以达到效果

记录一下

相关推荐
二月垂耳兔798几秒前
jQueryHTML与插件
前端·jquery
quo-te24 分钟前
AJAX简介
前端·ajax·okhttp
bingbingyihao35 分钟前
通过代码获取接口文档工具
开发语言·前端·javascript
月伤5935 分钟前
JS中Map对象与数组的相互转换
前端·javascript·html
SEO_juper2 小时前
解密 URL 参数:如何利用它们提升网站性能和用户体验
前端·javascript·ux·seo·url·数字营销·谷歌seo
nuIl2 小时前
让 Cursor 帮你把想法落地
前端·ai编程
去伪存真2 小时前
看我如何破解api接口文档定义空白, 还不想手动写接口TS类型定义的困局
前端·typescript
skyWang4163 小时前
Vite模块联邦(vite-plugin-federation)实现去中心化微前端后台管理系统架构
前端
喝拿铁写前端3 小时前
你以为你是中级前端,其实你还停留在执行阶段-完整前端成长之路
前端
前端卧龙人3 小时前
uniapp开发技巧:开启代理与gzip优化实践
前端