Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

382
Vistas
Fix the size of 'html' and 'body' elements to be exactly those of the screen

I often need that html and body elements have the size of the screen. Typically, in the case when I want to have a svg element fit the whole screen.

To achieve that, I saw that it was possible to use CSS code as follow.

html,body{margin:0;padding:0;height:100%;}

The code that I personnaly use is the following one.

html {
    height: 100%;
    display: flex;
}

body {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
}

Both seem to work well, but I recently had the following remark.

html { height: 100%; display: flex; } is a useless declaration. html height will always be calculated to fit content. Also 100% means 100% of the parent. html has no parent... also applying flexbox to html is useless as it only has 1 child element that is visible: body.

Actually:

  • I put 100% of html height in order to have it fit the screen height.
  • I apply flexbox to html in order to be able to use flex-glow: 1 on its child, and have this child filling its parent.

Is there any better to solution than mine?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

I personally use this:

html {
  display: grid;
  min-height: 100%;
}

This will make your body full height by default and will also respect default margin

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

And you can easily use height:100% on an inner element without issue:

html {
  display: grid;
  min-height: 100%;
  background: blue;
}

body {
  background: red;
}

.box {
  height: 100%;
  border: 5px solid green;
  box-sizing: border-box;
}
<div class="box"></div>

over 4 years ago · Santiago Trujillo Denunciar

0

I find that whenever working with elements that need to be the full height of the screen height: 100vh is usually a good place to start. VH = viewport height. I use it over height: 100% as depending on the layout 100% doesn't always equal the page height, so with VH you know exactly what you are getting!

With VH you can also then use a calc() in your CSS, so if you needed your body to fill the whole height of the page, but subtract the height of a header for example you could do something like this:

<header style="height: 64px">
<section style="height: calc(100vh - 64px)"
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda