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;
      } 
    

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

记录一下

相关推荐
Hexene...9 分钟前
【前端Vue】el-dialog关闭后黑色遮罩依然存在如何解决?
前端·javascript·vue.js·elementui·前端框架
Jay_See10 分钟前
JC链客云——项目过程中获得的知识、遇到的问题及解决
前端·javascript·vue.js
普通码农26 分钟前
Element Plus 数字输入框箭头隐藏方案
前端
草字40 分钟前
css flex布局,设置flex-wrap:wrap换行后,如何保证子节点被内容撑高后,每一行的子节点高度一致。
前端·javascript·css
Slice_cy1 小时前
深入剖析Vue框架:实现精简的computed
前端
在芜湖工作的二狗子1 小时前
如何用AI Agent提高程序员写作效率
css
局i1 小时前
ES6 类与继承:现代 JavaScript 面向对象编程
前端·javascript·es6
白菜上路1 小时前
C# Web API Mapster基本使用
前端·c#
叫我詹躲躲1 小时前
偷偷收藏!前端老鸟绝不外传的150个JS插件,让你效率翻3倍…
前端·vue.js
会豪1 小时前
如何让自己的前端项目更优雅
前端