A Visual Guide To Relative Units In CSS

A Visual Guide To Relative Units In CSS

·

2 min read

Hello World! Welcome to yet another article by me. In today's article we'll be looking forward to demystify and understand the mostly used relative units in CSS. Hope that you like it and find it helpful.

To stay notified about all my upcoming articles make sure that you subscribe to my newsletter.

  1. Percentage Unit. Percentage.png

    Conclusion:

    1. In case of fonts the percentage units are relative to the parents absolute(computed to be precise) font-size.
    2. In case of lengths(i.e. padding, margin, width, height, etc) the percentage units are relative to the parents absolute(computed) width.
  2. Em Unit. Ems.png

    Conclusion:

    1. In case of fonts the em units are relative to the parents computed font-size for a given child element.
    2. In case of lengths(i.e. padding, margin, width, height, etc) the em units are relative to the current(the element in which they are declared) element's computed font-size.
  3. Rem Unit. Rems.png

    Conclusion:

    1. In case of fonts the rem units are relative to the root element's computed font-size for all the child elements within it.
    2. The above case applies for lengths too.

      Root element is the one which is at the top position in your webpage element hierarchy. In HTML, the root element is the <html> element.

  4. Viewport Units. Viewport.png

    Conclusion:

    1. In case of fonts or lengths the viewport units are relative to the viewports width or height.

Note:

  1. Each unit has an initial value, used if nothing is declared explicitly.
  2. The Browser specifies a root font-size for each page by default.
  3. Relative units are always converted to pixels by the browser.
  1. Benefits of an education email.

  2. Remove the N-th node from the end of a linked list.

Make sure to share this if you found it helpful, and keep learning, you're a genius.