html - CSS:如何以一列覆盖另一列的方式组织两列?
问题描述
我希望将页面分成两列,其中一列应覆盖另一列。我一直无法做到这一点,所以我请大家帮忙。请忽略凌乱的 svg 代码,它是从 inkscape 复制的,因为我想在其中包含一些可点击的元素。
第一/左列(测试列)仅在有要迭代的列表时出现,否则隐藏。我在下面的脚本中使用的 flexbox 解决方案的问题是,当第一列出现时,第二列是偏移的。我希望它只出现在黑色方块周围区域的页面“上方”,而不移动任何其他元素。我怎么能做到这一点?
.svg_container {
width: 100%;
background: #fff;
}
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
border: 2px solid pink;
}
.test {
display: flex;
justify-content: flex-start;
align-items: center;
}
.svg {
width: 100%;
flex: 5;
height: 90vh;
}
<html>
<body>
<main>
<div class="svg_container">
<div class="row">
<div class="test column">
<p>List Header</p>
<p>Elem 1</p>
<p>Elem 2</p>
<p>Elem 3</p>
<p>Elem 4</p>
<p>Elem 5</p>
</div>
<svg
viewBox="0 0 219.64919 197.34595"
height="197.34595mm"
width="219.64919mm"
class="svg column">
<g transform="translate(-4.3242022,-42.81862)" id="layer1" inkscape:groupmode="layer" inkscape:label="Layer 1">
<g
transform="matrix(0.26458333,0,0,0.26458333,-52.392597,15.930767)"
id="g768">
<g id="group1780-42" transform="translate(317.239,-185.674)" >
<title id="title2168">Square</title>
<g id="shape1781-43" >
<title id="title2170">Square</title>
<path d="M 0,841.89 H 559.56 V 348.66 H 0 Z" class="st3" id="path2172" />
<text x="253.13" y="401.75" class="st4" id="text2176">Laboratory
<tspan x="257.67999" dy="1.2em" class="st5" id="tspan2174">86 sq. m.</tspan>
</text>
</g>
</g>
</g>
</g>
</svg>
</div>
</div>
</main>
</body>
</html>
代码也保存为jsfiddle
解决方案
由于您希望侧边栏“覆盖”它需要具有的 svg 区域(内容)position: absolute
。这是一个例子:
function toggle () {
const sidebar = document.querySelector('.test.column');
sidebar.classList.toggle('hide');
}
html, body, main {
height: 100vh;
}
.svg_container {
width: 100%;
background: #fff;
height: 100%;
}
.test.column {
background-color: gray;
position: absolute;
height: 100%;
top: 0;
left: 0;
flex-direction: column;
flex-basis: 100%;
flex: 1;
border: 2px solid pink;
display: flex;
justify-content: flex-start;
align-items: center;
width: 200px;
}
.column.hide {
display: none;
}
.content {
background-color: teal;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
}
.svg {
width: 100%;
flex: 5;
height: 90vh;
}
<html>
<body>
<main>
<div class="svg_container">
<div class="test column">
<p>List Header</p>
<p>Elem 1</p>
<p>Elem 2</p>
<p>Elem 3</p>
<p>Elem 4</p>
<p>Elem 5</p>
</div>
<div class="content">
<button onclick="toggle()"> Show/Hide </button>
</div>
</div>
</div>
</main>
</body>
</html>
推荐阅读
- javascript - 为什么使用 Express 应用时 setTimeout 仍然有效?
- python - 合并在 Pandas 中返回 nan
- tensorflow - `batch` 在 Tensorflow 数据集管道创建中解决了什么问题,它如何与训练中使用的(迷你)批量大小交互?
- jenkins - jenkins 在环境声明中访问环境变量
- oracle - 为 Oracle 设置 ODBC 连接
- java - 如何检查 ArrayList 中的前两个字母是否等于某个值,然后在 java 中增加总和?
- graphics - 在 Domino Designer 中,根据用户输入在表单上绘制矩形的最佳方法是什么?
- oauth-2.0 - JWT 是 OAuth2 的实现吗
- imagemagick - 使用 convert 垂直连接多个图像会产生许多输出
- google-cloud-platform - GCP 上的 GoDaddy 域不安全