是否尊重 CSS 宽度中的小数位? [英] Are the decimal places in a CSS width respected?

查看:23
本文介绍了是否尊重 CSS 宽度中的小数位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做 CSS 设计时一直想知道的事情.

Something I've been wondering for a while whilst doing CSS design.

是否尊重 CSS 宽度中的小数位?还是四舍五入?

Are decimal places in CSS widths respected? Or are they rounded?

.percentage {
  width: 49.5%;
}

.pixel {
  width: 122.5px;
}

推荐答案

如果是百分比宽度,那么是的,尊重.正如 Martin 指出的那样,当您获得小数像素时,事情就会崩溃,但如果您的百分比值产生整数像素值(例如,在示例中为 200px 的 50.5%),您将获得合理的预期行为.

If it's a percentage width, then yes, it is respected. As Martin pointed out, things break down when you get to fractional pixels, but if your percentage values yield integer pixel value (e.g. 50.5% of 200px in the example) you'll get sensible, expected behaviour.

我已经更新了示例以显示分数像素会发生什么(在Chrome 中的值被截断,因此 50、50.5 和 50.6 都显示相同的宽度).

I've updated the example to show what happens to fractional pixels (in Chrome the values are truncated, so 50, 50.5 and 50.6 all show the same width).

这篇关于是否尊重 CSS 宽度中的小数位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