【问题标题】:Why do the browsers render center aligned text differently at different resolutions?为什么浏览器在不同的分辨率下会以不同的方式呈现居中对齐的文本?
【发布时间】:2020-01-14 12:09:41
【问题描述】:

我的网站上有一个“通知铃”,里面有一个数字(您有多少个通知)。该数字位于中间直到大约 1200 像素,然后稍微向左(1 像素)。 用 Photoshop 仔细观察,文本变小了 1 像素。这是怎么发生的,为什么会这样?

在 Chrome 中测试。

编辑 4: 所以这实际上只是一个渲染问题。 CSS 无能为力。

编辑 3: 这越来越奇怪了。我在删除浮动后添加了 display: inline-block ,但它又出错了。但是,删除 float: left 并删除与之配对的菜单选项会带来问题。哇... :(

编辑 2: 看来我找到了原因。这是我的菜单,菜单项有浮动:左。我去掉了左边的浮动(当然现在很乱),但数字正好到了中间。不知道为什么在较低的分辨率下不会发生这种情况。

编辑:添加屏幕截图! 左边是较小的分辨率,“0”在中间,但在较大分辨率的右边是左边 1px。我知道它几乎看不见,但它让我很烦。

一些html:

<span class="open_notifs main notif"><span class="item">0</span></span>

这里还有一些 CSS:

#cs_menu span {
    position: relative;
    display: inline-block;
    outline: 0;
    color: #FFF;
    text-decoration: none;
    letter-spacing: 1px;
    text-shadow: 0 0 1px rgba(255,255,255,.3);
    font-size: 14px;
    cursor: pointer;
    height: 35px;
    line-height: 35px;
}

#cs_menu span.notif {
    background: url(/images/notif-bell.png) no-repeat;
    padding: 0;
    margin: 0 4px;
}

#cs_menu span.item {
    padding-left: 0;
    padding-right: 0;
    width: 30px;
    text-align: center;
    line-height: initial;
    letter-spacing: 0px;
}

【问题讨论】:

  • 添加浏览器视图的屏幕截图...打开显示 css 的开发面板。
  • 看起来下图中的零是奇数像素,因此居中将偏向左侧 1px,因为第一张图片中的零是偶数,因此中心可以直接在中间。必须是字体为每个分辨率呈现的方式
  • @Pete 我认为 OP 试图询问为什么相同大小的相同字符在更高分辨率下显示不同。而“就是这样”并不是一个非常令人满意的答案。
  • 这就是为什么如果我有一个答案我会回答的@MrLister 的评论 - 最好在图形设计堆栈交换中提出这个问题,他们对字体和渲染有更多的了解
  • @Zydnar 不幸的是,这并没有帮助,但似乎我找到了原因。这是我的菜单,菜单项有浮动:左。我去掉了左边的浮动(当然现在很乱),但数字正好到了中间。不知道为什么在较低的分辨率下不会发生这种情况。

标签: html css


【解决方案1】:

我在您的 html 和 css code 中看到了一些问题。您的CSS 被多次覆盖。 span tag 与 CSS code 冲突。您可以在div 和span 之间替换通知icon 和text。我使用flexbox 到fix 这个issue。另外你还没有为background image写完整的code。

HTML

<div class="notificaiton-icon">
    <span>0<span>
</div>

CSS

.notificaiton-icon{
  align-items: center;
  background-attachment: scroll;
  background-image: url(https://hearthstonehungary.hu//images/notif-bell.png);
  background-color: transparent;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  flex-direction: row;
  height: 36px;
  justify-content: center;
  width: 30px;
}
.notificaiton-icon span{
  color: white;
  font-family: 'Roboto', sans-serif;
  font-size: 14px;
  line-height: 1;
}

Here is the Pen.

注意:使用新的element 通知此通知icon 和text 避免冲突css 覆盖问题。使用flexbox 它将帮助您center vertically 和horizontally。

【讨论】:

  • 感谢您这样做。然而,这只是证明这只是一个渲染问题。我对您的代码进行了一项修改:我将 .main-container 设置为 40px 宽度:codepen.io/Phoenixy/pen/wvBxyQq - 它仍然很好,但如果我将其设置为 41px,那就不好了:codepen.io/Phoenixy/pen/OJPwQam ... 42px 很好,不好43px,44 好,45 坏,依此类推……
猜你喜欢
  • 2011-04-01
  • 2012-03-09
  • 2018-01-07
  • 2015-12-28
  • 2011-06-24
  • 2014-05-17
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多