修复移动设备上的CSS宽度问题

时间:2020-08-29 19:18:49

标签: html css

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

@import "variables";
@import "utilities";
body {
  color: $neutral;
  font-family: "Open Sans", sans-serif;
  background-color: $main-bg;
}

a {
  color: $cyan;
  text-decoration: none;
}

ul {
  list-style: none;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-family: "Raleway", sans-serif;
}

p {
  font-family: "Raleway", sans-serif;
}

#header {
  grid-area: heading;
  height: 100vh;
  background-color: $intro-email;
  position: relative;
  background-image: $bg-image;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: left bottom;
  //   &::before {
  //     content: "";
  //     background-image: $bg-image;
  //     position: absolute;
  //     width: 100%;
  //     background-size: contain;
  //     background-repeat: no-repeat;
  //     background-position: left bottom;
  //   }
  .navbar {
    grid-area: heading;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6rem;
    .nav-list {
      display: flex;
      li {
        padding: 1rem 1.2rem;
        text-transform: uppercase;
        a {
          text-decoration: none;
          color: #fff;
          text-transform: uppercase;
          border-bottom: 3px transparent solid;
          padding-bottom: 0.5rem;
          transition: border-color 0.5s;
          font-size: 0.8rem;
          font-weight: 400;
          font-family: "Raleway", sans-serif;
          &:hover {
            border-color: #fff;
          }
          &.current {
            border-color: #fff;
          }
        }
      }
    }
  }
  .header-content {
    max-width: 100%;
    margin: 20px auto;
    text-align: center;
    width: 600px;
    img {
      max-width: 90%;
      margin-top: -50px;
    }
  }
}

Variabes for CSS $intro-email: hsl(217, 28%, 15%);
$main-bg: hsl(218, 28%, 13%);
$footer-bg: hsl(216, 53%, 9%);
$testimonial-bg: hsl(219, 30%, 18%);
$neutral: hsl(0, 0%, 100%);
$cyan: hsl(176, 68%, 64%);
$blue: hsl(198, 60%, 50%);
$website-width: 1440px;
$bg-image: url("../img/bg-curvy-desktop.svg");
Container .container {
  width: $website-width;
  max-width: 100%;
  padding: 2rem;
  margin: 0 auto;
  overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@400;700&display=swap" rel="stylesheet" />

<header id="header">
  <div class="container">
    <div class="wrapper">
      <nav class="navbar">
        <img src="../dist/img/logo.svg" alt="" />
        <ul class="nav-list">
          <li><a href="#" class="current">Features</a></li>
          <li><a href="#">Team</a></li>
          <li><a href="#">SignIn</a></li>
        </ul>
      </nav>
      <div class="header-content">
        <img src="../dist/img/illustration-intro.png" alt="" />
        <h1 class="title">
          All your files in one secue location, accessible anywhere
        </h1>
        <p class="text">
          Flyo stores all your most important files in one secure location. Access them whenever you need, share and collaborate with friends, family and co-workers
        </p>
        <button class="btn-main">Get Started</button>
      </div>
    </div>
  </div>[Image showing what is happening in my code][1]
</header>

网站的宽度设置为1440像素,但是只要我移动到屏幕上的移动版本,网站的宽度就永远不会相同,并且网站的某些元素开始不在网站的位置范围内,有没有办法解决此问题,还是我应该删除嵌套元素的容器,因为那是设计的网站宽度

enter image description here

1 个答案:

答案 0 :(得分:1)

对于您的容器,请考虑使用width: 100vw之类的自适应宽度(即视口/窗口宽度的100%)。或者,如果您确实想坚持使用1440px,则可以使用媒体查询。

媒体查询有助于将不同的CSS规则应用于不同的设备屏幕尺寸,例如:

@media(max-width: 599px) {
    .container {
        /* Your styling for mobile phones */
    }
}

@media(min-width: 600px) {
    /* Your styling for desktops */
}