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>
相关推荐
EndingCoder3 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客4 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro5 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom5 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...6 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡7 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜058 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx8 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9998 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o9 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构