What exactly does px represent in web font size - html

I'm doing some graphics (in Javascript, if that's relevant), and I know you can size fonts with pt, px, %, etc. When looking up what px means, every site seems to have the same vague answer -- the size of the font in pixels. I'm wondering what exactly px represents. For example, if I have 20px, will 20 pixels represent the height of a capital A, a small e, a lowercase g or the difference between the bottom of the g and the top of the A? (I'm assuming it's the size of the A, but I just want to double check).
Furthermore, px is apparently relative to the view size. So if I do a fillRect(1,1,20,20), will this be the size of a capital A at 20px, or does the font size change on different devices independent of the graphics?

For example, if I have 20px, will 20 pixels represent the height of a
capital A, a small e, a lowercase g or the difference between the
bottom of the g and the top of the A?
None of them. It represent an abstract object called the "em-square". The height of capitals, the width of the glyphs, and various other measures of the glyphs are then defined in each font as proportional to the em-square. But often, none of the measures you see is exactly equal to 1 em-square size, or in your case, equal to 20px.

per http://www.unitconversion.org/typography/pixels-x-to-centimeters-conversion.html
10 pixels are 0.264583333 CM.
other than that, it can very depending on your font and the display resolution of the screen.

No, the size of one pixel px is not officially defined in a technical way.
Although I cannot tell you who exactly defines the value, if it's the pc-manufacturer or who it is and whether w3 guidelines tell these specifying parties to measure it from a certain distance depending on screen size, But the most specific and most helpful description of the "magic" px unit is
1 px equals the smallest width to make a line visible on the specific device.
given that the watching user stands at a device relative reasonable distance.
(see Official w3 Info about the px unit).

Related

w and px of the srcset attribute in html

In the srcset attribute of img element in html, we can specify either the width or the pixel density of each source. We use w to specify width and x to specify pixel density. I have some questions about w and x.
Does 500w mean that the width of the image is 500 pixels? If so, why is it w and not px, as used when in the size attribute?
Does 1x means a pixel density of 72pixels/inch
Why does an image have pixel density? I thought pixel density means the number of pixels within a physical length/space. For example, the number of pixels per inch. But a digital image doesn't occupy a physical length/space, it only occupies a number of pixels. So what does the pixel density of a digital image mean?...This is my guess, please tell me if I'm right: An image only occupies a number of pixels, but it could have been intended for a physical length. For example, an image of a button of 144 pixels width was intended to occupy a physical space of 1 inch. Therefore, it was intended for a device of 2x. We tell the browser this by specifying 2x in the srcset attribute. To sum up, the pixel density means the pixel density of the system the image is intended for.
#1. The 'w' value that appears to the right of each file listed in the srcset attribute of the <img> tag is known as the "w descriptor." The value of each 'w' descriptor is the intrinsic width, in pixels, of the file the 'w' descriptor is associated with. "Intrinsic width" means the native width of the image when it is was originally created or the last time its width changed. You can view a file's intrinsic width in Photoshop or the Windows 10 Photo Viewer (click the three-dot menu "..." and select File Info). "px" is not used to describe intrinsic width because it is used to describe various other types of image widths. The 'w' descriptor describes a specific type of image width that is measured in pixels - intrinsic width - and is used only with the srcset attribute of the <img> tag.
#2. "1x" does NOT mean a pixel density of 72 pixels per inch. That's because pixel density is NOT device resolution. Pixel density is the ratio of browser image width in pixels (known as "CSS pixels") to the native pixel width of the device's display screen. Pixel density has nothing to do with device resolution.
#3. An image does NOT have pixel density. It has only two types of width measurements - both in pixels. One width measure is the width the browser is displaying the image at. The other width measure is the intrinsic width of the image (see #1). For example, suppose I have an image whose intrinsic width is 800px wide. Also suppose that the browser is displaying the image at 650px wide. These two measures are not related in any way (this is NOT pixel density!) - they're just two different ways an image can be viewed which results in two different width measurements.
I've answered your questions directly, without any other context or explanation, and I'm certain that you are scratching your head saying, "That didn't help!" That's the exact same reaction I had when I first started to educate myself about how inline images (images that are specified with the <img> tag in HTML) are selected and displayed by browsers. I suggest you read this post to begin to gain a thorough understanding about how inline images are specified in HTML markup and how browsers select the appropriate image from the srcset attribute of the <img> tag. You can expect several weeks of study before this topic comes into sharp focus. And when you've finished mastering inline images, background images are waiting for you to master with a completely different set of rules. :>)

How to set physical dimensions (like actual inches) for elements?

