2012-09-12 14 views

回答

2

你不能,任何比你可以转换米到秒。

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

1

我怀疑是在.NET中的预生成功能,将很容易帮助你做到这一点,而无需编写自己的转换器,但这个jQuery转换器可以帮助你:

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

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

假设你有一个div容器800像素宽,如果你有在容器384px宽的一处山坳DIV,这将是48%:

384/800 = 48% 
+0

谢谢!我需要在纯C#中使用的东西。 –

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

非常感谢你! –

+0

声明'font-size:65%'将大小设置为父元素字体大小的65%。所提供的代码基于猜测什么尺寸以及其他猜测。 –