简体   繁体   English

CSS网格中的意外间隙

[英]Unintentional gaps in CSS grid

I am attempting to follow this Stripe blog post: 我正在尝试关注此Stripe博客文章:

https://stripe.com/blog/connect-front-end-experience https://stripe.com/blog/connect-front-end-experience

While my final product generally looks like what was in the tutorial, my browser seems to be placing in unintentional gaps. 虽然我的最终产品通常看起来像教程中的一样,但我的浏览器似乎处于无意间的空白。 I'm reluctant to call them gaps since gap has a specific meaning in CSS grids, and my error seems to be more of a randomized rounding error. 我不愿意将它们称为“ gap”,因为“ gap”在CSS网格中具有特定的含义,而且我的错误似乎更多是随机的舍入错误。

Here are some pictures for comparison. 这是一些图片供您比较。 Safari handles the grid more elegantly than chrome, but there are still issues. Safari比chrome更优雅地处理网格,但是仍然存在问题。 Notice the pixilated white lines in the photos. 请注意照片中的像素化白线。

Stripe final product: 条纹最终产品:

条纹最终产品

My attempt on Safari: 我在Safari上的尝试:

我在Safari上的尝试

My attempt on Chrome: 我在Chrome上的尝试:

我在Chrome上的尝试

Here is the code I am using: 这是我正在使用的代码:

 .stripes { display: grid; grid: repeat(5, 200px) / repeat(10, 1fr); transform: skewY(-12deg); transform-origin: 0; } .stripes :nth-child(1) { grid-column: span 3; } .stripes :nth-child(2) { grid-area: 3 / span 3 / auto / -1; } .stripes :nth-child(3) { grid-row: 4; grid-column: span 5; } .blue { background: blue; } .red { background: red; } .special1 { background: linear-gradient(to bottom left, #1890ff, #42C6EB); } .special2{ background: linear-gradient(to left, #1890ff, #42C6EB); } .special3{ background: linear-gradient(to left, rgba(66,198,235, 0.9), rgba(66,198,235,0.2)); } .topRow { background: linear-gradient(to top, rgb(52, 173, 242), #1890ff); } .secondRow { background: linear-gradient(to top, #42C6EB, rgb(52, 173, 242)) } .thirdRow { background: #42C6EB } .fourthRow { background: linear-gradient(to top, rgba(66,198,235,0.8), #42C6EB) } .fifthRow { background: linear-gradient(to top, rgba(66,198,235,0.2), rgba(66,198,235,0.8)) } 
 <div class="stripes"> <div class="special1"></div> <div class="special2"></div> <div class="special3"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> </div> 

After playing around a little bit, it seems the the issue lies in using the linear-gradient . 在玩了一会儿之后,似乎问题出在使用linear-gradient When I switch from a gradient to a solid color, vertical bars go away, but not the tiny aliased lines running along the diagonal stripes. 当我从渐变色切换为纯色时,竖线消失了,但沿着对角线条纹延伸的细混叠线却消失了。 Any ideas? 有任何想法吗?

This is a sub-pixel rendring issue . 这是一个亚像素渲染问题 Unfortunately there is no trivial and generic fix since each browser handle the calculation differently when it comes to values smaller than 1px . 不幸的是,由于每个浏览器处理小于1px的值时,对计算的处理方式都不相同,因此没有简单而通用的修补程序。

In your case, you can add a background to whole container in order to avoid (or at least minimize) this effect: 您可以为整个容器添加背景,以避免(或至少最小化)这种影响:

 .stripes { display: grid; grid: repeat(5, 200px) / repeat(10, 1fr); background:linear-gradient(to top,rgba(66, 198, 235, 0.2),#1890ff); transform: skewY(-12deg); transform-origin: 0; } .stripes :nth-child(1) { grid-column: span 3; } .stripes :nth-child(2) { grid-area: 3 / span 3 / auto / -1; } .stripes :nth-child(3) { grid-row: 4; grid-column: span 5; } .blue { background: blue; } .red { background: red; } .special1 { background: linear-gradient(to bottom left, #1890ff, #42C6EB); } .special2{ background: linear-gradient(to left, #1890ff, #42C6EB); } .special3{ background: linear-gradient(to left, rgba(66,198,235, 0.9), rgba(66,198,235,0.2)); } .topRow { background: linear-gradient(to top, rgb(52, 173, 242), #1890ff); } .secondRow { background: linear-gradient(to top, #42C6EB, rgb(52, 173, 242)) } .thirdRow { background: #42C6EB } .fourthRow { background: linear-gradient(to top, rgba(66,198,235,0.8), #42C6EB) } .fifthRow { background: linear-gradient(to top, rgba(66,198,235,0.2), rgba(66,198,235,0.8)) } 
 <div class="stripes"> <div class="special1"></div> <div class="special2"></div> <div class="special3"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="topRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="secondRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="thirdRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fourthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> <div class="fifthRow"></div> </div> 

By the way if you are looking to achieve only the visual effect you can consider multiple background inside one container instead of all this code: 顺便说一句,如果您只想获得视觉效果,则可以考虑在一个容器中放置多个背景,而不是考虑所有这些代码:

 .container { height:1000px; transform: skewY(-12deg); transform-origin:0; background: /*special 1*/ linear-gradient(to bottom left, #1890ff, #42C6EB) 0 0/calc(3*100%/10) calc(100%/5), /*special 2*/ linear-gradient(to left, #1890ff, #42C6EB) 100% calc(100%/2)/calc(3*100%/10) calc(100%/5), /*special 3*/ linear-gradient(to left, rgba(66,198,235, 0.9), rgba(66,198,235,0.2)) 0 calc(3*100%/4)/50% calc(100%/5), /*Top row*/ linear-gradient(to top, rgb(52, 173, 242), #1890ff) 0 0/100% calc(100%/5), /*second row*/ linear-gradient(to top, #42C6EB, rgb(52, 173, 242)) 0 calc(100%/4)/100% calc(100%/5), /*third row*/ linear-gradient(#42C6EB, #42C6EB) 0 calc(100%/2)/100% calc(100%/5), /*fourth row*/ linear-gradient(to top, rgba(66,198,235,0.8), #42C6EB) 100% calc(3*100%/4)/50.1% calc(100%/5), /*fifth row*/ linear-gradient(to top, rgba(66,198,235,0.2), rgba(66,198,235,0.8)) 0 100%/100% calc(100%/5); background-repeat:no-repeat; } 
 <div class="container"> </div> 

There is a weird interaction with divs in HTML where if the next div doesn't start on the same line as the previous div ends, it creates whitespace. HTML中的div有一个怪异的交互作用,如果下一个div与上一个div不在同一行开始,它将创建空白。 There are two fixes that I have found to this. 我发现有两个修复程序。 One is to start each div on the same line as the end of the previous div. 一种是在与上一个div末尾相同的行上开始每个div。 Another is to have a comment between the two divs like so : 另一个方法是在两个div之间进行评论,如下所示:

</div><!--
--><div>

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

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