My end goal is to display an image of the same exact size on every screen (yes, I really do need this for something).
I started reading about how to get the user's screen size here - and the answer seems to be that it's impossible (without calling an API which attempts to guess the actual device based on some params).
I then remembered that css has physical dimension units for sizing elements and decided to test on my screen. I set dimensions like so:
<div style = "background-color:black; height: 5in; width: 3in;"></div>
and then put a physical 3"x 5" (7.6cm x 12.7cm) flashcard against the screen.
It was significantly off in size.
Fun note - setting size in mm was ridiculously off - I expected to get the same results as with inches.
With that said, is there a way to do what I need?
In CSS, one inch does not equal one physical inch. It equals 96px.
This section of the spec may help you:
5.2. Absolute lengths: the cm, mm, q, in, pt, pc, px units
The absolute length units are fixed in relation to each other and
anchored to some physical measurement.
If the anchor unit is the pixel unit, the physical units might not
match their physical measurements.
Alternatively if the anchor unit is a physical unit, the pixel unit
might not map to a whole number of device pixels

html - How are pixels defined?

From my experience up to now the pixel size is something very relevant and intepreted differently based on many factors. My question is how do pixels work in html pages.
For example we can set the width and height of an image:
<img src="lalala.jpg" width="100px;" height="100px;">
What does 100 pixels actually mean?
How are the 100 pixels translated on screen?
How are the 100 pixels translated if on print papper?
To be more specific. If I set the image size to 100px then is it going to be the same size in inches on different screen sizes ? Ad if so..Is it going to be the same size if I print the same page while using a different screen size?
In HTML, pixel lengths of the height and width attributes are controlled by CSS. In some instances, these are refereed to as "CSS pixels". HTML itself does not provide a definition for what a pixel should represent (thus why I added the css tag into your question).
CSS itself has its own Units and Values documentation which defines the Pixel in its section on Absolute Lengths:
5.2. Absolute lengths: the ‘cm’, ‘mm’, ‘in’, ‘pt’, ‘pc’, ‘px’ units
The absolute length units are fixed in relation to each other and anchored to some physical measurement. They are mainly useful when the output environment is known. The absolute units consist of the physical units (in, cm, mm, pt, pc) and the px unit:
unit definition
---- ----------
‘cm’ centimeters
‘mm’ millimeters
‘in’ inches; 1in is equal to 2.54cm
‘px’ pixels; 1px is equal to 1/96th of 1in
‘pt’ points; 1pt is equal to 1/72nd of 1in
‘pc’ picas; 1pc is equal to 12pt
100 pixels is equal to roughly 1.041in, which itself is equal to roughly 2.65cm.
I'm not going to answer the further questions you've asked about different monitors and printing as this would make my answer incredibly long and dull. If you want to find out these answers yourself, a good place to start is in the same document I've already linked, which goes into detail about the Reference Pixel. Values in CSS are based upon a value of 96dpi (which means that 96 pixels on a monitor which has a pixel dencity of 96dpi will be equal to one inch if you were to measure it with a ruler).
Because every screen has diferent pixel size HTML/CSS or whatever software language is going to be made never gonna match the physical pixel size.
To calculate pixel size you need Hardware machine language not software.

CSS Font Size Specifics

