【问题标题】:CSS grid not displaying correctly on actual mobile browsers (all displays correctly when testing mobile screens on desktop though )CSS 网格无法在实际的移动浏览器上正确显示(尽管在桌面上测试移动屏幕时所有显示都正确)
【发布时间】:2020-04-10 02:17:02
【问题描述】:

CSS 网格在实际的移动浏览器上显示不正确(但在桌面上测试移动屏幕时所有显示都正确)。

如果我在桌面浏览器上打开应用程序并使用浏览器对不同移动屏幕尺寸和类型的测试,我发现样式有效。

但是,如果我使用(实际)手机的浏览器单击该链接,我会发现样式不起作用。

即浏览器调试具有误导性。这是不一致的,而不是应该如何工作。

问题:

  1. 我错过了什么?为什么 devTools 与移动设备上的现实存在差异?
  2. 什么媒体查询可以在移动设备上修复这种样式,同时仍然保持它基于 CSS 网格(我没有尝试修复它)?
    body {
        font-family: 'Helvetica Neue', sans-serif;
             font-weight: 400;
             height: 100%;
             width: 100%;
             margin: 0;
             padding: 0;
             background: #53687e;
             background: linear-gradient(120deg, #53687e, hsl(0, 0%, 18%)) fixed;
    }  

       #calculator {
          margin: 0 auto;
          text-align: center;
          padding-top: 100px;
        }

        .container {
          position: relative;
          line-height: 1.5;
          display: flex;
          align-items: center;
          justify-content: center;
            padding-bottom: 50px;
        }

        .key-group {
          display: grid;
          grid-template-columns: 1fr 1fr 1fr 1fr;
          grid-gap: 10px;
          align-items: center;
          justify-content: center;
          background-color: #000;
          padding: 8px;
          border-radius: 8px;
          box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);

        }


        .key-pad {
          cursor: pointer;
          background: #33373f;
          color: #ffff;
          border-radius: 8px;
          font-size: 2.5rem;
          border-style: none;
          padding-right: 50px;
          padding-bottom: 50px;
          padding-left: 50px; */
        }

        .display {
          grid-column: 1 / span 4;
          grid-row: 1;
          color: #51aaff;
          background: none;
          height: 5rem;
          display: flex;
          align-items: flex-end;
          justify-content: flex-end;
        }
        .seven {
          grid-column: 1 / span 1;
                grid-row: 2;
        }
        .eight {
          grid-column: 2 / span 1;
                grid-row: 2;
        }
        .nine {
          grid-column: 3 / span 1;
                grid-row: 2;
        }
        .divide {
          grid-column: 4 / span 1;
          grid-row: 2;
          color: #51aaff;
          background-color: #1e242c;
        }
        .four {
          grid-column: 1 / span 1;
          grid-row: 3;
        }
        .five {
          grid-column: 2 / span 1;
          grid-row: 3;
        }
        .six {
          grid-column: 3 / span 1;
          grid-row: 3;
        }
        .multiply {
          grid-column: 4 / span 1;
          grid-row: 3;
          color: #51aaff;
          background-color: #1e242c;
        }
        .one {
          grid-column: 1 / span 1;
          grid-row: 4;
        }
        .two {
          grid-column: 2 / span 1;
          grid-row: 4;
        }
        .three {
          grid-column: 3 / span 1;
          grid-row: 4;
        }
        .subtract {
          grid-column: 4 / span 1;
          grid-row: 4;
          color: #51aaff;
          background-color: #1e242c; 
        }
        .zero {
          grid-column: 1 / span 1;
          grid-row: 5;
        }
        .decimal {
          grid-column: 2 / span 1;
          grid-row: 5;
        }
        .clear {
          grid-column: 3 / span 1;
          grid-row: 5;
          color: #51aaff;
          background-color: #1e242c;
          font-size: 2rem;
          height: 100%;
          width: 100%;  
        }
        .add {
          grid-column: 4 / span 1;
          grid-row: 5;
          color: #51aaff;
          background-color: #1e242c;
        }
        .equals {
          grid-column: 1 / span 4;
          grid-row: 6;
          color: #51aaff;
          background-color: #1e242c;
        }


        footer {
          position: fixed;
            font-size: small;
          color: #fff;
          background-color: #262626;
            display: flex;
          width: 100vw;
          height: 4rem;
          bottom: 0;
        }

        .footer-options {
                width: 50vw;
                display: flex;
                justify-content: flex-start;
            padding-inline-start: 20px;
            align-items: center;
        }


        footer span {
                width: 50vw;
                display: flex;
                justify-content: flex-end;
                padding: 0 20px;
            align-items: center;
            }

        @media (max-width: 900px)  {

            footer {
                        flex-wrap: wrap;
                height: 5rem;  
            }

            .footer-options {
                width: 100vw;
                flex-direction: row;
                justify-content: center;
            padding-inline-start: 0;
            align-items: center;
            }

            footer span {
                        width: 100vw;
                        flex-direction: column;
                    text-align: center;
                align-self: flex-start;
            }
        }

