You can use the style height: 100vh;
, which gives the element the full height of the viewport (visible area of the window):
Example:
body{
margin: 0;
}
#primeira{
background-color: yellow;
height: 100vh;
}
<div id="primeira">
Primeira div
</div>
<div>
Segunda div
</div>
<div>
Terceira div
</div>
Using height: 100%
To have div
equal the height of the viewport using percentage ( %
), you must set html
and body
to 100%
. >
html, body{
margin: 0;
height: 100%;
}
#primeira{
background-color: yellow;
height: 100%;
}
<div id="primeira">
Primeira div
</div>
<div>
Segunda div
</div>
<div>
Terceira div
</div>
Using JavaScript
You can use window.innerHeight
to get the height of the viewport and assign to height
of the first div
. Example:
var el = document.body.querySelector("#primeira");
function altura(){
el.style.height = window.innerHeight+"px";
}
document.addEventListener("DOMContentLoaded", altura);
window.addEventListener("resize", altura);
body{
margin: 0;
}
#primeira{
background-color: yellow;
}
<div id="primeira">
Primeira div
</div>
<div>
Segunda div
</div>
<div>
Terceira div
</div>
In jQuery would be:
$(window).on("load resize", function(){
$("#primeira").css("height",window.innerHeight+"px");
});
body{
margin: 0;
}
#primeira{
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="primeira">
Primeira div
</div>
<div>
Segunda div
</div>
<div>
Terceira div
</div>