
css 左右布局之优雅隐藏右侧面板
在 css 布局中,实现左右并列布局是常见需求。但当需要隐藏右侧面板时,又不想其内容受到挤压,该如何操作呢?
समस्या
如下 vue 代码所示,右侧面板的宽度在缩小时,其内容也会随之挤压:
立即学习“前端免费学习笔记(深入)”;
<template>
<div class="about">
<div class="lft">
<button @click="() => isshowright = !isshowright">显示/隐藏</button>
</div>
<div class="rht" :class="!isshowright ? 'closed' : ''">
<div>隐藏右侧区块后,这些内容怎么不隐藏,还会受到挤压?</div>
</div>
</div>
</template>
<script>
export default {
name: '',
data() {
return {
isshowright: true
}
},
methods: {
}
}
</script>
<style lang='css' scoped>
.about{
height: 100%;
width: 100%;
display: flex;
}
.lft, .rht{
height: 100%;
}
.lft{
flex: 1;
background: #e3e3e3;
}
.rht{
transition: all 3s;
width: 400px;
background: rgb(201, 186, 186);
}
.closed{
overflow: hidden;
width: 0;
}
</style>解决办法
要解决此问题,关键在于在不压缩右侧面板内容的情况下,改变外层宽度。
使用 white-space: nowrap 可以禁止子元素换行,从而实现上述目的。具体操作如下:
在右侧面板内套一层 div,用于包裹需要保持不被挤压的内容。
<div class="detail-area" :class="!isshowdetailpanel ? 'closed' : ''">
<div class="detail-content">
...
</div>
</div>设置外层 div 宽度为 350px,高 100%,overflow 为 hidden。内层 div 采用绝对定位,且宽高均为 100%,设置 white-space 为 nowrap。
.detail-area{
width: 350px;
height: 100%;
background: $bg-block;
transition: all $delay;
margin-left: 2px;
box-sizing: border-box;
display: flex;
position: relative;
&.closed{
width: 0;
}
.detail-content{
padding: 10px;
box-sizing: border-box;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
white-space: nowrap;
}
}采用这种方式,右侧面板内的内容不会在宽度改变时受到影响,从而实现优雅隐藏。
以上就是在 CSS 中,如何优雅地隐藏并列布局中的右侧面板而不挤压其内容?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号