Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Units in CSS

Units in CSS

Units in CSS — Measuring Sizes and Distances

CSS uses various units to specify lengths, sizes, and measurements in styles. These units can be absolute or relative.


1. Absolute Units

Fixed sizes that don’t change based on context or screen size.

UnitDescriptionExample
pxPixels, fixed screen pixelswidth: 100px;
ptPoints, mostly for print (1pt=1/72 inch)font-size: 12pt;
cmCentimetersmargin: 2cm;
mmMillimeterspadding: 5mm;
inInchesheight: 3in;
pcPicas (1pc = 12pt)border-width: 2pc;

2. Relative Units

Relative to some other value, which makes them more flexible and responsive.

UnitDescriptionExample
%Percentage relative to parent elementwidth: 50%;
emRelative to font-size of the elementfont-size: 2em; (2 ื current font size)
remRelative to root element's font size (html font size)margin: 1.5rem; (1.5 ื root font size)
vw1% of viewport widthwidth: 50vw;
vh1% of viewport heightheight: 100vh;
vminMinimum of vw or vhfont-size: 5vmin;
vmaxMaximum of vw or vhfont-size: 10vmax;
chWidth of the “0” character in the fontwidth: 20ch;
exHeight of the lowercase “x” in the fontLess commonly used

Which Unit to Use?

  • Use px for precise control, but not responsive-friendly.

  • Use %, em, or rem for flexible, scalable layouts.

  • Use viewport units (vw, vh) for full-screen and responsive designs.

  • Use rem for font sizes to keep scalability consistent.


Example

body {  font-size: 16px; /* Base font size */}h1 {  font-size: 2rem; /* 2 ื 16px = 32px */  margin-bottom: 1.5em; /* 1.5 ื current font size */  width: 80%; /* 80% of parent width */}.container {  width: 90vw; /* 90% of viewport width */  height: 50vh; /* 50% of viewport height */}

Want me to help with a unit converter tool or explain when to use each unit for responsive design?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql