
如何在父元素内实现子元素的两行排列?
问题:
我想在红色框形父元素内排列子元素,使它们形成两行。当超出父元素范围时,初始状态下隐藏多余内容,并显示省略号按钮。点击该按钮时,隐藏内容将展开,并出现水平滚动条。
在线演示
HTML:
<div id="container" class="hidden">
<div id="flex-container">
<div class="item">item1</div>
<div class="item">item2</div>
<div class="item">item3</div>
<div class="item">item4</div>
<div class="item">item5</div>
<div class="item">item6</div>
<div class="item">item7</div>
<div class="item">item8</div>
<div class="item">item9</div>
<div class="item">item10</div>
<div id="more">...</div>
</div>
</div>CSS:
#container {
width: 400px;
height: 200px;
border: 1px solid red;
position: relative;
}
#container.hidden {
overflow-x: hidden;
overflow-y: hidden;
}
#container.scroll {
overflow-x: scroll;
overflow-y: hidden;
}
#flex-container {
width: 800px;
height: 200px;
background: pink;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
#flex-container .item {
width: 150px;
text-align: center;
background: yellow;
height: 94px;
display: flex-item;
}
#more {
display: block;
position: absolute;
top: 160px;
right: 10px;
z-index: 5;
width: 80px;
height: 30px;
background: green;
border-radius: 5px;
text-align: center;
color: white;
cursor: pointer;
}JavaScript:
document.getElementById("more").onclick = function() {
this.style.display = "none";
document.getElementById("container").className = "scroll";
}基本原理:
以上就是如何实现父元素内子元素两行排列,超出部分隐藏并显示省略号按钮?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号