亚像素像素值_你好亚像素世界

随着IE9及Firefox等浏览器引入亚像素字体渲染,精确获取文本尺寸成为难题。文章探讨了使用offsetWidth获取尺寸的问题,并提出利用getComputedStyle()进行更准确测量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

亚像素像素值

亚像素像素值

Since IE9 and Firefox(v.?) we now have subpixel rendering of fonts. This is cool and all but imagine this:

从IE9和Firefox(v。?)开始,我们现在有了fonts的亚像素渲染。 这很酷,但所有人都可以想象一下:

  • you have some text

    你有一些文字
  • you want to measure the width of the text and size another element to the same dimensions

    您想要测量文本的宽度并将另一个元素的大小调整为相同的尺寸

Simple.

简单。

But if you use offsetWidth/offsetHeight to measure, you get a rounded integer and not the exact dimensions.

但是,如果使用offsetWidth / offsetHeight进行测量,则会得到四舍五入的整数,而不是确切的尺寸。

See this example.

请参阅此示例

In Firefox:

在Firefox中:

sub-ff

In IE:

在IE中:

sub-ie

So sizing something based on the offsetWidth will result in the familiar "css is awesome" picture.

因此,基于offsetWidth调整内容的大小将得到熟悉的“ css很棒”图片。

The solution is to use getComputedStyle() and then round up to make more space, like:

解决方案是使用getComputedStyle()然后四舍五入以腾出更多空间,例如:

var w = Math.ceil(parseFloat(getComputedStyle(text).width)) + 'px';

In other words:

换一种说法:

offsetWidth被认为是有害的 (offsetWidth considered harmful)

Side note: getComputedStyle() doesn't exist in old IEs, but these don't have subpixel rendering either. So something like:

附注:getComputedStyle()在旧的IE中不存在,但它们也不具有亚像素渲染。 所以像这样:

var w = window.getComputedStyle
  ? Math.ceil(parseFloat(getComputedStyle(text).width)) + 'px'
  : text.offsetWidth + 'px';

More typing, but hey - sexy fonts!

输入更多,但嘿-性感字体!

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/hello-subpixel-world/

亚像素像素值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值