css实现元素内容过高时自动出现展开收起按钮

1. css

css 复制代码
.content {
    width: 600px;
    margin: 0 auto;
    max-height: 200px;
    overflow: hidden;
    border-radius: 4px;
    outline: 2px dashed royalblue;
}

.section {
    display: flex;
}
  
.content::before {
    content: '';
    width: 100px;
    height: 100%;
    float: left;
}

.btn {
    float: right;
    width: 100px;
    text-align: center;
    position: relative;
    left: calc(50% - 50px);
    transform: translateY(-100%);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.btn::after{
    content: '';
    display: block;
    height: 34px;
    background-color: #666;
    transition: .2s background-color;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'%3E %3Cpath d='M143 352.3L7 216.3c-9.4-9.4-9.4-24.6 0-33.9l22.6-22.6c9.4-9.4 24.6-9.4 33.9 0l96.4 96.4 96.4-96.4c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9l-136 136c-9.2 9.4-24.4 9.4-33.8 0z'%3E%3C/path%3E %3C/svg%3E") center/ 24px 24px no-repeat;
}

.btn:hover::after {
    background-color: royalblue;
}

.btn::before {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 34px;
}

.text {
    box-sizing: border-box;
    width: 100%;
    padding: 10px 15px;
    float: right;
    line-height: 1.5;
    margin: 0;
    margin-left: -100px;
    font-size: 18px;
    color: #232323;
}

.checkbox:checked+.content{
    max-height: fit-content;
}

.checkbox:checked+.content .btn{
    left: auto;
    right: calc(50% - 50px);
}

.checkbox:checked+.content .btn::after{
    transform: scaleY(-1);
}

2. html

css 复制代码
<div className={style.section}>
     <input className={style.checkbox} type="checkbox" id="c1" hidden></input>
     <div className={style.content}>
          <div className={style.text}>
              hello1 <br />
              hello2 <br />
              hello3 <br />
              hello4 <br />
              hello5 <br />
              hello6 <br />
              hello7 <br />
              hello8 <br />
              hello1 <br />
              hello2 <br />
              hello3 <br />
              hello4 <br />
              hello5 <br />
              hello6 <br />
              hello7 <br />
              hello8 <br />
          </div>
          <label className={style.btn} htmlFor="c1"></label>
      </div>
  </div>
相关推荐
鹏程十八少2 分钟前
8. Android 深入插件化Shadow源码:揭秘插件Activity启动的完整链路(源码解析)
java·前端·面试
wuhen_n3 分钟前
Function Calling解剖:从请求到响应的完整数据流
前端·人工智能·ai编程
喝咖啡的女孩4 分钟前
多智能体任务可视化界面
前端
whisper5 分钟前
#新手必看!Map.size 和 Object.keys().length 的区别,看完再也不混淆
前端
秋天的一阵风8 分钟前
【LeetCode 刷题系列|第 3 篇】详解大数相加:从模拟竖式到简洁写法的优化之路🔢
前端·算法·面试
假面骑士阿猫10 分钟前
TRAE配置OpenSpec实现SDD规范驱动开发
前端·人工智能·代码规范
哈哈哈哈哈哈哈哈85313 分钟前
WSL + Tailscale 导致 apt update 卡 0% 的解决方案
前端
JYeontu14 分钟前
程序员都是这样剪视频的?
前端
小雨cc5566ru15 分钟前
基于Nodejs+vue+ElementUI的大学生课程排课管理系统设计
前端·vue.js·elementui
qq_84061223315 分钟前
Nodejs+vue+ElementUI框架的家政服务评价系统 保洁员预约系统的设计与实现
前端·vue.js·elementui