纯 CSS(一些限制)
此解决方案基于对another solution for a similar problem I gave elsewhere 的修改。
Here is the fiddle.
它涉及到重叠伪元素的复杂关系来创建边框,这可能导致解决方案对在其中可能或不可能在其中完成的事情有一定的限制(复杂的背景也是一个问题作为某些定位方面的必要条件)。然而,它在给定的情况下起作用。
一点解释
基本上,每个.item 元素都使用:after 和:before 元素构建自己的顶部/底部边框部分,前者与.itemContainer 相关联,后者与.item 本身相关联(需要:before 在行尾创建最后一位边框)。此外,:before 还创建了右边框的“灵活”位置,以便在元素移出视图时为其提供所需的响应。这就是为什么:before 必须与.item 本身相关,也是为什么必须使用每个:after 元素的背景来“隐藏”前面:before 元素的右边框。
由于我们不通过 css 知道任何给定点的“计数”,即显示中哪个元素是“最后一个”,因此必须显示所有 :before 元素,但我们不想要右边框对于他们所有人,因此:after 需要覆盖它们。当一个元素向下移动到下一行时,它的:after 不再覆盖现在成为最后一个显示元素的右边框,显示该边框将用作整个组的“右”边框。
HTML(匹配你原来的小提琴)
<div class="itemBar">
<div class="itemContainer">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
</div>
主要项目的 CSS
.itemBar {
display: inline-block;
width: 50%; /* some width can be set, does not need to be this */
}
.itemContainer {
position: relative; /* :after pseudo-elements are positioned off this */
z-index: 1; /* needed for pseudo-element interaction */
overflow: hidden;
display: inline-block;
max-height: 68px;
width: 100%;
border-left: 1px solid black; /* left border is supplied by this */
}
.item {
width: 60px;
height: 62px;
display: inline-block;
margin: 2px;
border: 1px solid black;
/* NOTE: CANNOT be given positioning */
}
伪元素的 CSS
.item::after {
content: '';
position: absolute; /* will position off itemContainer */
z-index: -1; /* push it to the background */
top: 0; /* set it to top of itemContainer */
bottom: 0; /* set it to bottom of itemContainer */
margin-left: -100%; /* shove it past the far left edge of itemContainer */
/* next, use padding to bring it back to its position at the end
of the text string of .item */
padding-left: 100%;
/* next, add enough padding on the right to compensate for the right
padding, right margin, and right border of .item */
padding-right: 3px;
/* next, create the top and bottom border of "container",
in conjunction with the :before; so this is a pseudo-border for
.itemContainer being created by the .item elements */
border-top: 1px solid black;
border-bottom: 1px solid black;
background: #fff; /* hide other :before borders */
}
.item:before { /* make right border */
content: '';
padding-top: 66px; /* give it .itemContainer height minus border heights */
width: 100%;
margin-top: -3px; /* .item top margin + border width */
margin-left: -100%; /* pull the text in .item back into position */
margin-right: 0;
/* next, push this behind the background with an even lower z-index
to hide it if it is not the right most element beign used to
form the right border */
z-index: -2;
float: right; /* get the before element to the right */
position: relative; /* needs to be adjusted in position */
right: -4px; /* move it same as padding-right of the after element */
display: block; /* give it a display */
/* next, use it to build the fake right border and also the fake
final top/bottom borders of the of itemContainer */
border-right: 1px solid black;
border-top: 1px solid black;
border-bottom: 1px solid black;
}