[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


CSS Font Width Calculation

Posted on 2010-01-04
Medium Priority
Last Modified: 2012-06-21
Hi All,

The issue that I've come across is that I have a label that displays the month of the year and I need to calculate the width of the month so that all months are approximately 130 pixels in length. What I was hoping to do is to change the letter-spacing style on the fly but I'm having a real hard time getting the width of the text. So for example if I am using Arial and a font size of 16 I need to find the calculation to make sure that the month of JULY is approximately the same width as DECEMBER.  

The System.Drawing.Graphics class for windows development has a method called MessageString but I cannot seem to find a similar method for ASP .NET. Also I tried to cheat and just get the Label.Width.Value and do a calculation with that but it returns a value of 0 in the Page_Load method. Also if there is a slick way to do this just using CSS I'd love to know it.

Any help would be greatly appreciated

Question by:aiuWebServices

Expert Comment

ID: 26175443
can I ask why they need to be the same width?  It may look weird if you do not change the hight also, this could possibly stretch your letters to look funny on months like july.
LVL 96

Expert Comment

by:Bob Learned
ID: 26175453
I believe that what you are talking about concerns using kerning techniques.

Understanding Typography Controls with CSS

"Kerning and Leading
Probably the most interesting of the new properties are the kerning and leading settings. If you don't already know, kerning is the spacing between letters and leading is the spacing between lines. Leading controls are an especially nice feature of CSS, as there's no such thing as one-size-fits-all leading. Some fonts need more, while others need less to make them more readable."
LVL 12

Accepted Solution

kumar754 earned 2000 total points
ID: 26176055
this will give you width of a font in pixels:

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100,100);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
float fontWidthInPixel = g.MeasureString("July", new System.Drawing.Font("Arial", 16, System.Drawing.GraphicsUnit.Pixel)).Width;

rest you can figure out what to do with the font width and to manage your code accordingly.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question