相关反应代码:

return (        
    <React.Fragment>
        <header>
        {/* <h1>Calculator</h1> */}
        </header>
        <div id="calculator">
  <div className="container">
            <div className="key-group">

      <div id="display" className="key-pad display">{display != 0 ? display : 0}</div>                   
    <button id="seven" className="key-pad seven" onClick={() => Input(7)}>7</button>
      <button id="eight" className="key-pad eight" onClick={() => Input(8)}>8</button>
      <button id="nine" className="key-pad nine" onClick={() => Input(9)}>9</button>
      <button id="divide" className="key-pad divide" onClick={() => {Operator("/")}}>÷</button>
      <button id="four" className="key-pad four" onClick={() => Input(4)}>4</button>
      <button id="five" className="key-pad five" onClick={() => Input(5)}>5</button>
      <button id="six" className="key-pad six" onClick={() => Input(6)}>6</button>       
      <button id="multiply" className="key-pad multiply" onClick={() => {Operator("*")}}>×</button>              
      <button id="one" className="key-pad one" onClick={() => Input(1)}>1</button>
      <button id="two" className="key-pad two" onClick={() => Input(2)}>2</button>
      <button id="three" className="key-pad three" onClick={() => Input(3)}>3</button>
      <button id="subtract" className="key-pad subtract" onClick={() => {Operator("-")}}>-</button>
      <button id="zero" className="key-pad zero" onClick={() => Zero()}>0</button>
      <button id="decimal" className="key-pad decimal" onClick={() => Decimal()}>.</button>
      <button id="clear" className="key-pad clear" onClick={() => Clear()}>AC</button>
      <button id="add" className="key-pad add" onClick={() => {Operator("+")}}>+</button>       
      <button id="equals" className="key-pad equals" onClick={()=>Equals()}>=</button>   
    </div>      
  <footer>
    <ul className="footer-options">
  <li className="footer-link"><a href="#" className="footer-linktext">Legal</a></li>
  <li className="footer-link"><a href="#" className="footer-linktext">Contact Us</a></li>
</ul>
<span>© 2019 Developed by Pat. All Rights Reserved.</span>
       </footer>
                </div>
            </div>
        </React.Fragment>
      )  

HTML:

<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Calculator</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Unica+One&display=swap" rel="stylesheet">
      <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css">
    </head>
    <body>
      <div id="app">
      </div> 
    </body>
    </html>

【问题讨论】:

  • 你试过@media规则CSS
  • @KiranMistry 感谢您的提问。媒体查询被用于必要的地方,即页脚。如果您单击我在您的桌面上提供的第一个链接并使用浏览器对不同移动屏幕尺寸和类型的测试,您将看到样式有效。但是,如果您使用(实际)手机单击该链接,您会发现样式不起作用。浏览器调试具有误导性。这是不一致的,而不是应该如何工作。
  • 您使用的是哪种手机浏览器?r
  • 也将您的 HTML 发布到问题中。
  • @Pete 感谢您的提问。在桌面上:在 Safari、Chrome 和 Brave 中尝试过。在移动设备上:Safari、Chrome 和 Opera

