Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a pixel (px) in CSS?

Tags:

css

graphics

I am reading an ASP.NET book and it refers to CSS files and talks about pixels. But I never understood it from a resolution, layout, etc. point of view. For example, what does the following mean in CSS file definition?

#header
{
    padding: 0px;
    margin: 0px;
}
like image 211
dotnet-practitioner Avatar asked Mar 01 '09 19:03

dotnet-practitioner


People also ask

Are CSS pixels real pixels?

A hardware pixel is an individual dot of light in the display. A software pixel, also called a CSS pixel in the web world, is a unit of measurement. The device manufacturer determines how many hardware pixels equals one software pixel, a concept known as the device pixel ratio.

How big is a px in CSS?

Pixels, or px , are one of the most common length units in CSS. In CSS, 1 pixel is formally defined as 1/96 of an inch. All other absolute length units are based on this definition of a pixel.

What does px mean in code?

It means a dimension, measured in pixels on-screen. E.g. width: 200px; means an element is 200 pixels wide.

What size is 1px?

1 pixel (px) is usually assumed to be 1/96th of an inch. 1 point (pt) is assumed to be 1/72nd of an inch. Therefore 16px = 12pt.


2 Answers

This is a little beyond where you might be at the moment, but a CSS pixel is not necessarily exactly the same size as a single pixel on your display. According to the spec:

If the pixel density of the output device is very different from that of a typical computer display, the user agent should rescale pixel values. It is recommended that the reference pixel be the visual angle of one pixel on a device with a pixel density of 96dpi and a distance from the reader of an arm's length.

So if you have one of those incredibly expensive extra-high-resolution displays that doesn't count as “typical”, the browser and/or OS may choose to redefine what a “pixel” is.

The useful definition for a ‘px’ as far as CSS authoring is concerned is: a ‘px’ is the quantity of length equal to the pixel in an unscaled HTML <img> or CSS ‘background-image’.

like image 62
bobince Avatar answered Sep 16 '22 15:09

bobince


A pixel is generally thought of as the smallest single component of a digital image.

The number of pixels in an image is called resolution.

The screen resolution is the number of distinct pixels in each dimension that can be displayed by your screen.

In the css snippet that you posted you're applying 0 pixels of margin and padding to the element with id="header".


(source: functionx.com)

like image 20
Christian C. Salvadó Avatar answered Sep 19 '22 15:09

Christian C. Salvadó