2

CSS grid is not displaying correctly on actual mobile browsers (all displays correctly when testing mobile screens on desktop though).

If I open the app on a desktop's browser and use the browser's testing of different mobile screen sizes and types, I see that the styling works. enter image description here

However, if I click the link using my (actual) mobile phone's browser I see that the styling does not work. enter image description here

i.e. Browser debugging is misleading. This is inconsistent and not how this should work.

Questions:

  1. What am I missing? Why the discrepancy between devTools and the reality on the mobile device?
  2. What media query could fix this styling on mobile, while still keeping it CSS grid based (nothing I try fixes it)?
    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;
            }
        }

Relevant React code:

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>
Pat
  • 443
  • 6
  • 13
  • have you tried `@media` rule CSS – Kiran Mistry Dec 17 '19 at 14:52
  • @KiranMistry Thanks for your question. Media queries are being used where they were necessary, i.e. for the footer. If you click on the first link I've provided on your desktop and use the browser's testing of different mobile screen sizes and types you will see the styling works. However, if you click the link using your (actual) mobile phone you'll see that the styling does not work. Browser debugging is misleading. This is inconsistent and not how this should work. – Pat Dec 17 '19 at 14:57
  • Which mobile browser are you using?r – Pete Dec 17 '19 at 15:35
  • Post your HTML to the question as well. – disinfor Dec 17 '19 at 15:35
  • @Pete Thanks for asking. On desktop: Tried in Safari, Chrome and Brave. On mobile: Safari, Chrome and Opera – Pat Dec 17 '19 at 15:36
  • @disinfor Thanks for writing. I'm building the UI with React. So, the HTML and the React code will be a ton of code :) Could you check the links for that code? Or I can post it all here, but may detract from css-centric question? – Pat Dec 17 '19 at 15:38
  • You can still post the relevant rendered HTML. Minimal example and all: Please read: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – disinfor Dec 17 '19 at 15:40
  • @disinfor Sure, done. both HTML and React relevant code added. – Pat Dec 17 '19 at 15:46
  • Copy/paste your code, ran it, accessed it from my Nexus 5 Android 7.1.2 Chrome 78, Everything works fine ? – Rainbow Dec 17 '19 at 19:59
  • Thanks @ZohirSalak for writing. Just tried on an Android and the styling works! the styling however does not work on any browser running on IOS (neither Safari, Chrome, Brave or Opera - well they are all Safari anyways...). Finally, moving forward. Is there any polyfill to make this work on IOS devices? – Pat Dec 17 '19 at 20:26
  • According to this: https://caniuse.com/#search=grid CSS grid should work on latest IOS... my CSS grid code, however, clearly does not work. Pointers on how to make this work on Safari, welcome – Pat Dec 17 '19 at 20:46
  • Have in mind that CSS grid does not have fully support on old browsers. https://caniuse.com/#feat=css-grid – Gustavo Santamaría Dec 19 '19 at 16:26

2 Answers2

0

I have changed some html from your code also I'm use flexbox instead of grid.

Here is 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%
}

I use max-width for container. You can change max-width in @media (max-width:*) or @media (min-width:*).

Edit this answer after @Pat comments

Using grid CSS it can be fix

Example Calculator

Explanation

I seen you haven't use Normalize.css. Normalize.css is a small CSS file that provides better cross-browser consistency in the default styling of HTML elements. It’s a modern, HTML5-ready, alternative to the traditional CSS reset.

Most important you missing in Viewport meta tags. You used <meta name="viewport" content="width=device-width, initial-scale=1.0">. But you have to use <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">. Benefit using shrink-to-fit=no adding this to the viewport meta tag restores pre-Safari 9.0 behaviour. Good explanation is here.

You haven't use box model *{box-sizing: ?;}. The box-sizing CSS property sets how the total width and height of an element is calculated. I used *{box-sizing: border-box;}*:after,*:before{box-sizing: border-box}. Details in MDN.

You use grid-gap. grid-gap has been deprecated in favor of gap. The gap CSS property sets the gaps (gutters) between rows and columns. It is a shorthand for row-gap and column-gap. CSS Grid Layout initially defined the grid-gap property. This prefixed property is being replaced by gap. However, in order to support browsers that implemented grid-gap and not gap for grid, you will need to use the prefixed property. Details in MDN.

You use grid-column property every child element where parent set grid-template-columns: repeat(4, 1fr);. You no need to specific every child element. You just need to first and last child element need to specific grid-column: 1 / span 4;.

Note: I changed font family and given height for display class according your example.

Here is 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>

Here is 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; }

Hope this help!.

Rahul
  • 2,011
  • 3
  • 18
  • 31
  • Super thanks for this @rahul, but on this question I'm really trying to 1) understand that discrepancy between desktop debugging of mobile screens and the rendering of the style on the devices themselves (need this to continuing trusting devTools for CSS grid styling), but also 2) looking for suggestions for any media queries that might fix this styling while still using CSS grid (because nothing I'm implementing is fixing it). – Pat Dec 17 '19 at 18:46
  • You have to use `css prefixer` for support latest and old browser. Your `key-group` need `fixed` `width` for desktop because it will help calculate your `grid` item `width`. For mobile `@media` you can use `max-width` i.e.: `@media (max-width: 424px){.key-group{width: 100%;}}` In mobile view `key-group` should be `100% width`. `display: grid` have browser compatibility issue. IE isn't support. Need to replace `grid-template-columns: 1fr 1fr 1fr 1fr;` to `grid-template-columns: repeat(4, 1fr);`. `.key-pad:first-child` and `key-pad:last-child` will be `grid-column: 1 / span 4;`. – Rahul Dec 17 '19 at 19:33
  • Codepen have issue in **Safari** at this moment. They are suggesting to use **Chrome** and **Firefox**. – Rahul Dec 18 '19 at 17:38
  • 1
    @Pat I made live link https://raw.githack.com/RahulMoral/calculator/master/index.html using `grid` `CSS`. Check this. How it work in **IOS** Mobile. – Rahul Dec 19 '19 at 17:21
  • If your **iOS** Safari browser 3.2 and 10.0 - 10.2 `grid` **CSS** will not support. – Rahul Dec 19 '19 at 17:29
  • That is a great contribution @Rahul. Thank you!. I am not being able to explain why your code works and mine didn't, but maybe you can? :-) Want to "Add another answer" to this question with the code and your explanation? I'll then accept that as an answer. – Pat Dec 19 '19 at 18:43
0

After much tinkering:

I was trying to control grid cell height with font-size and somehow this scales both width and height on iOS devices. Something about CSS grid on IOS safari is indeed off.

I’ve fixed it by assigning fixed height to rows and removing font-size, height and width properties away from grid cells.

.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; 
}
```
Gustavo Santamaría
  • 837
  • 1
  • 10
  • 21
Pat
  • 443
  • 6
  • 13