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>
相关推荐
徐同保13 小时前
react两个组件中间加一个可以拖动跳转左右大小的功能
前端·javascript·react.js
爱迪斯通13 小时前
MANUS:用于视觉、语言、行动模型创建的高保真第一人称数据采集设备
前端
bjzhang7513 小时前
使用 HTML + JavaScript 实现在线知识挑战
前端·javascript·html
薛定谔的猫213 小时前
Cursor 系列(3):关于MCP
前端·cursor·mcp
sheji341614 小时前
【开题答辩全过程】以 基于web的拍卖系统设计与实现为例,包含答辩的问题和答案
前端
明月_清风14 小时前
模仿 create-vite / create-vue 风格写一个现代脚手架
前端·后端
aou14 小时前
让表格式录入像 Excel 一样顺滑
前端·ai编程
前端付豪14 小时前
必知 Express和 MVC
前端·node.js·全栈
重铸码农荣光14 小时前
CSS 也能“私有化”?揭秘模块化 CSS 的防坑指南(附 Vue & React 实战)
前端·css·vue.js
南囝coding14 小时前
CSS终于能做瀑布流了!三行代码搞定,告别JavaScript布局
前端·后端·面试