有没有办法检测屏幕尺寸是偶数还是奇数,以便我可以根据结果制定 CSS 规则?

发布于 2025-01-15 06:22:31 字数 1039 浏览 5 评论 0原文

我目前正在开发我的作品集网站,我注意到主页上有一条线,根据屏幕尺寸的偶数或奇数移动 1 像素。

如果显示宽度是奇数(例如 1141px),则该线居中。如果是 1140px,则不再居中。

这是导致问题的 css

&:before {
      content: '';
      display: block;
      position: absolute;
      top: calc(-3.5rem - 1px);
      left: calc(50% - #{_size(border-width) * 1});
      width: _size(border-width);
      height: calc(3.5rem + 1px);
      background: _palette(border);
    }

具体是这一行:

left: calc(50% - #{_size(border-width) * 1});

网站是:https://www.Kyle-Richey.com

所讨论的线是页面底部之间的垂直线菜单和水平线。

第一个图像的显示宽度为偶数且线偏离中心,第二个图像的显示宽度为奇数且线位于中心。

理想情况下,如果可能的话,我会编写一些代码来根据显示宽度是偶数还是奇数来更改 css 规则。

均匀显示宽度。线偏离中心。

奇数显示宽度。线条居中

I am currently working on my portfolio website and I have noticed that I have a line on the home page that moves by 1 pixel based on the screen size being an even or odd number.

If the Display width is an odd number like 1141px the line is centered. If it is 1140px it is no longer centered.

Here is the css that is causing the issue

&:before {
      content: '';
      display: block;
      position: absolute;
      top: calc(-3.5rem - 1px);
      left: calc(50% - #{_size(border-width) * 1});
      width: _size(border-width);
      height: calc(3.5rem + 1px);
      background: _palette(border);
    }

Specifically it is this line:

left: calc(50% - #{_size(border-width) * 1});

Website is: https://www.Kyle-Richey.com

The Line in question is the vertical line at the bottom of the page between the menu and horizontal line.

The First image is of an even display width and the line is off center, the second is an odd display width and the line is center.

Ideally I would write some code that would change the css rule based on the display width being even or odd if that is possible.

Even Display Width. Line is off center.

Odd Display Width. Line is centered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-01-22 06:22:31

计算 left: calc(50% - #{_size(border-width) * 1}); 可能会得出像素的百分比,并且会向上或向下舍入到最接近的值导致未对齐的像素。

不知道 #{_size(border-width) 正在做什么,很难给出准确的答案。
但将 left: calc(50% - #{_size(border-width) * 1}); 交换为 left: calc(50% - 0.5px); 似乎修复了我在 Chrome 中的快速测试中的问题。

It's possible that the calculation left: calc(50% - #{_size(border-width) * 1}); is resulting in a percentage of a pixel and that get's rounded up or down to the nearest pixel causing the miss alignment.

Not knowing what #{_size(border-width) is doing it's hard to give an exact answer.
but swapping left: calc(50% - #{_size(border-width) * 1}); to left: calc(50% - 0.5px); appears to fix the issue in my quick test in Chrome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文