码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • css flex 踩坑总结


    css 的 flex 布局虽然好用,但坑也不少。整理如下:

    (一)使用 flex 的 center 时,如果本身尺寸 > 父容器尺寸,则内容会显示不全

    bug描述

    想要的 center 效果是:

    • 元素尺寸小时,在父元素里居中显示

    • 元素尺寸超过父元素时,父元素出滚动条
      在这里插入图片描述在这里插入图片描述

    但 flex 的 center 是有bug 的(justify-content:center 和 align-items:center):

        <style>
          #container {
            display: flex;
            width: 100px;
            height: 100px;
            outline: 1px solid #aaa;
            justify-content: center;
            align-items: center;
            overflow: auto;
          }
          #content {
            background: pink;
            font-size: 100px;
          }
        style>
        <div id="container">
          <div id="content">helloWorlddiv>
        div>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    效果:
    在这里插入图片描述
    当元素尺寸超过父容器时,无论是水平方向还是竖直方向,元素内容都无法显示全。

    解决方案:

    能确保元素尺寸一定小于父容器时,可以用 justify-content:center 和 align-items:center;不然别用。

    想要 center 的话,用 margin: auto 来替代:

    • 水平方向,元素设置 margin-left:auto; margin-right:auto;,父元素设置 display:flex (如果不给父元素设置 display:flex,不会影响子元素的水平居中效果。但是子元素的背景会被切断)
       <style>
        #container {
          display: flex;
          width: 100px;
          height: 100px;
          outline: 1px solid #aaa;
          overflow: auto;
        }
        #content {
          background: pink;
          font-size: 50px;
          margin-left: auto;
          margin-right: auto;
        }
      style>
      <div id="container">
        <div id="content">helloWorlddiv>
      div>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    • 竖直方向,元素设置 margin-top :auto; margin-bottom:auto;,父元素设置 display:flex
          <style>
          #container {
            display: flex;
            width: 300px;
            height: 80px;
            outline: 1px solid #aaa;
            overflow: auto;
          }
          #content {
            background: pink;
            font-size: 100px;
            margin-top: auto;
            margin-bottom: auto;
          }
        style>
        <div id="container">
          <div id="content">heydiv>
        div>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18

    参考:

    • Can’t scroll to top of flex item that is overflowing container

    (二)flex 布局中 overflow:hidden 失效

    需要加 min-width:0 ,具体见 flex布局中 text-overflow:ellipsis 失效

    所以,常见的左定宽,右伸缩布局应该这样写:

    .left{
    	flex: 0 0 300px;	
    }
    right{
    	flex: 1 1 auto;
    	min-width: 0; /* 必不可少!因为后代元素中难免有 overflow:hidden 的 */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    (n, 0)维NumPy数组 和 (n, 1)维NumPy数组 有什么区别?
    【openwrt】libubox组件——uloop_process
    SpringBoot 实现 阿里云语音通知(SingleCallByTts)
    Linux脚本练习之script086-etstat练习之查看各个状态的连接数
    Java安全 CC链6分析
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    前端学习开发之【Vue】-中
    【华为OD机试真题 python】 停车场车辆统计【2022 Q4 | 100分】
    使用Python进行数据分析入门
    【毕业设计】基于java+swing的模拟写字板设计与实现(毕业论文+程序源码)——模拟写字板
  • 原文地址:https://blog.csdn.net/tangran0526/article/details/126297403
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号