CSS Font Width Calculation

Posted on 2010-01-04
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
    LVL 3

    Expert Comment

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

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
    In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now