0

Let's say we have got a tag font-size:65%;

How do we can change it with pt or px?

Thank you!

NoWar
  • 36,338
  • 80
  • 323
  • 498

3 Answers3

3

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));
    }
Community
  • 1
  • 1
user1424311
  • 417
  • 1
  • 6
  • 17
  • The declaration `font-size: 65%` sets the size to 65% of parent element’s font size. The code presented is based on a guess of what that size is as well as other guesses. – Jukka K. Korpela Sep 12 '12 at 21:07
2

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. But this would just mean calculating the value in a specific situation, not generally converting the relative value 65% to an absolute value.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

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:

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.

Say you have a container div 800px wide, if you have a col div in the container 384px wide that would be 48%:

384 / 800 = 48%
Delimitry
  • 2,987
  • 4
  • 30
  • 39
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52