Reputation: 2832
I've designed one web site from asp.net & in that i designed the html pages with the help of css. In that css all the measurement that i've taken are in pixel. I want to convert all from pixel to percentage?
Is there any conversion formula for this?
how to do this?
thanks.
Upvotes: 0
Views: 12722
Reputation: 11
I use the following formula when converting for a flexible layout from a static layout:
result = 100 * target / context
where
target
is what you want to changecontext
is (usually) the page width you want to work with.For example, if you have a block element <section>
that currently has a width of 330px and you are working in a wrapper of 1024px, you could use 330/1024*100
which gives you a result of 32.23% for your width. However, as a rule, for fonts you want to convert to em
rather than percentages.
Upvotes: 1
Reputation: 5579
To change the width of a block element to a percentage value, you'll have to know the width of its container in the same units.
var widthChildPercent = widthChildPixels / widthParentPixels;
Obviously, you'll likely want to format that to an integer value when you update the style, but that will be the general formula.
Upvotes: 1