EniunEniunEniunEniun
  • Contact
  • EN
  • ES
✕
Sin resultados Ver todos los resultados

How to Use vh and vw Units in CSS

  • Inicio
  • CSS for Beginners
  • How to Use vh and vw Units in CSS
01/12/2026
Ilustración de una ventana con las medidas 100vw y vh

Viewport units measure the available browser viewport. They are useful for full-height sections, responsive layouts, and pages whose footer should reach the bottom when content is short.

Contenidos del artículo

Toggle
  • 1. What are vh and vw?
  • 2. Using viewport units
  • 3. Viewport units versus percentages
  • 4. Practical examples
    • 4.1. Center one line of text
    • 4.2. Center an element with transform
    • 4.3. Keep the footer at the bottom on short pages
  • Quick check

1. What are vh and vw?

1vh is one hundredth of the viewport height and 1vw is one hundredth of its width. For an 800px by 600px viewport, 50vw is 400px and 50vh is 300px.

2. Using viewport units

Use viewport units anywhere that accepts a length, such as a minimum height or width. On mobile, browser toolbars can hide and reappear. svh uses the smaller viewport, while dvh responds to changes in the visible height. You can put 100vh first as a fallback.

.hero { min-height: 100vh; min-height: 100dvh; }

3. Viewport units versus percentages

width: 50% usually measures half the containing block. width: 50vw measures half the viewport, even when the element is inside a narrow container. Prefer width: 100% for an ordinary full-width container, since 100vw can cause horizontal overflow in layouts with a vertical scrollbar.

4. Practical examples

4.1. Center one line of text

A line height matching the viewport height can center a single line of text vertically. Pair it with centered text alignment. For text that may wrap, use Grid or Flexbox instead.

.single-line { line-height: 100vh; text-align: center; }

Try the single-line example

4.2. Center an element with transform

Move the top-left corner to the center of the viewport, then translate by half of the element’s own width and height.

.centered {
  position: fixed;
  top: 50vh;
  left: 50vw;
  transform: translate(-50%, -50%);
}

Try the transform example

4.3. Keep the footer at the bottom on short pages

Make the page a vertical flex container. The main section grows when space is available. If there is a lot of content, the footer follows it in the normal flow.

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
main { flex: 1; }

Try the footer example

Quick check

A container is 400px wide and the viewport is 1,000px wide. What is the difference between 50% and 50vw?

For a width property resolved against this container, 50% is 200px. The viewport length 50vw is 500px.

Servicios

Desarrollo Web
Diseño Web
Marketing Digital
Social Media
Experiencia de Usuario
Cursos formativos
Services

WordPress development (in Spanish)
Custom software (in Spanish)
Web design (in Spanish)
UX consulting (in Spanish)
SEO and GEO (in Spanish)
Courses and tutorials
Contacta con nosotros

info@eniun.com

Reportar un error

                   

Get in touch

Contact Eniun


info@eniun.com

Report an issue (in Spanish)

                   

Suscríbete

Únete a nuestra comunidad para recibir contenido exclusivo y relevante para diseñadores y desarrolladores web


Apuntarme

Subscribe to the newsletter

Get updates on web design, development, and computing from Eniun. The newsletter is in Spanish.


Sign up (in Spanish)
Katadoku retos de logica y deduccion
Simulador montaje PC
© 2026 Eniun | Política de privacidad y cookies
© 2026 Eniun | Privacy and cookies policy
    EN
    • EN
    • ES