【问题标题】:Width = 100% CSS property doesn't work on mobile. Seems like Viewport gets wrong WidthWidth = 100% CSS 属性在移动设备上不起作用。似乎视口的宽度错误
【发布时间】:2020-08-14 07:47:30
【问题描述】:

我最近推出了这个网站。在移动视图中,标题不会覆盖整个屏幕宽度,而是在右侧有一个边距。我找不到让它同时在桌面和移动设备上运行的解决方案。

这是视口设置

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

网址是camisite.now.sh

这是我的 CSS,对象只是一个没有类的标题。 我尝试更改为 100vw,但大致相同。我认为 viewport-width 可能设置了错误的宽度。

@font-face{
  font-family: songenia;
  src:url("songenia.otf")
}

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


body {
    font-family: songenia;
    font-size: xx-large;
    background-image: url(background.jpg);
    line-height: 1.2;
}

.input{
  line-height: 1.6;
  margin-top: 30px;
}

  .posts {
    list-style: none;
  }

  .onepost {
  padding: 10px;
  background: rgba(215, 177, 88, 0.548);
  margin: 30px 0;
}

header {
  background: rgba(215, 177, 88, 0.548);
  padding: 2rem;
  min-width: 100%;
  text-align: center;
}

h1, h4 {
  text-align: center;
}


.error-message {
  text-align: center;
}

.input {
  margin-top: 30px;
  font-size: large;
}

    main {
    margin: auto;
    width: 500px;
    overflow: auto;
    padding: 3rem 2rem;
  }

    .post-form {
    padding: 2rem;
    background:rgba(215, 177, 88, 0.548);
  }

    .post-form label {
    text-align: center;
    display: block;
  }

    .post-form input[type='text'] {
    width: 100%;
    padding: 8px;
    margin-bottom: 10px;
    border-radius: 5px;
    color: black;
    border: 1px solid #ccc;
  }

    .btn {
    display: block;
    width: 100%;
    padding: 10px 15px;
    border: 0;
    background:rgba(25, 17, 68, 0.644);
    color: #fff;
    border-radius: 5px;
    margin: 5px 0;
  }

  .btn:hover {
    background: rgba(215, 177, 88, 0.548);
  }

【问题讨论】:

    标签: html css responsive-design responsive viewport


    【解决方案1】:

    我现在看到了你的问题。这是因为您的.post-form 的制作方式。在移动模式下,表单会延伸到视口之外。您有两个解决方案
    1.改变.post-form的行为
    2.调整header的宽度
    对于选项 1,在 style.css:75 for main 中,规则为

    main {
        margin: auto;
        width: 500px;
        overflow: auto;
        padding: 3rem 2rem;
    }
    

    500px 导致了你的问题,你可以摆脱它。如果您的手机宽度小于500px,那么main 将“溢出”到您的视口之外,而您的header 不会这样做。它的宽度保持在视口宽度的 100%。
    对于选项2,您可以制作一个CSS规则,当屏幕小于500px时宽度为500px

    @media only screen and (max-width: 500) {
      header {
        min-width: 500px;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 2013-09-23
      • 2016-03-27
      相关资源
      最近更新 更多