Jump to content

How do I find the pixel equivalent of an "em?"


programmer-in-training

Recommended Posts

There really isn't a way. The "em" is set by the default font size of the user's browser. If you want pixel values just use px as a unit instead.Though...Maybe with javascript you can detect it, something like this:

<div style="height: 1em" id="check"></div><script type="text/javascript">  var emSize = document.getElementById("check").offsetHeight;  // Use the variable "emSize" wherever you need the pixel value of the em unit</script>

Link to comment
Share on other sites

EDIT: That was a nice function to find the em size, but I realized I actually don't need it! Instead of "100%" I put in "menu.offsetHeight" and now it works. Thanks though it will be very useful. I've had to use less efficient workarounds before I came to these forums! Wonder why I never noticed them at w3schools before.... :)Also, I see why document.getElementById() would be useful if you're searching for a string.

Link to comment
Share on other sites

Using the element's ID directly as an object without an assignment is not really correct, and isn't part of the W3C standard. Always use document.getElementById().

menu.offsetHeight; //Incorrect (though will work)menu = document.getElementById("menu"); //Correctmenu.offsetHeight; //Now also correct because of the assignment on the line above

By the way, an "em" is the width of the letter m at the current font size.

Link to comment
Share on other sites

My version of a little routine that saves a lot of typing:

function gg(id) {   return document.getElementById(id);}//call it like this:gg("id");

A lot of people use a $ instead of gg, but that makes my perl/php confusing. I remember "gg" by thinking of it as "go get."

Link to comment
Share on other sites

Actually, it is. C/P this to be sure.

function $(){   alert ("Hello");}$();

I'm surprised you haven't seen this. It's in dozens of "My top-ten Javascript tips" pages around the net. I suppose it could stop being legal without warning; I don't really know. And as I said, I don't use it.

Link to comment
Share on other sites

Yeah, like DD said there are quite a few libraries that define the $ function to replace document.getElementById. It confused the crap out of me until I realized that $ was a legal function name, I was assuming it was some operator where everyone but me got the memo.

function $(s){  return document.getElementById(s);}val = $('elId').value;$('elId').style.display = 'none';

It just makes the code a little cleaner, it looks more like you're working directly with the object instead of working with document.getElementById.

Link to comment
Share on other sites

Actually, it is.
Ahh, right. I knew it was a valid function name. I had assumed that the OP was asking if "$" was a built in feature of javascript/DOM like "alert", "getElementById", etc. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...