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>
相关推荐
风骏时光牛马9 分钟前
AIAgent高可用架构:弹性容错与故障自愈的落地实践
前端
IT_陈寒17 分钟前
Vue的响应式什么时候会失灵?这个坑我踩了
前端·人工智能·后端
烟锁池塘柳029 分钟前
解决 Firefox 浏览器网页文本选中高亮不明显问题(含仅修改特定网站高亮显示的方案)
前端·firefox
Patrick_Wilson31 分钟前
Web 认证方案技术指南
前端·后端·面试
YuJie1 小时前
JSBridge 基础知识
前端·javascript
BigTopOne1 小时前
Ubuntu 虚拟机编译 WebRTC Android AAR(M140 / branch-heads/7339)
前端
默_笙1 小时前
🍕 后端接口还没写好,前端已经跑起来了?Mock 数据 + axios 接口层,前后端再也不互相"等"
前端·javascript
书源1 小时前
AI 时代写给前端同行:什么在贬值,什么在涨价
前端·程序员·ai编程
爱勇宝1 小时前
客户只想看个页面,我却做了一个静态演示发布系统
前端·javascript·后端
渔夫正在掘金1 小时前
告别构建时代:为什么 AI 编程浪潮正让 Vue 与 React 走向落后?
前端