html - Container width based on child flex items
问题描述
I'm trying to make a sidebar that aligns vertically an undetermined number of list items. When the number of items reaches the bottom of the sidebar, they are grouped in columns via flex.
My problem is that I want the width of the sidebar to be 100px wide when closed and when opened, I want it to be the width of the columns. If I use a fixed width for the open state, this happens:
HTML:
<nav class="sidebar">
<ul>
<li>
1
</li>
<li>
2
</li>
<li>
3
</li>
<li>
4
</li>
<li>
5
</li>
<li>
...
</li>
</ul>
</nav>
SCSS:
.sidebar {
background: #ccc;
width: 100px;
ul {
list-style-type: none;
height: 100vh;
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: flex-start;
li {
background: #000;
height: 80px;
width: 80px;
}
}
&:hover
{
//width: !?!?!?!
}
}
How can i accomplish this? How can a open sidebar and show all the columns?
UPDATE: The height of the sidebar can vary depending on the screen size. I updated the code with the original settings.
UPDATE #2: I tested both solutions, although @itay-gal was a clever CSS only option, it lacked the ability to animate. Since i'm already using JS, i ended up using @pablo-darde solution.
I made a small change in @pablo-darde's so that it could work with a variable height sidebar:
const sidebar = document.querySelector('.sidebar');
const ul = sidebar.querySelector('ul');
const li = ul.querySelector('li');
const showRemainder = () => {
const ulHeight = ul.clientHeight;
const liHeight = li.clientHeight;
const width = ul.clientWidth * Math.ceil(ul.children.length / (ulHeight / liHeight));
sidebar.style.maxWidth = `${width}px`;
}
解决方案
I really attempted to code only with CSS and your code, but without success. I reached the desired result with some JavaScript code. Please, let me know if you can get some more elegant.
Note the css pointer-events: none;
property in your li
elements. It is very important here. If you want to use href
in li
s, I believe some refactoring will be needed.
const sidebar = document.querySelector('.sidebar');
const ul = sidebar.querySelector('ul');
const showRemainder = () => {
const width = ul.clientWidth * Math.ceil(ul.children.length / 5);
sidebar.style.maxWidth = `${width}px`;
}
const hideRemainder = () => {
sidebar.style.maxWidth = '80px';
}
sidebar.addEventListener('mouseover', showRemainder);
sidebar.addEventListener('mouseout', hideRemainder);
.sidebar {
background: #ccc;
max-width: 80px;
width: auto;
height: 400px;
overflow: hidden;
transition: all 0.5s ease;
}
.sidebar ul {
list-style-type: none;
height: 400px;
display: flex;
padding: 0;
margin: 0;
flex-flow: column wrap;
align-content: flex-start;
}
.sidebar ul li {
display: flex;
pointer-events: none;
justify-content: center;
align-items: center;
background: #000;
height: 80px;
width: 80px;
color: #fff;
box-sizing: border-box;
border: 1px solid #fff;
}
<nav class="sidebar">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
<li>11</li>
<li>12</li>
<li>13</li>
</ul>
</nav>
推荐阅读
- c# - 仅托管 - 不保留 FpSpread 数据源字体大小写
- xslt - 如何通过@variable在xsl中添加样式属性
- python - 引发异常,处理它。只是再次举起它;Python
- bazaar - bzr 无法完成拉取,现在文件丢失
- powershell - 如何从 cmdlet 命令获取属性并将它们作为整数保存到变量中
- ansible - 覆盖特定角色的 Ansible 主机
- google-chrome - Chrome 以后会支持 CCID USB 设备吗
- python - 在 django 中操作 URL - 中间件
- reactjs - 无法解决在 reactjs 中显示为未定义状态的错误?
- html - 在 html 的 span 类中显示向下箭头,就像代码显示中的加号一样?