CSS uses calc() to obtain the current visible screen height

CSS uses calc() to obtain the current visible screen height

First, let's take a look at the relative length units of CSS3 (refer to the detailed tutorial):

Relative length units specify the property of one length relative to another length. Relative lengths are more applicable to different devices.

em It describes the size relative to the font used in the current element, so it is also a relative length unit. The default font size of general browsers is 16px, so 2em == 32px;
ex Depends on the height of the English letter x
ch The width of the number 0
rem The font-size of the root element (html)
vw viewpoint width, viewpoint width, 1vw = 1% of the viewpoint width
v viewpoint height, window height, 1vh = 1% of the window height
vmin The smaller of vw and vh.
vmax The larger of vw and vh.

It can be seen that through vh / vw we can get the current screen window width, so in CSS, by calculating this height, the height of the div can be automatically expanded to the screen height. To calculate this height, you can use the calc() function of CSS3 (refer to the detailed tutorial):

The calc() function is used to dynamically calculate the length value.

  • It should be noted that a space is required before and after the operator, for example: width: calc(100% - 10px);
  • Any length value can be calculated using the calc() function;
  • The calc() function supports "+", "-", "*", "/" operations;
  • The calc() function uses standard mathematical operation precedence rules;

So, just set the height of the div to calc(100vh) , 100vh = 100% of the window height, example:

div {
  width: 100%;
  height: calc(100vh);
}

It should be noted that this method is suitable for the web page height being equal to the current screen window height, and JS may still be required according to actual needs.

This is the end of this article about how to use calc() in CSS to get the current visible screen height. For more information about how to use calc() in CSS to get the current screen height, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Discuss the value of Web standards from four aspects with a mind map

>>:  Some simple implementation codes of the form element take registration as an example

Recommend

JavaScript design pattern chain of responsibility pattern

Table of contents Overview Code Implementation Pa...

Detailed explanation of JavaScript error capture

Table of contents 1. Basic usage and logic 2. Fea...

How to keep running after exiting Docker container

Phenomenon: Run an image, for example, ubuntu14.0...

Share the 15 best HTML/CSS design and development frameworks

Professional web design is complex and time-consu...

v-for directive in vue completes list rendering

Table of contents 1. List traversal 2. The role o...

MySQL 5.7.17 installation and configuration graphic tutorial

The blogger said : I have been writing a series o...

Detailed tutorial on installing and using Kong API Gateway with Docker

1 Introduction Kong is not a simple product. The ...

Summary of the differences between Vue's watch, computed, and methods

Table of contents 1 Introduction 2 Basic usage 2....

JavaScript to achieve digital clock effects

This article example shares the specific code for...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

vue $set implements assignment of values ​​to array collection objects

Vue $set array collection object assignment In th...

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

Implementing the preview function of multiple image uploads based on HTML

I recently wrote a script for uploading multiple ...