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

时间:2012-09-12 17:52:06

标签: c# .net html css

假设我们有一个标记font-size:65%;

我们如何使用ptpx

进行更改

谢谢!

3 个答案:

答案 0 :(得分:3)

看来这篇文章here是一个很好的资源。

来自here的代码似乎与您的需求接近,特别是:

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;
        }

在第二眼看来,似乎这样的东西更准确,但我还没有真正测试过它。

    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));
    }

答案 1 :(得分:2)

你不能,除了你可以将米转换为秒。

如果您知道某个单元中元素中父级的字体大小,则可以通过将父字体大小的数值乘以数字0.65来计算同一单位中font-size: 65%对应的计算值。 。但这只是意味着在特定情况下计算价值,而不是通常将相对值65%转换为绝对值。

答案 2 :(得分:1)

我怀疑.net中的prebuild功能可以在不编写自己的转换器的情况下轻松帮助您完成此功能,但这个jquery转换器可能对您有所帮助:

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

为了帮助您,如果您知道宽度,可以使用以下公式计算出px。

假设你有一个800px宽的容器div,如果你在384px宽的容器中有一个col div,那将是48%:

384 / 800 = 48%
相关问题