简体   繁体   English

如何使边框不与另一个边框重叠,多选或单选?

[英]How to make a border not overlap another one, with multiple selections or single?

I have this sample code:我有这个示例代码:

<div class="container">
  <div class="square">1</div>
  <div class="square">2</div>
  <div class="square">3</div>
  <div class="square">4</div>
</div>

.container {
  display: flex;
  .square {
    display: inline-flex;
    width: 30px;
    justify-content: center;
    border: 1px solid black;
  }
}

And one problem is that if i add a border, it overlaps the next one.一个问题是,如果我添加一个边框,它会与下一个重叠。

边框图片

And i know that if i want to fix that issue, i can just use SASS and target all divs but not the first and last one, and this problem would be solved.而且我知道如果我想解决这个问题,我可以只使用 SASS 并定位所有 div 而不是第一个和最后一个,这个问题将得到解决。 But this solution creates a new problem: If i select only the div of number 2 and not multiple elements, the div 2 will now not have all of his borders.但是这个解决方案产生了一个新问题:如果我只选择数字 2 的 div 而不是多个元素,那么 div 2 现在将不会有他的所有边框。

How to fix the first issue, without causing a second one?如何解决第一个问题,而不引起第二个问题? I dont think this is duplicated, since i'm having a problem caused by a fix.我不认为这是重复的,因为我遇到了由修复引起的问题。

If helps, i'm using React atm.如果有帮助,我正在使用 React atm。

There are multiple solutions to this common problem.这个常见问题有多种解决方案。

Another would would be to shift every element but the first to the left by 1px (border-width)另一种方法是将除第一个元素以外的每个元素向左移动1px (边框宽度)

Assuming your selection changes the color of the border.假设您的选择会更改边框的颜色。 You can change the z-index while the element is selected.您可以在选择元素时更改 z-index。 The same technique can also be used for your explained problem with the missing borders.同样的技术也可以用于您解释的缺少边界的问题。 To visualize this I added a hover styling.为了形象化,我添加了一个悬停样式。

 .container { display: flex; } .square { display: inline-flex; width: 30px; justify-content: center; border: 1px solid black; } .square:not(:first-child) { margin-left: -1px; } .square:hover { border-color: red; z-index: 1; }
 <div class="container"> <div class="square">1</div> <div class="square">2</div> <div class="square">3</div> <div class="square">4</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM