【问题标题】:Box-sizing and Media Queries not working?盒子大小和媒体查询不起作用?
【发布时间】:2020-09-08 14:16:11
【问题描述】:

我想创建一个响应式页面

  1. 当页面宽度小于 1000 像素时隐藏右侧聊天信息框

  1. 当页面小于 600px 时隐藏右侧聊天框和左侧边栏;

但我无法弄清楚我的代码有什么问题导致媒体查询不起作用。

此外,即使我向其添加“box-sizing:border”框,左侧栏中的搜索框也会溢出其父级。

最后一个问题是我想让框的垂直侧边框与页面高度相同,而不创建侧滚动条(我尝试将盒装高度设置为 100%,这是可以使用的但它也创建了一个侧滚动条,这在本作业中是不允许的)。

请看下面我的代码,非常感谢:

@media screen and (max-width: 1400px;) {
  .groupsettings,
  .search {
    width: 300px;
  }
}
@media screen and (max-width: 1000px;) {
  .groupsettings {
    display: none;
  }
}
@media screen and (max-width: 600px;) {
  .search {
    display: none;
  }
}
.avatar {
  border-radius: 50%;
  height: 25px;
  margin-left: 0;
  margin-right: 10px;
  width: 25px;
}
.chatroom {
  border-right: 1px solid lightgray;
  grid-column: 2/3;
  grid-row: 2/3;
}
.groupprofile {
  border-bottom: 1px solid lightgray;
  padding: 10px;
}
.groupsettings {
  grid-column: 3/4;
  grid-row: 2/3;
  min-width: 300px;
}
.grouptitle {
  grid-column: 2/4;
  grid-row: 1/2;
}
.his {
  background-color: lightgray;
}
.his,
.mine {
  border-bottom-left-radius: 50px;
  border-bottom-right-radius: 50px;
  border-top-left-radius: 50px;
  border-top-right-radius: 50px;
  font-size: 14px;
  width: max-content;
  padding: 5px 10px 7px;
}
.loading {
  color: lightgray;
  font-size: 14px;
  text-align: center;
}
.logo {
  border-radius: 50%;
  height: 40px;
  vertical-align: middle;
  width: 40px;
}
.messenger {
  border-top: 1px solid lightgray;
  display: grid;
  grid-template-columns: 3fr 4fr 3fr;
  grid-template-rows: auto 1fr;
}
.mine {
  align-self: right;
  background-color: #0097f7;
  color: white;
  margin-left: auto;
  margin-right: 10px;
}
.name {
  color: lightgray;
  font-size: 12px;
  margin: 0;
  text-indent: 10%;
}
.normal {
  font-size: 14px;
  margin-left: 5px;
}
.options {
  color: gray;
  font-size: 14px;
  margin-left: 10px;
  margin-top: 10px;
}
.search {
  border-right: 1px solid lightgray;
  grid-column: 1/2;
  grid-row: 1/3;
  min-width: 300px;
}
.searchbox {
  background-color: lightgray;
  border: none;
  border-radius: 2px;
  box-sizing: border-box;
  color: gray;
  font-size: 12px;
  margin: 10px;
  padding-bottom: 5px;
  padding-top: 5px;
  text-align: center;
  width: 100%;
}
.searchbox:focus {
  border: none;
  padding-left: 10px;
  text-align: left;
}
.settings {
  padding: 0px;
}
.steve {
  display: flex;
  align-items: center;
  margin-left: 10px;
}
body {
  font-family: "SFUIText-Regular", "Segoe UI", "Helvetica Neue", Helvetica,
    Arial, sans-serif;
  margin: 5px 0 0;
}
strong {
  border-bottom: 1px solid lightgray;
  display: block;
  text-align: center;
  padding-bottom: 10px;
  padding-top: 10px;
}
<html>
  <head>
    <title>Messenger</title>
  </head>
  <body>
    <div class="messenger">
      <div class="search">
        <strong>Messenger</strong>
        <input class="searchbox" placeholder="Search Messenger" />
      </div>
      <div class="grouptitle"><strong>normal group chat</strong></div>
      <div class="chatroom">
        <p class="mine">Hey man, hope everything's okay!</p>
        <p class="name">Steve</p>
        <div class="steve">
          <img
            class="avatar"
            alt="mine-craft-chara"
            src="https://i.imgur.com/R10B80x.png"
          /><span class="his"
            >All good man, those stacks of diamonds coming in hot!</span
          >
        </div>
        <p class="mine">Nice - make sure to save me some haha :D</p>
      </div>
      <div class="groupsettings">
        <div class="groupprofile">
          <img
            class="logo"
            alt="mine-craft-logo"
            src="https://i.imgur.com/vWJzAsu.jpg"
          />
          <span class="normal">normal group chat</span>
        </div>
        <div class="settings">
          <p class="options">Options</p>
          <p class="loading">Loading...</p>
        </div>
      </div>
    </div>
  </body>
</html>

【问题讨论】:

    标签: html css responsive-design media-queries


    【解决方案1】:

    去掉最大宽度后的分号。

    @media screen and (max-width: 1400px) {
      .groupsettings,
      .search {
        width: 300px;
      }
    }
    @media screen and (max-width: 1000px) {
      .groupsettings {
        display: none;
      }
    }
    @media screen and (max-width: 600px) {
      .search {
        display: none;
      }
    }
    .avatar {
      border-radius: 50%;
      height: 25px;
      margin-left: 0;
      margin-right: 10px;
      width: 25px;
    }
    .chatroom {
      border-right: 1px solid lightgray;
      grid-column: 2/3;
      grid-row: 2/3;
    }
    .groupprofile {
      border-bottom: 1px solid lightgray;
      padding: 10px;
    }
    .groupsettings {
      grid-column: 3/4;
      grid-row: 2/3;
      min-width: 300px;
    }
    .grouptitle {
      grid-column: 2/4;
      grid-row: 1/2;
    }
    .his {
      background-color: lightgray;
    }
    .his,
    .mine {
      border-bottom-left-radius: 50px;
      border-bottom-right-radius: 50px;
      border-top-left-radius: 50px;
      border-top-right-radius: 50px;
      font-size: 14px;
      width: max-content;
      padding: 5px 10px 7px;
    }
    .loading {
      color: lightgray;
      font-size: 14px;
      text-align: center;
    }
    .logo {
      border-radius: 50%;
      height: 40px;
      vertical-align: middle;
      width: 40px;
    }
    .messenger {
      border-top: 1px solid lightgray;
      display: grid;
      grid-template-columns: 3fr 4fr 3fr;
      grid-template-rows: auto 1fr;
    }
    .mine {
      align-self: right;
      background-color: #0097f7;
      color: white;
      margin-left: auto;
      margin-right: 10px;
    }
    .name {
      color: lightgray;
      font-size: 12px;
      margin: 0;
      text-indent: 10%;
    }
    .normal {
      font-size: 14px;
      margin-left: 5px;
    }
    .options {
      color: gray;
      font-size: 14px;
      margin-left: 10px;
      margin-top: 10px;
    }
    .search {
      border-right: 1px solid lightgray;
      grid-column: 1/2;
      grid-row: 1/3;
      min-width: 300px;
    }
    
    .searchbox-container {
      padding: 10px;
      box-sizing: border-box;
      width: 100%;
    }
      
    .searchbox {
      background-color: lightgray;
      border: none;
      border-radius: 2px;
      box-sizing: border-box;
      color: gray;
      font-size: 12px;
      padding-bottom: 5px;
      padding-top: 5px;
      text-align: center;
      width: 100%;
    }
    .searchbox:focus {
      border: none;
      padding-left: 10px;
      text-align: left;
    }
    .settings {
      padding: 0px;
    }
    .steve {
      display: flex;
      align-items: center;
      margin-left: 10px;
    }
    body {
      font-family: "SFUIText-Regular", "Segoe UI", "Helvetica Neue", Helvetica,
        Arial, sans-serif;
      margin: 5px 0 0;
    }
    strong {
      border-bottom: 1px solid lightgray;
      display: block;
      text-align: center;
      padding-bottom: 10px;
      padding-top: 10px;
    }
    <html>
      <head>
        <title>Messenger</title>
      </head>
      <body>
        <div class="messenger">
          <div class="search">
            <strong>Messenger</strong>
            <div class='searchbox-container'>
              <input class="searchbox" placeholder="Search Messenger" />
            </div>
          </div>
          <div class="grouptitle"><strong>normal group chat</strong></div>
          <div class="chatroom">
            <p class="mine">Hey man, hope everything's okay!</p>
            <p class="name">Steve</p>
            <div class="steve">
              <img
                class="avatar"
                alt="mine-craft-chara"
                src="https://i.imgur.com/R10B80x.png"
              /><span class="his"
                >All good man, those stacks of diamonds coming in hot!</span
              >
            </div>
            <p class="mine">Nice - make sure to save me some haha :D</p>
          </div>
          <div class="groupsettings">
            <div class="groupprofile">
              <img
                class="logo"
                alt="mine-craft-logo"
                src="https://i.imgur.com/vWJzAsu.jpg"
              />
              <span class="normal">normal group chat</span>
            </div>
            <div class="settings">
              <p class="options">Options</p>
              <p class="loading">Loading...</p>
            </div>
          </div>
        </div>
      </body>
    </html>

    【讨论】:

    • 非常感谢!这适用于媒体查询问题!但是我应该如何解决左侧边栏中的搜索框溢出其父项?另外我注意到盒子之间的底部边框之间形成了间隙,我认为这也属于与上述相同的问题,我应该如何解决这个问题?
    • 修复了搜索框的问题。您必须删除搜索框中的margin: 10px。如果它不必正好是 10px,您可以使用 width: 90% 而不是 100%,或者另一种方法是使用带有 padding: 10px 的父容器。我不明白你所说的边界是什么意思。
    • 太棒了,我在搜索框中添加了一个 div,它成功了!对于边界问题,我将修改我的问题并上传几张照片。再次感谢!
    【解决方案2】:

    你应该把它加到你的脑海里

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

    【讨论】:

    • 谢谢,我添加了这一行,它有什么作用?
    • 它基本上告诉浏览器该网站应该是一个响应式网站
    • 酷,记笔记。谢谢!
    猜你喜欢
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多