标签: css css-grid


【解决方案1】:

我已经从您的code 更改了一些html,而且我使用flexbox 而不是grid

这里是example

HTML

<div class="calculator" id="calculator">
    <div class="container">
        <div class="key-area">
            <div id="display" class="display">0</div>
            <div class="key-group">
                <div class="key-item"><button id="seven" class="key-pad seven">7</button></div>
                <div class="key-item"><button id="eight" class="key-pad eight">8</button></div>
                <div class="key-item"><button id="nine" class="key-pad nine">9</button></div>
                <div class="key-item"><button id="divide" class="key-pad divide">÷</button></div>
                <div class="key-item"><button id="four" class="key-pad four">4</button></div>
                <div class="key-item"><button id="five" class="key-pad five">5</button></div>
                <div class="key-item"><button id="six" class="key-pad six">6</button></div>
                <div class="key-item"><button id="multiply" class="key-pad multiply">×</button></div>
                <div class="key-item"><button id="one" class="key-pad one">1</button></div>
                <div class="key-item"><button id="two" class="key-pad two">2</button></div>
                <div class="key-item"><button id="three" class="key-pad three">3</button></div>
                <div class="key-item"><button id="subtract" class="key-pad subtract">-</button></div>
                <div class="key-item"><button id="zero" class="key-pad zero">0</button></div>
                <div class="key-item"><button id="decimal" class="key-pad decimal">.</button></div>
                <div class="key-item"><button id="clear" class="key-pad clear">AC</button></div>
                <div class="key-item"><button id="add" class="key-pad add">+</button></div>
                <div class="key-item"><button id="equals" class="key-pad equals">=</button></div>
            </div>   
        </div>
    </div>
</div>

CSS

