1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MediaQuery - LandScapeが意図せずに適用される話(Chrome×Android)

Last updated at Posted at 2021-04-06

発生した事象

  • 事象

    • スマホ縦画面の入力フォームで、キーボードを起動すると、画面スタイルにLandScape用のスタイルが適用される
  • 発生条件

    • Chrome×Android環境
    • MediaQueryでLandScape時のスタイルを設定している
  • イメージは下記

sample.html
<div class="container">
  <div class="main-content-wrapper">
    <div>こんにちは<div>
  </div>
  <div class="input-wrapper">
    <input type=text placeholder="何か入力して">
  </div>
</div>
sample.css
/* スマホが横向きのときに、コンテンツを横並びにしたい */
@media (orientation landscape) and (max-width: 959px) {
  .container {
    display: flex;
  }
}

調査

When the software keyboard is displayed in Chrome for Android or the stock Android Browser[1] the browser window is resized, which triggers a window.resize event and a window.orientationchange event in JavaScript as well as possibly causing CSS media queries to be reapplied.

  • Android×Chromeの場合に、ブラウザウィンドウがリサイズされてしまい、JSではWindowの向きが変わったと認識されたり、CSSのメディアクエリがリサイズされたウィンドウで再適用されてしまうとのこと

解決方法

sample.css
@media (orientation: landscape) and (min-aspect-ratio: 16/9) and (max-width: 959px) {
  .container {
    display: flex;
  }
}

以上です

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?