Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

css

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)

like image 461
Rajasekar Avatar asked May 04 '10 07:05

Rajasekar


People also ask

What unit should I use for margin CSS?

In order to ensure consistent use of whitespace throughout the application, given a component could be used in a variety of contexts, it may be best to use rem for margin and padding than em .

What are the 2 units of measurement used to create a responsive web site?

In this article, I will show you the most suitable CSS units to use for responsive design. There are two types of CSS Units which are: Absolute Length Unit and Relative Length Unit.

Should I use px or VH?

While PX, EM, and REM are primarily used for font sizing, %, VW, and VH are mostly used for margins, padding, spacing, and widths/heights.

What are the 3 measurements used in CSS?

CSS supports a number of measurements including absolute units such as inches, centimeters, points, and so on, as well as relative measures such as percentages and em units. You need these values while specifying various measurements in your Style rules e.g. border = "1px solid red".


2 Answers

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.
like image 168
Quentin Avatar answered Oct 18 '22 00:10

Quentin


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.

like image 25
Chris Avatar answered Oct 18 '22 01:10

Chris