Unidades CSS


Tabla de contenido

    Mostrar tabla de contenidos


Unidades CSS

CSS tiene varias unidades diferentes para expresar una longitud.

Muchas propiedades CSS toman valores de "longitud", como por ejemplo:

width, margin, padding, font-size

Longitud es un número seguido de una unidad de longitud, como por ejemplo:

10px, 2em

Ejemplo

Establezca diferentes valores de longitud, usando px (píxeles):

 h1 {
  font-size: 60px;
}
p {
  font-size: 25px;
  
  line-height: 50px;
}

Pruébelo usted mismo →

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 60px;
}

p {
  font-size: 25px;
  line-height: 50px;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


Nota: No puede aparecer un espacio en blanco entre el número y la unidad. Sin embargo, si el valor es 0, la unidad se puede omitir.

Para algunas propiedades CSS, se permiten longitudes negativas.

Hay dos tipos de unidades de longitud: absoluta y relativa.


Longitudes absolutas

Las unidades de longitud absoluta son fijas y una longitud expresada en cualquiera de ellas aparecerá exactamente con ese tamaño.

No se recomienda el uso de unidades de longitud absoluta en la pantalla, porque los tamaños de la pantalla varían mucho. Sin embargo, se pueden utilizar si se conoce el medio de salida, como en cuanto al diseño de impresión.

cm

Descripción: centímetros

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 1.5cm;}
h2 {font-size: 1cm;}
p {
  font-size: 0.5cm;
  line-height: 1cm;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


mm

Descripción: milímetros

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 15mm;}
h2 {font-size: 10mm;}
p {
  font-size: 5mm;
  line-height: 10mm;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


in

Descripción: pulgadas (1 pulgada=96 píxeles=2,54 cm)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 1in;}
h2 {font-size: 0.5in;}
p {
  font-size: 0.2in;
  line-height: 0.5in;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


px *

Descripción: píxeles (1 px=1/96 de 1 pulgada)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 50px;}
h2 {font-size: 30px;}
p {
  font-size: 15px;
  line-height: 20px;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


pt

Descripción: puntos (1 punto=1/72 de 1 pulgada)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 50pt;}
h2 {font-size: 25pt;}
p {
  font-size: 15pt;
  line-height: 25pt;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


pc

Descripción: picas (1 pieza=12 pt)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {font-size: 4.5pc;}
h2 {font-size: 3pc;}
p {
  font-size: 1.5pc;
  line-height: 3pc;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>


* Los píxeles (px) son relativos al dispositivo de visualización. Para dispositivos con baja densidad de ppp, 1 px es un píxel (punto) del dispositivo en la pantalla. Para impresoras y alta resolución. pantallas 1px implica múltiples píxeles del dispositivo.


Longitudes relativas

Las unidades de longitud relativa especifican una longitud relativa a otra propiedad de longitud. Las unidades de longitud relativa escalan mejor entre diferentes medios de renderizado.

em

Descripción: Relativo al tamaño de fuente del elemento (2em significa 2 veces el tamaño de la fuente actual)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
p {
  font-size: 16px;
  line-height: 2em;
}

div {
  font-size: 30px;
  border: 1px solid black;
}

span {
  font-size: 0.5em;
}
</style>
</head>
<body>

<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<p>These paragraphs have a calculated line-height of: 2x16px = 32px.</p>
<div>The font-size of the div element is set to 30px. <span>The span element inside the div element has a font-size of 0.5em, which equals to 0.5x30 = 15px</span>.</div>

</body>
</html>


ex

Descripción: Relativa a la altura x de la fuente actual (rara vez se utiliza)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
div {
  font-size: 30px;
  border: 1px solid black;
}

span {
  font-size: 1ex;
}
</style>
</head>
<body>

<div>The font-size of the div element is set to 30px. <span>The span element inside the div element has a font-size of 1ex</span>.</div>

</body>
</html>


ch

Descripción: Relativo al ancho del "0" (cero)

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
body {
 font-size:16px;
}

div {
  font-size: 3ch;
  border: 1px solid black;
}

</style>
</head>
<body>

<p>The font-size of this document is 16px.</p>

<div>The font-size of this div element is 3ch.</div>

<p>The ch unit sets the font-size relative to the width of the character "0".</p>

</body>
</html>


rem

Descripción: Relativo al tamaño de fuente del elemento raíz

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
body {
 font-size:16px;
}

div {
  font-size: 2rem;
  border: 1px solid black;
}

</style>
</head>
<body>

<p>The font-size of this document is 16px.</p>

<div>The font-size of this div element is 2rem.</div>

<p>The rem unit sets the font-size relative to the browsers base font-size, and will not inherit from its parents.</p>

</body>
</html>


vw

Descripción: Relativo al 1 % del ancho de la ventana gráfica*

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 20vw;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the width of the browser window to see how the font-size of h1 changes.</p>
<p>1vw = 1% of viewport width.</p>

</body>
</html>


vh

Descripción: Relativo al 1 % de la altura de la ventana gráfica*

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 20vh;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the height of the browser window to see how the font-size of h1 changes.</p>
<p>1vh = 1% of viewport height.</p>

</body>
</html>


vmin

Descripción: Relativo al 1% de la dimensión más pequeña de la ventana gráfica*

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 15vmin;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the browser window (both width and height) to see how the font-size of h1 changes.</p>
<p>1vmin = 1vw or 1vh, whichever is smaller.</p>

</body>
</html>


vmax

Descripción: Relativo al 1 % de la dimensión más grande de la ventana gráfica*

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
h1 {
  font-size: 15vmax;
}
</style>
</head>
<body>

<h1>Hello</h1>

<p>Resize the browser window (both width and height) to see how the font-size of h1 changes.</p>
<p>1vmax = 1vw or 1vh, whichever is larger.</p>
<p>The vmax unit is not supported in Internet Explorer or Safari 6.1 and earlier versions.</p>

</body>
</html>


%

Descripción: Relativo al elemento principal

Intentalo

<!DOCTYPE html>
<html>
<head>
<style>
body {
 font-size:16px;
}

div {
  font-size: 150%;
  border: 1px solid black;
}

</style>
</head>
<body>

<p>The font-size of this document is 16px.</p>

<div>The font-size of this div element is 150%.</div>

<p>The % unit sets the font-size relative to the current font-size.</p>

</body>
</html>


Consejo: Las unidades em y rem son prácticas para crear perfectamente ¡diseño escalable!
* Ventana gráfica=el tamaño de la ventana del navegador. Si la ventana gráfica es de 50 cm de ancho, 1vw=0,5 cm.



Soporte del navegador

Los números de la tabla especifican la primera versión del navegador que es totalmente compatible con unidad de longitud.

Length Unit
em, ex, %, px, cm, mm, in, pt, pc 1.0 3.0 1.0 1.0 3.5
ch 27.0 9.0 1.0 7.0 20.0
rem 4.0 9.0 3.6 4.1 11.6
vh, vw 20.0 9.0 19.0 6.0 20.0
vmin 20.0 12.0 19.0 6.0 20.0
vmax 26.0 16.0 19.0 7.0 20.0