Thursday, September 14, 2023
HomeCSSThe digital key-board API

The digital key-board API

Have you find a problem where there is a set component on mobile, and also when the key-board is triggered, that component will be concealed beneath the key-board?

This has actually been a default actions on the internet for many years. In this post, we’ll discover the trouble, why it takes place, and also exactly how we can fix it today with the digital key-board API.

Allow’s dive in.

The trouble

Prior to diving right into the great information, allow us go through an instance. This is a UI that has the following:

  • Sticky header
  • Sticky drifting activity switch (FAB)

When the individual concentrates on the input, the digital key-board will certainly reveal. Can you anticipate what will occur?

The internet browser will certainly scroll upwards to make the input over the key-board, and also therefore the sticky header and also drifting switch will certainly go away.

It looks comparable to the following:

This is a default actions in mobile web browsers generally. From a UX viewpoint, it may be frustrating to conceal components of the UI, specifically those components that belong to the present activity I’m doing while the key-board is energetic.

Behind the scenes, what takes place is something comparable to the complying with number.

In technological terms, the noticeable component is called the aesthetic viewport, and also the covert components together with what’s presently noticeable is the design viewport

The primary trouble is that the aesthetic viewport reduces in dimension when the digital key-board is energetic.

Repair material concealed under the key-board with the digital key-board API

Many thanks to the digital key-board API, we can specify that both the aesthetic and also design viewports are equivalent. With that said, we can identify the key-board placement and also measurements with the complying with CSS atmosphere variables:

  • keyboard-inset-top
  • keyboard-inset-right
  • keyboard-inset-bottom
  • keyboard-inset-left
  • keyboard-inset-width
  • keyboard-inset-height

By utilizing the above variables, we can change a format when the digital key-board is energetic.

Internet browser assistance

At the time of composing this post, the VirtualKeyboard API is sustained just in Chrome for Android.

In the following area, I will certainly discover a couple of instances and also use-cases where it can be valuable.

Allowing the digital key-board API

This API isn’t offered by default. We require to utilize Javascript to allow it.

See the following:

 if (" virtualKeyboard"  in navigator)  {.
navigator virtualKeyboard overlaysContent  =  real

I located this a little bit unusual; utilizing Javascript to make it possible for such actions. I concur with Bramus in his post regarding the subject. He recommended utilizing a meta tag similar to this:

< Or CSS building: 
   html { virtual-keyboard:  overlays-content
  ;}  Update: 2 Aug 2023 Bramus kindly kept in mind that there is a brand-new 

in the viewport meta tag which aids in altering the resizing actions.

 See   MDN
   for information. Usage situations for the VirtualKeyboard API Bottom-fixed activities On a smaller sized viewport, you may require to have a phone call to activity switch or footer that is dealt with to all-time low of the UI.
 Take into consideration the complying with number where we have a CTA switch that is dealt with to all-time low. In the center of the display, there is an input area.

When the input area is energetic, the check out switch will certainly be under the digital key-board, therefore it’s concealed.

We can deal with that quickly with the digital key-board API. input { font-size

: 16px;







(  keyboard-inset-height
  , 0);
 On mobile, the worth of   base
   will certainly amount to the key-board elevation, therefore balancing out the CTA switch keeping that worth. If the internet browser does not sustain the API, it will certainly skip to  absolutely no.  You may be questioning the lowered room as a result of the visibility of the header and also dealt with base. We can utilize  upright media inquiries to reveal the header if the upright room suffices. Scrolling to the actual end of the web page isn't feasible When there is a product with  placement: dealt with at the really lower of the design viewport, we normally include a 

to counter the web page and also permit the individual to scroll to the actual end. body {-- cta-height

: 60px;


: var(-- cta-height)

; }
   cta { base: 
   env(  keyboard-inset-height, 0);

 The   padding-bottom
   must be a worth that amounts to or higher than the set component elevation. Cool, right? What takes place when we entail a digital key-board? Allow's have a look.  Take into consideration the complying with number:  When the digital key-board is energetic, the  padding-bottom worth with the elevation of the set component isn't sufficient. We require to include a key-board to it. To picture the problem much better, below is a video clip:  To deal with that, we require to identify when the input is concentrated and also transform the  padding-bottom
 based upon that.

body: has( input: emphasis) { padding-bottom



( var(

— cta-height

) + env

(  keyboard-inset-height
  , 0 ))
    ;}  You might ask yourself, what will occur on desktop computer? Excellent inquiry. The  env() will certainly drop back to  0 and also the total amount will certainly cause the worth of  var(-- cta-height) Drifting activity switch In this instance, we have a drifting activity switch that is placed near the bottom appropriate edge of the web page.
   When the key-board is energetic, the drifting switch needs to relocate over it. As in the really initial instance, the drifting switch will certainly be under the key-board. To deal with that, we can utilize the 
 env( keyboard-inset-height)

worth. Allowed's go through the service: fab { base: calc


1rem +


( keyboard-inset-height,


) )
  ;}   I utilized  1rem plus the key-board elevation, to stay clear of having the drifting switch straight on top side of the key-board. With CSS contrast features, it is essential to keep in mind that utilizing a unitless number for the fallback worth within the  env() will certainly damage the entire point in Safari. We should include the device  rapid eye movement Making use of a various worth for desktop computer Intend that we wish to counter the drifting switch a little bit a lot more on desktop computer web browsers, exactly how we can do that? Well, I thought of utilizing the  max()
 contrast feature, and also it functioned.

fab { base

: max( 2rem,

1rem +

env( keyboard-inset-height

,  0rem
  )) ;}  Below is exactly how it functions:  The contrast feature will certainly contrast in between both worths. Given that the  env( keyboard-inset-height) examines to  absolutely no on desktop computer, the optimum worth is  2rem On mobile, the optimum worth is the 2nd one. To read more regarding CSS contrast features, you can review  this post

Conversation design

  1. I obtained influenced by the instance in this post by Thomas Steiner and also wished to reveal you exactly how it functions. Take into consideration the complying with number: When the key-board is energetic, both the header and also the message area are concealed. We can utilize the env( keyboard-inset-height) as a worth for the
  2. grid-row

building. design {


: grid;


: automobile minmax( 0

,  1fr
  ) automobile  env(
   keyboard-inset-height, 0); elevation:  100dvh;}  Below is exactly how it looks with the repair over:  Make use of the digital key-board API sensibly The digital key-board must be utilized just when required. Utilizing it in each context may create troubles. Yes, you review that right. Allow's take a basic instance. We have a call web page with lengthy web content and also kind inputs. If we opt-in for making the digital key-board superimposing the web page's web content, it will not be feasible to scroll to the actual end of the kind. In this instance, I do not advise having the key-board overlay the web content. Utilize it sensibly. See the complying with video clip to obtain a sensation of the trouble:  Demonstration on Codepen
   Making use of the contrast operates with the digital key-board API Changing a switch based upon the presence of the digital key-board This may be a worthless usage instance or an instance, yet it interests see what takes place when an attribute is utilized to its complete capacity. I believed to myself, why not blend CSS contrast features and also the digital key-board 

worths? I did that, and also it functioned.

See the video clip listed below:

Exactly how does that job? Right here you go:



— dimension





: dealt with;



 minutes (
   1rem, 100vw -  env
  ( keyboard-inset-width, 0rem
  )) ; base:  max( 1rem, env( keyboard-inset-height, 0rem)
  );  size:  max(  var(-- dimension), env( keyboard-inset-width
  , 0rem )); elevation:  var( -- dimension); border-radius:  max(
  , minutes ( 50px, 100% -  env
  ( keyboard-inset-width )));
    }  This services both desktop computer and also mobile. Right here is what's occurring:  The  right worth will certainly be either  1rem or  absolutely no The initial is for desktop computer, and also the last is for mobile (when the key-board is energetic).  100vw
   amounts to the key-board size because instance, therefore the result is absolutely no.  minutes( 1rem, 0)

