I'm building a responsive website and I'm wondering what unit I should use? I've seen a lot of sites using pixels (px) for measurements and I've seen some using percent (%). Is there a preferred — or right — way of doing responsive design?
I've found percent to be hard to use, since it makes calculations hard and I've ended up with values like 2.754% and so on when setting widths/margins etc. Pixels seems easier, it's just simple addition and subtraction, but I've read that it isn't "future proof" or something like that and wont scale properly if the user zooms in the browser window. Is that still true?
If you have any experience or expertise, please share! I would love to hear what you guys have to say!
Thanks!
Rem is an absolute unit relative to the root element of the HTML document and is commonly used for font sizes. The Rem unit is a scalable unit in which the browser renders into pixel values. I recommend it for responsiveness.
Google has always recommended responsive web design (RWD), especially after rolling out a big update on 4/21/15 which ranked mobile-friendly sites higher.
Responsive design makes it possible to adapt a website to any screen. In responsive design, the size of a site's elements change based on the browser size and CSS media queries. Viewport units is an interesting CSS feature that allows you to automate some aspects of your responsive design.
In using “height” in paddings or margins, you should use % instead of px, in case your website is responsive. Because the big deal about responsive is: it's responsive. It should be responsive, so make it responsive.
For layout type things like the sizes of boxes, you want to use %
because you will typically have several columns sized as a percentage of their parent that will stack on top of each other at a certain breakpoint (width:100%
). No other unit will allow you to fill 100% of the space like %
does.
For padding/margins use em
, normally you will want to space your elements out relative to the size of your text. With em
(the with of an 'M' character) you can quite easily say I want approximately 1 character spacing here.
For borders you can use px
or em
, there is a difference though. If you want your border to look like it's one pixel wide on all devices, use 1px
. It may not be one pixel on all devices however, high density displays convert 1px
into 2px
for example. If you want your border to be a size based on your font, use em
.
For fonts use em
(or %
), the use of em
carries through parents to children and it just a nicer unit to work with over px
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With