css使用弹性盒,让每个子元素平均等分父元素的4/1大小

css使用弹性盒,让每个子元素平均等分父元素的4/1大小

原本:

复制代码
           ul {
                padding: 0;
                width: 100%;
                background-color: rgb(74, 80, 62);
                display: flex;
                justify-content: space-between;
                flex-wrap: wrap;

                li {
                  /* 每个占4/1 */
                  overflow: hidden;
                  background-color: rgb(30, 15, 46);
                  display: flex;
                  justify-content: space-between;
                  align-items: center;
                  flex-direction: column;
                }

修改后:

复制代码
                li {
                  padding: 0;
                  border: 1px solid #00bcd4;
                  background-color: rgb(30, 15, 46);
                  display: flex;
                  justify-content: space-between;
                  align-items: center;
                  flex-direction: column;
                  flex: 0 0 50%; /* 宽度设置为父元素的1/2 */
                  max-width: 50%; /* 最大宽度设置为父元素的1/2 */
                  height: 50%; /* 高度设置为父元素的1/4 */

                  &:nth-child(2n) {
                    margin-right: 0; /* 每行的第二个li元素移除右边距 */
                  }

                  &:nth-child(odd) {
                    margin-bottom: 0; /* 每列的第一个li元素(奇数位置)移除下边距 */
                  }

                  &:nth-last-child(-n + 2) {
                    margin-bottom: 0; /* 最后两个li元素移除下边距 */
                  }
                }
              }
相关推荐
我是苏苏23 分钟前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端
无羡仙43 分钟前
Vue插槽
前端·vue.js
哈__1 小时前
React Native 鸿蒙跨平台开发:PixelRatio 像素适配
javascript·react native·react.js
用户6387994773052 小时前
每组件(Per-Component)与集中式(Centralized)i18n
前端·javascript
SsunmdayKT2 小时前
React + Ts eslint配置
前端
开始学java2 小时前
useEffect 空依赖 + 定时器 = 闭包陷阱?count 永远停在 1 的坑我踩透了
前端
zerosrat2 小时前
从零实现 React Native(2): 跨平台支持
前端·react native
狗哥哥2 小时前
🔥 Vue 3 项目深度优化之旅:从 787KB 到极致性能
前端·vue.js
青莲8432 小时前
RecyclerView 完全指南
android·前端·面试
青莲8432 小时前
Android WebView 混合开发完整指南
android·前端·面试