The lower worth will certainly be either

  1. 1rem or the key-board's elevation. The size on the desktop computer dimension amounts to the -- dimension variable, and also on mobile, it will certainly take the complete size, therefore why env( keyboard-inset-width, 0) is utilized. Ultimately, the border-radius can be either 50px
  2. or 0
  3. Neat, right? I have actually never ever anticipated to find up with such a demonstration. Do you assume maybe helpful? I’m delighted to see what you will certainly construct. Demonstration on Codepen Linkedin blog post kind and also navigating An instance that I saw great capacity for using the digital key-board API is exactly how the blog post kind & & navigating are revealed for a Linkedin blog post. Take into consideration the complying with number:
  4. The blog post kind and also navigating are dealt with to all-time low. When the individual triggers the input area, it appears like this: Notification exactly how the upright realty is as well little. What do to do? By blending contrast features and also the digital key-board API, we can conceal the navigating when the key-board is revealed. Below is the CSS that does that. post-form,
    . nav
    { placement:

dealt with








 0 ;
  } post-form { base
  :  max( 48px
  , env( keyboard-inset-height

 0px )
  ); } nav { base :  max( 0px, env( keyboard-inset-height

 0 )
   - 100px) ;}  Do not stress, I will certainly discuss it gradually. Article kind  In the default state, the kind is balanced out by  48px from all-time low. In this state, the 2nd component of the  max() feature is non-active. When the key-board is energetic, the 2nd component of the  max() will certainly function and also the  base
 worth will certainly end up being the elevation of the key-board.


The navigating is placed at

base: 0 The initial component of the max() feature is what's energetic currently. When the key-board is energetic, we’ll relocate the nav under the key-board. The

100px below is an arbitrary number, the factor is to include something that is bigger than the navigating's elevation. Right here is a video clip of exactly how it functions: Demonstration on Codepen You can likewise see

in the Codepen collections. Outro That’s it for this post. I found out a whole lot regarding the digital key-board API and also can not wait to use it in my following job. The last point I anticipated is to create 1600 words on such a subject. If that indicates anything, it indicates to never ever trust your internal sensations regarding something you do not recognize. Simply begin and also the good ideas will certainly adhere to. Do you have any type of ideas or concerns? Please do not hesitate to sound me on Twitter (sorry, X) @shadeed9 or Mastodon

or Strings


The list below sources assisted me a whole lot in my first research study regarding the subject. Thanks for analysis.


Most Popular

Recent Comments