简体   繁体   English

如何使用pt或px正确更改font-size:x%?

[英]How to change correctly font-size:x% with pt or px?

Let's say we have got a tag font-size:65%; 假设我们有一个标记font-size:65%;

How do we can change it with pt or px ? 如何使用ptpx更改它?

Thank you! 谢谢!

It looks like this post here is a good resource. 它看起来像这个帖子这里是一个很好的资源。

The code from here seems to be close to what you need, specifically: 这里的代码似乎很接近您的需求,尤其是:

switch (unitSize.Unit.Type)
        {
            case UnitType.Pixel:
                result = (int)Math.Round(unitSize.Unit.Value);
                break;
            case UnitType.Point:
                result = (int)Math.Round(unitSize.Unit.Value*1.33);
                break;
            case UnitType.Em:
                result = (int)Math.Round(unitSize.Unit.Value * 16);
                break;
            case UnitType.Percentage:
                result = (int)Math.Round(unitSize.Unit.Value * 16 / 100);
                break;
            default:
                // other types are not supported. just return the medium
                result = 16;
                break;
        }

After a second glance it seems like something like this is more accurate but I have not really tested it throughout. 再看一眼,看起来像这样更准确,但是我并没有真正对其进行全面测试。

    public int PercentToPoint(int percent)
    {
        return (int)Math.Round(Convert.ToDouble(percent * 12 / 100));
    }

    public int PercentToPixel(int percent)
    {
        return (int)Math.Round(Convert.ToDouble(percent * 16 / 100));
    }

You cannot, any more than you can convert meters to seconds. 您无法将米转换为秒。

If you know the font size of the parent in element in some unit, then you can calculate the computed value corresponding to font-size: 65% in that same unit by multiplying the numeric value of the parent font size by the number 0.65. 如果您知道父in元素的字体大小以某个单位为单位,则可以通过将父字体大小的数值乘以数字0.65来计算对应于font-size: 65%的计算值font-size: 65%以相同单位为font-size: 65% But this would just mean calculating the value in a specific situation, not generally converting the relative value 65% to an absolute value. 但这仅意味着在特定情况下计算值,而不是通常将相对值65%转换为绝对值。

I doubt the is a prebuild function in .net that would easily help you do it without writing your own converter but this jquery converter might help you: 我怀疑是.net中的prebuild函数是否可以轻松地帮助您完成此任务,而无需编写自己的转换器,但是此jquery转换器可能会帮助您:

http://www.headcrash.us/blog/2011/10/jquery-plugin-to-convert-css-pixels-to-em-pt-percent-and-other-units/ http://www.headcrash.us/blog/2011/10/jquery-plugin-to-convert-css-pixels-to-em-pt-percent-and-other-units/

To help you more, if you know the width, you can use the below formula to figure out px. 为了更好地帮助您,如果您知道宽度,则可以使用以下公式来计算px。

Say you have a container div 800px wide, if you have a col div in the container 384px wide that would be 48%: 假设您的容器div宽度为800px,如果容器中的col div宽度为384px,则为48%:

384 / 800 = 48%

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

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