简体   繁体   English

如何在多个div周围放置边框

[英]How to place border around multiple divs

I want to have a border around multiple divs, so I was trying to set the border around the parent div.我想在多个 div 周围设置边框,所以我试图在父 div 周围设置边框。 However, this leads to a slight gap between the children and the parent (One of the children will have a different background color).但是,这会导致子项和父项之间存在细微差距(其中一个子项将具有不同的背景颜色)。 Is there a way to prevent this behavior?有没有办法防止这种行为?

https://jsfiddle.net/fpcg2x07/8/ https://jsfiddle.net/fpcg2x07/8/

 .container { display: inline-flex; border: 1px solid black; } .child { padding: 0.5rem; } .child:first-child { background-color: red; }
 <div class="container"> <div class="child"> child1 </div> <div class="child"> child2 </div> </div>

Thanks!谢谢!

The reason is still to be figured out.原因还有待查明。 Maybe this happens because of the 0.5rem padding that for some reason doesn't coincide well with the fact that zoom of browser is different than 100%.可能发生这种情况是因为 0.5rem 填充由于某种原因与浏览器缩放不同于 100% 的事实并不吻合。 That's why px unit is better(?) Should I use px or rem value units in my CSS?这就是为什么px单位更好(?) 我应该在我的 CSS 中使用 px 还是 rem 值单位? ) )

But here's a workaround (using borders for the child indeed, but only top and bottom).但这是一种解决方法(确实为孩子使用边框,但仅使用顶部和底部)。

 .container { display: inline-flex; border-left: 1px solid black; border-right: 1px solid black; } .child { padding: 0.5rem; border-top: 1px solid black; border-bottom: 1px solid black; } .child:first-child { background-color: red; }
 <div class="container"> <div class="child"> child1 </div> <div class="child"> child2 </div> </div>

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

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