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>
相关推荐
豆豆33 分钟前
为什么用PageAdmin CMS建设网站?
服务器·开发语言·前端·php·软件构建
看到请催我学习1 小时前
如何实现两个标签页之间的通信
javascript·css·typescript·node.js·html5
twins35202 小时前
解决Vue应用中遇到路由刷新后出现 404 错误
前端·javascript·vue.js
qiyi.sky2 小时前
JavaWeb——Vue组件库Element(3/6):常见组件:Dialog对话框、Form表单(介绍、使用、实际效果)
前端·javascript·vue.js
煸橙干儿~~2 小时前
分析JS Crash(进程崩溃)
java·前端·javascript
安冬的码畜日常2 小时前
【D3.js in Action 3 精译_027】3.4 让 D3 数据适应屏幕(下)—— D3 分段比例尺的用法
前端·javascript·信息可视化·数据可视化·d3.js·d3比例尺·分段比例尺
l1x1n03 小时前
No.3 笔记 | Web安全基础:Web1.0 - 3.0 发展史
前端·http·html
昨天;明天。今天。3 小时前
案例-任务清单
前端·javascript·css
zqx_74 小时前
随记 前端框架React的初步认识
前端·react.js·前端框架
惜.己4 小时前
javaScript基础(8个案例+代码+效果图)
开发语言·前端·javascript·vscode·css3·html5