如何让我的 CSS @media 代码在移动设备上运行以使我的网站响应?

How do I get my CSS @media code to work on mobile devices to make my website responsive?

我的 CSS 中有以下代码,我希望它能在移动设备上堆叠页面的不同部分。但是,当我在 iPhone 浏览器 (Safari) 中打开该网站时,它只会打开我页面的桌面版本。这是 CSS:

body {
  display: grid;
  grid-template-areas:
    'header header header'
    'nav article ads'
    'nav footer footer';
  grid-template-rows: 80px 1fr 70px;
  grid-template-columns: 20% 1fr 15%;
  grid-row-gap: 1px;
  grid-column-gap: 1px;
  height: 100vh;
  margin: 0;
}

a {
  color: #0ad05b;
  text-decoration: none;
}

a:hover {
  color: #e3eaee;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}
footer,
article,
nav,
div {
  padding: 1.2em;
  background: #061621;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
  font-size: 14px;
}
textarea {
  font-family: Arial, Helvetica, sans-serif;
}
#pageHeader {
  grid-area: header;
}
#pageFooter {
  grid-area: footer;
}
#mainArticle {
  grid-area: article;
  align-content: flex-start;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: flex-start;
}
#mainNav {
  grid-area: nav;
}
#siteAds {
  grid-area: ads;
}
/* Stack the layout on small devices/viewports. */
@media all and (max-width: 575px) {
  body {
    grid-template-areas:
      'header'
      'article'
      'ads'
      'nav'
      'footer';
    grid-template-rows: 80px 1fr 70px 1fr 70px;
    grid-template-columns: 1fr;
  }
}
.innerArticle {
  height: 16vh;
  width: 16vh;
}
.big-user-photo {
  border-radius: 50%;
}
.userAvatar {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}
.mainPage {
  height: 3rem;
  width: 3rem;
  border-radius: 50%;
  margin-right: 1rem;
}
.headerRight {
  grid-area: header;
  justify-self: right;
  padding: 1.2em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}
.right {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 0.5em;
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}

.left {
  background: #21313c;
  font-family: Helvetica, Arial, sans-serif;
  color: #e3eaee;
}

.form-user-photo {
  height: 15rem;
  width: 15rem;
}

.alert {
  position: fixed;
  top: 0;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  z-index: 9999;
  color: #fff;
  font-size: 1.8rem;
  font-weight: 400;
  text-align: center;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
  padding: 1.6rem 15rem;
  -webkit-box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
  box-shadow: 0 2rem 4rem rgba(0, 0, 0, 0.25);
}
.alert--success {
  background-color: #0ad05b;
}
.alert--error {
  background-color: #eb4d4b;
}

当我在 phone 上打开页面时,它是这样显示的(来自 iPhone 的屏幕截图):

我希望有人能告诉我我错过了什么。谢谢。

您必须将此标记添加到 HTML 的 <head> 以告知浏览器正确处理响应。

<meta name="viewport" content="width=device-width, initial-scale=1.0">

来自 W3School,

The width=device-width part sets the width of the page to follow the screen-width of the device

更多信息here