If you’re a web developer or designing your WordPress theme, then it’s important to have a good understanding of the different available CSS units. These units allow you to control the size and spacing of elements on a webpage. In this guide, we will explore the different CSS units and how they work. We will also discuss when and where to use each one. By the end of this guide, you should have a better understanding of how to use CSS units to create responsive and effective web pages.
Absolute Length Units in CSS
Absolute means physical.
There are a few different units of measurement that you can use in CSS, but the most common is pixel (px). Each of the physical units has its advantages and disadvantages, so it’s important to understand the difference between them before choosing which one to use.
---
Pixels are the most commonly used unit of measurement in CSS. They’re easy to understand and work well in most situations. The only downside is that they’re not always accurate, especially when it comes to responsive design. 96px is/was one inch. That is because, when the unit standard was formulated, computer monitors had a resolution of 1024 x 768, and a DPI of 96. Today, 96px is less than one inch because of the huge number of pixels. Points (pt) is another unit like pixels, pt is 1/72th of an inch. Inch, and cm are also absolute units in CSS. But they have several understandable problems due to changing smartphone dimensions.
Pixel is still commonly used in web designing since we have a rough idea of how big 700px is. The sidebars and ad units can easily be expressed in pixels than in other units. We use these units in this way:
1 2 3 4 5 6 7 8 9 10 11 | <style> .outer { background: white; border: 1in solid red; } .inner { background: blue; border: 1px solid black; margin: 10px; } </style> |
The above CSS will give a fixed-size box:
1 2 3 | <div class="outer"> <div class="inner">EXAMPLE</div> </div> |
Relative Length Units in CSS
Several different relative length units can be used to specify lengths in CSS. The most common relative length units are em, rem, %, vh, and vw.
The em unit is based on the font size of the element. Ems are similar to pixels, but they’re relative to the font size of the element they’re applied to. This can be helpful if you want your elements to resize based on the user’s font size preference. However, it can also be tricky to control how ems to scale, so they’re not always the best choice. If the font size is not specified, it will be inherited from the parent element.
The rem unit is similar to the em unit, but it is based on the root element’s font size. This means that if the root element’s font size changes, all elements using rem will change their sizes accordingly.
The vh and vw units are based on the viewport size. The vh unit is equal to 1% of the viewport height, and the vw unit is equal to 1% of the viewport width. These units are useful for making elements that resize automatically when the viewport size changes.