码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • css-解决Flex布局下居中溢出滚动截断问题


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

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

    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

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

  • 相关阅读:
    Android端Base64解码表情emoj乱码
    计算机毕业设计Java高等数学试卷系统(源码+系统+mysql数据库+lw文档)
    中科大郑烇、杨坚 《计算机网络》第二章
    KMP算法的实现详解
    Go context 原理(channel广播机制 + mutex线程安全)
    ★「C++游戏」BattleOfPhantom:大乱斗游戏升级版
    小知识· Zigbee 简介
    Unity WebView 中文输入支持
    数据中台-资产管理、数据安全
    String(二)————迭代器及相关接口使用
  • 原文地址:https://blog.csdn.net/qq_45030898/article/details/136342149
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号