I have a couple of specific questions about font size:
Is pt (I.E. 12pt) standardized? I think I read somewhere that pt = 1/72 of an inch. However, the size of 24pt font in photoshop is significantly different from size 24pt font in css.
What exactly does font-size determine? Is it a sort of unstandardized arbitrary description about letter size or does it actually refer to the specific height or width attribute of letters?
Thanks Stack!
font-size is the baseline height of the font (the height of a capital H character) plus a little room above it (the ascender) and larger amount of room below it (the descender).
For a particular font at a particular font-size, the baseline height will always be the same size. Below are some examples, based on measurements of the Arial font:
font-size baseline height
10 7
11 8
12 9
14 10
In theory, for CSS, pt is a rough approximation of the baseline height in px. For instance, a font-size:9pt has a baseline height of 9px, which corresponds to font-size:12px. In practice, I've sometimes found that pt is larger than the baseline height by 0.5 to 1.0 px.
Here's a jsfiddle showing a side-by-side comparison of capital H's in px and pt.
In most PSDs I've seen, a font specified as 12pt is in fact 12px. But this may vary. When in doubt, measure the baseline height of the text and convert backwards from that to a font-size in px. For instance, if a capital H has a height of 9px, then the font-size is 12px. Sometimes antialiasing in a PSD makes it difficult to accurately measure the baseline height.
You could take a look here from a similar question about font size in Photoshop and CSS.
While here You can find a nice conversione table: you can notice that PT in Photoshop are equivalent to PX in CSS.. i think you shouldn't use PT in both.
Finally, here you can read something about the history of PT in typography (yes, you are right about 1pt = 1/72 inch) :)
It's usually better practice to measure font in pixels in CSS. Like the question you asked pt size is pretty arbitrary and varies upon the developers own definitions.
In CSS 2.1 (the current spec), pt is unambiguously 1/72 of an inch. The reality is different, though, and this is reflected in the CSS3 Values and Units draft, section 5. It describes two setups, using different “anchor units”. If the pixel is the anchor unit, an inch (in as CSS unit) need not be an inch (the physical unit, 2.54 mm), and hence the meaning of p may vary as well. This is one reason why pt in CSS need not match pt in some software.
The font size is a basic property of a font. Any relationships between it and dimensions of characters are up to the font designer, but font design is of course not arbitrary. The CSS 2.1 spec puts this rather vaguely, but the formulation is still useful (against many common misconceptions): “The font size corresponds to the em square, a concept used in typography. Note that certain glyphs may bleed outside their em squares.” (And on the other hand, most glyphs use just a small part of the em square.)
"pt" in fonts is only for print CSS.
Here is a link which explains all differences between types of sizes
http://css-tricks.com/css-font-size/
You can go straight to the official W3C documentation on CSS for specifics of the font-size property.
And to your question about the meaning of pt in CSS, again the official documentation shows that yes, it is 1/72 of an inch.
Remember that in different applications you can zoom your screen, so think about these values more for printed, rather than screen, media.
1)
In print css, a pt is exactly 1/72 of an inch.
In screen
css everything is defined in terms of pixels, and a pt is defined as
1 1/3 pixel. But how large this is in physical measurements like inch
depend on screen DPI and vary greatly between devices. For this
reason, pt (and other physical measurements like in, cm etc.) is not
that useful in screen css.
2)
Font-size refers the the height of the "em-box" - a bit
simplified this is the rectangle which can contain all letters in
the font including ascenders and descenders. Individual letters will of course
often take up less space than the whole em-box. For example a
lower-case x will have space above and below, while a lower-case j
might fill the whole height of the em-box.
Font-size therefore indicates how much vertical space is required by a line of text, but the dimensions of individual letters and glyphs will vary relative to others depending on the letter and the design of the font in use.
Furthermore some fonts may have letters which extend ("bleed") even outside the em box, which is also a stylistic choice by the designers of the font. E.g. the letter "Å" will often bleed above the em-box.

Which unit I should use in CSS when designing a web page

I have designed and developed more than 10 sites, but I still have a doubt about the correct unit I should use. Should it be px, em or %?
EDIT 1: FOR LAYOUTS (Especially for container boxes)
Different units depending on context. If there was one that was best for every situation, then there wouldn't be so many units.
As rules of thumb go:
If you are working on screen media:
Use % for font sizes
Use px for images
Use px, %, or em for box sizes
Use ratios for line height
If you are working in print media:
It might be wise to avoid px (this is not a hard rule by any means) and everything else is fair game. It really depends how much control you want.
There's no real right or wrong, but as a rule of thumb:
For anything you want a certain, fixed size, use PX
For anything you want to scale with font-size, use EM
For anything you want to scale to the available space in the window/container, use %
Each used to have specific advantages or disadvantages in different browsers when it came to users scaling the browser's base font-size/zooming, but more recent versions of the browsers by-and-large get around these issues by scaling everything, not just font-size.
If you're talking about font-size then px and pt are not ideal.
Ems and Percent units are scalable, therefore they are far more accessible - friendly for the visually-impaired. They also scale down well for mobile phone users.
Px and Pt units do not scale upward for visually-impaired users, or downward for mobile phones.
If you're talking about layout or containers then it depends on the type of design you want - fluid or static - and there isn't necessarily a "right" answer.
Without going into an example, it's difficult to advice. Do you have a site in mind we could look at?
Use the unit you need in the specific context.
Unit Description
====================
% percentage
in inch
cm centimeter
mm millimeter
em 1em is equal to the current font size. 2em means 2 times the size of the current font. E.g., if an element is displayed with a font of 12 pt, then '2em' is 24 pt. The 'em' is a very useful unit in CSS, since it can adapt automatically to the font that the reader uses
ex one ex is the x-height of a font (x-height is usually about half the font-size)
pt point (1 pt is the same as 1/72 inch)
pc pica (1 pc is the same as 12 points)
px pixels (a dot on the computer screen)
source: http://www.w3schools.com/css/css_units.asp
For flexibility and accessibility I recommend using % for horizontal measures (relative to the user's screen), and em for vertical measures (relative to the user's font setting).
For fixed width layouts
For as much as pixel perfection I would suggest to use PX for width ,height, margin, and padding
for line-height use unit-less value like {line-height:1.2}
for typographic elemets use {font-size:62.5%) for body then use em for other elements
in HTML for <img> always use unit-less width and height .