*:after, *:before {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}
body {
        font-family: 'Helvetica Neue', sans-serif;
        font-weight: 400;
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
        background: #53687e;
    background: linear-gradient(120deg, #53687e, hsl(0, 0%, 18%)) fixed;
}
.calculator .container {
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
  padding-left: 8px;
  padding-right: 8px;
}
.calculator .container .key-area {
  background-color: black;
  -webkit-border-radius: 8px;
     -moz-border-radius: 8px;
          border-radius: 8px;
  -webkit-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
     -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
          box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
}
.calculator .container .key-area .display {
  cursor: pointer;
  background: transparent;
  font-size: 2.5rem;
  border: none;
  color: #51aaff;
  text-align: right;
  padding: 8px 8px 0;
}
.calculator .container .key-area .key-pad {
  cursor: pointer;
  background: #33373f;
  border-radius: 5px;
  color: #ffff;
  font-size: 2rem;
  border: none;
  padding: 8px;
  width: 100%;
}
.calculator .container .key-area .key-pad.divide, .calculator .container .key-area .key-pad.multiply, .calculator .container .key-area .key-pad.subtract, .calculator .container .key-area .key-pad.clear, .calculator .container .key-area .key-pad.add, .calculator .container .key-area .key-pad.equals {
  color: #51aaff;
  background-color: #1e242c;
}
.calculator .container .key-area .key-group {
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  flex-direction: row;
  -webkit-flex-wrap: wrap;
      -ms-flex-wrap: wrap;
          flex-wrap: wrap;
}
.calculator .container .key-area .key-group .key-item {
  -webkit-flex-basis: 25%;
      -ms-flex-preferred-size: 25%;
          flex-basis: 25%;
  -webkit-box-flex: 0;
  -webkit-flex-grow: 0;
     -moz-box-flex: 0;
      -ms-flex-positive: 0;
          flex-grow: 0;
  -webkit-flex-shrink: 0;
      -ms-flex-negative: 0;
          flex-shrink: 0;
  max-width: 25%;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: horizontal;
  -webkit-box-direction: normal;
  -webkit-flex-direction: row;
     -moz-box-orient: horizontal;
     -moz-box-direction: normal;
      -ms-flex-direction: row;
          flex-direction: row;
  padding: 8px;
}
.calculator .container .key-area .key-group .key-item:last-child{
  flex-basis: 100%;
  max-width: 100%
}

我将max-width 用于container。您可以将max-width 更改为@media (max-width:*)@media (min-width:*)

在@Pat cmets 之后编辑此答案

使用gridCSS可以修复

例如Calculator

说明

我看到你没有使用Normalize.cssNormalize.css 是一个小的 CSS 文件,它在 HTML 元素的默认样式中提供更好的跨浏览器一致性。它是一种现代的、支持 HTML5 的替代传统 CSS 重置的方法。

Viewport meta tags 中最重要的你错过了。你使用了&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;。但是你必须使用&lt;meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no"&gt;。使用shrink-to-fit=no 将其添加到视口元标记的好处可以恢复 Safari 9.0 之前的行为。很好的解释是here

您还没有使用box model *{box-sizing: ?;}box-sizing CSS 属性设置如何计算元素的总 widthheight。我用*{box-sizing: border-box;}*:after,*:before{box-sizing: border-box}。详情见MDN

您使用grid-gapgrid-gap 已被弃用,取而代之的是 gap。 gap CSS 属性设置rowscolumns 之间的间隙(gutters)。它是row-gapcolumn-gap 的简写。 CSS 网格布局最初定义了grid-gap 属性。这个带前缀的属性正在被 gap 取代。但是,为了支持实现了grid-gap 而不是gapgrid 的浏览器,您需要使用prefixed 属性。详情见MDN

您使用grid-column 属性在父设置grid-template-columns: repeat(4, 1fr); 的每个子元素。您无需指定每个子元素。您只需要将第一个和最后一个子元素指定为特定的grid-column: 1 / span 4;

注意:我根据您的示例更改了 display class 的字体系列和高度。

这里是 HTML

<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro&display=swap" />
        <!-- CSS -->
        <link rel="stylesheet" href="css/style.css" />

        <title>Calculator</title>
    </head>
    <body>
        <div class="calculator" id="calculator">
            <div class="container">
                <div class="key-group">
                    <div id="display" class="key-pad display">0</div>
                    <button id="seven" class="key-pad seven">7</button>
                    <button id="eight" class="key-pad eight">8</button>
                    <button id="nine" class="key-pad nine">9</button>
                    <button id="divide" class="key-pad divide">÷</button>
                    <button id="four" class="key-pad four">4</button>
                    <button id="five" class="key-pad five">5</button>
                    <button id="six" class="key-pad six">6</button>
                    <button id="multiply" class="key-pad multiply">×</button>
                    <button id="one" class="key-pad one">1</button>
                    <button id="two" class="key-pad two">2</button>
                    <button id="three" class="key-pad three">3</button>
                    <button id="subtract" class="key-pad subtract">-</button>
                    <button id="zero" class="key-pad zero">0</button>
                    <button id="decimal" class="key-pad decimal">.</button>
                    <button id="clear" class="key-pad clear">AC</button>
                    <button id="add" class="key-pad add">+</button>
                    <button id="equals" class="key-pad equals">=</button>
                </div>
            </div>
        </div>
    </body>
</html>

这里是 CSS

* {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box; }

*:after, *:before {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box; }

[role="button"] {
  cursor: pointer; }

body {
  background: #53687e;
  background: -webkit-linear-gradient(330deg, #53687e, #2e2e2e) fixed;
  background: -moz-linear-gradient(330deg, #53687e, #2e2e2e) fixed;
  background: -o-linear-gradient(330deg, #53687e, #2e2e2e) fixed;
  background: linear-gradient(120deg, #53687e, #2e2e2e) fixed;
  font-family: 'Source Sans Pro', sans-serif;
  font-weight: 400;
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  -moz-osx-font-smoothing: grayscale; }

.calculator {
  margin: 0 auto;
  text-align: center;
  padding-top: 100px; }

.calculator .container {
  -webkit-box-align: center;
  -webkit-align-items: center;
     -moz-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  line-height: 1.5;
  padding-bottom: 50px;
  position: relative; }

.calculator .container .key-group {
  background-color: black;
  -webkit-border-radius: 8px;
     -moz-border-radius: 8px;
          border-radius: 8px;
  -webkit-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
     -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
          box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(4, 1fr);
  overflow: hidden;
  padding: 8px; }

.calculator .container .key-group .key-pad {
  background-color: #33373f;
  -webkit-border-radius: 5px;
     -moz-border-radius: 5px;
          border-radius: 5px;
  color: #ffff;
  cursor: pointer;
  font-size: 2rem;
  border: none;
  margin: 0;
  padding: 8px; }

.calculator .container .key-group .key-pad.display {
  -webkit-box-align: end;
  -webkit-align-items: flex-end;
     -moz-box-align: end;
      -ms-flex-align: end;
          align-items: flex-end;
  cursor: default;
  background-color: transparent;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  font-size: 2.5rem;
  grid-column: 1 / span 4;
  height: 5rem;
  -webkit-box-pack: end;
  -webkit-justify-content: flex-end;
     -moz-box-pack: end;
      -ms-flex-pack: end;
          justify-content: flex-end;
  text-align: right; }

.calculator .container .key-group .key-pad.divide, .calculator .container .key-group .key-pad.multiply, .calculator .container .key-group .key-pad.subtract, .calculator .container .key-group .key-pad.clear, .calculator .container .key-group .key-pad.add, .calculator .container .key-group .key-pad.equals {
  background-color: #1e242c;
  color: #51aaff; }

.calculator .container .key-group .key-pad.equals {
  grid-column: 1 / span 4; }

希望对您有所帮助!

【讨论】:

  • 非常感谢@rahul,但在这个问题上,我真的很想 1) 了解移动屏幕的桌面调试与设备本身的样式呈现之间的差异(需要这个继续信任用于 CSS 网格样式的 devTools),但也 2)寻找任何媒体查询的建议,这些查询可能会在仍然使用 CSS 网格的同时修复此样式(因为我正在实施的任何事情都无法修复它)。
  • 您必须使用css prefixer 来支持最新和旧浏览器。您的key-group 需要fixed width 用于桌面,因为它将帮助计算您的grid 项目width。对于移动设备@media,您可以使用max-width,即:@media (max-width: 424px){.key-group{width: 100%;}} 在移动设备视图中key-group 应该是100% widthdisplay: grid 存在浏览器兼容性问题。不支持IE。需要将grid-template-columns: 1fr 1fr 1fr 1fr; 替换为grid-template-columns: repeat(4, 1fr);.key-pad:first-childkey-pad:last-child 将是 grid-column: 1 / span 4;
  • Codepen 目前在 Safari 中存在问题。他们建议使用 ChromeFirefox
  • @Pat 我使用grid CSS 制作了实时链接raw.githack.com/RahulMoral/calculator/master/index.html。检查这个。它在 IOS 移动设备中的工作原理。
  • 如果您的 iOS Safari 浏览器 3.2 和 10.0 - 10.2 grid CSS 将不支持。
【解决方案2】:

经过多次修改:

我试图用字体大小来控制网格单元格的高度,并且不知何故这会在 iOS 设备上同时缩放宽度和高度。 IOS safari 上的 CSS 网格确实有些问题。

我已通过为行分配固定高度并从网格单元格中删除字体大小、高度和宽度属性来修复它。

.key-group {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.2rem;
  align-items: center;
  justify-content: center;
  background-color: #000;
  padding: 8px;
  border-radius: 8px;
  box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.7);
  width: 20rem;
  height: 25rem;
}

.key-pad {
  cursor: pointer;
  background: #33373f;
  color: #ffff;
  height: 4rem;
  border-radius: 4px;
  border-style: none; 
}
```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 1970-01-01
    • 2019-10-11
    • 2020-10-01
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多