Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:35:28+00:00 2026-05-27T01:35:28+00:00

When using a fixed width font , I’d like to specify the width of

  • 0

When using a fixed width font, I’d like to specify the width of an HTML element in characters.

The “em” unit is supposed to be the width of the M character, so I should be able to use it to specify a width. This is an example:

<html>
  <head>
    <style>
      div {
        font-family: Courier;
        width: 10em;
      }
    </style>
  </head>
  <body>
    <div>
      1 3 5 7 9 1 3 5 7 9 1
    </div>
  </body>
</html>

The result is not what I wanted as the browser line breaks after column 15, not 10:

1 3 5 7 9 1 3 5
7 9 1

(Result in Firefox and Chromium, both in Ubuntu.)

Wikipedia’s article says that an “em” is not always the width of an M, so it definitely looks like the “em” unit can’t be trusted for this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:35:29+00:00Added an answer on May 27, 2026 at 1:35 am

    1em is the height of an M, rather than the width. Same holds for ex, which is the height of an x. More generally speaking, these are the heights of uppercase and lowercase letters.

    Width is a totally different issue….

    Change your example above to

    <div>
        <span>1</span> 3 5 7 9 1 3 5 7 9 1
    </div>
    

    and you will notice width and height of the span are different. For a font-size of 20px on Chrome the span is 12×22 px, where 20px is the height of the font, and 2px are for line height.

    Now since em and ex are of no use here, a possible strategy for a CSS-only solution would be to

    1. Create an element containing just a &nbsp;
    2. Let it autosize itself
    3. Place your div within and
    4. Make it 10 times as large as the surrounding element.

    I however did not manage to code this up. I also doubt it really is possible.

    The same logic could however be implemented in Javascript. I’m using ubiquitous jQuery here:

    <html>
      <head>
        <style>
          body { font-size: 20px; font-family: Monospace; }
        </style>
        <script 
          type="text/javascript" 
          src ="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
        </script>
      </head>
      <body>
        <div>1 3 5 7 9 1 3 5 7 9 1</div>
        <script>
          $('body').append('<div id="testwidth"><span>&nbsp;</span></div>');
          var w = $('#testwidth span').width();
          $('#testwidth').remove();
          $('div').css('width', (w * 10 + 1) + 'px');       
        </script>
      </body>
    </html> 
    

    The +1 in (w * 10 + 1) is to handle rounding problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While using fixed width select tag , there is one bug in IE. When
I'm using the following regex to capture a fixed width description field that is
I need to write some fixed-width font (i.e Courier New or Consolas) text to
I'm using a fixed size font ( eg: Courier New ). When I initialize
I'm using pre element to display some text including special unicode characters (⚡ ⚑
I'm implementing a table with a fixed header using this kind of setup: <div
I am using a System.Random object which is instantiated with a fixed seed all
For intense number-crunching i'm considering using fixed point instead of floating point. Of course
I'm interested in actual examples of using fixed point combinators (such as the y-combinator
How do I check if browser supports position:fixed using jQuery. I assume I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.