【发布时间】:2017-08-26 03:29:43
【问题描述】:
我想使用 css 列将项目列表排列为三列。当我点击某些项目时,我会使用 JS 来扩展它们,当这种情况发生时,我不希望调整布局来弥补。这可能吗?
$('.clickme').click(function(){
$(this).toggleClass('big');
});
/* relevant CSS */
ul{
display:block;
max-width:640px;
list-style:none;
margin:0;
padding:0;
}
.box1 ul{
columns: 200px 3;
}
.box1 li{
break-inside: avoid;
}
.box2 ul{
width: 200px;
float: left;
}
li{
display:block;
background-color:#555;
color:white;
border: 1px solid white;
padding: 4px;
min-height:1px;
}
/* less relevant CSS */
body{font-family: sans-serif;}
.clickme{
transition: min-height .5s;
}
.clickme:hover{
background-color:#999;
cursor: pointer;
}
.clickme.big{
min-height:100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="box1">
<h3>I don't want this - when an element expands the other elements rearrange themselves.</h3>
<ul>
<li>item</li>
<li>item</li>
<li>some of these items are longer than others</li>
<li>item</li>
<li>some of these items are longer than others</li>
<li>item</li>
<li class="clickme">click me</li>
<li>item</li>
<li>item</li>
<li>some of these items are longer than others</li>
<li>item</li>
<li>item</li>
</ul>
</div>
<div class="box2">
<h3>This is what I want - when an element expands the other elements stay put. I'm using multiple lists for this though, and I'd like to use just one list if possible.</h3>
<ul>
<li>item</li>
<li>item</li>
<li>some of these items are longer than others</li>
<li>item</li>
</ul>
<ul>
<li>some of these items are longer than others</li>
<li>item</li>
<li class="clickme">click me</li>
<li>item</li>
</ul>
<ul>
<li>item</li>
<li>some of these items are longer than others</li>
<li>item</li>
<li>item</li>
</ul>
</div>
这是我用于列的 CSS:
.box1 ul{
columns: 200px 3;
}
这里有一个代码笔来说明 - http://codepen.io/anon/pen/YNpJYG
注意:这显然不太理想,但如果可以让 CSS 列根据元素的数量而不是它们的大小来创建列,那可能对我有用。
【问题讨论】:
-
不幸的是,做你想做的事情的唯一方法是使用提供的示例中的多个列表。无法将每列设置为具有一定数量的元素。
标签: html css css-multicolumn-layout