css - 为什么我的响应式网页上有水平滚动条

标签 css responsive-design media-queries

我的网站响应式运行,那么为什么会有水平滚动条?我对此感到困惑!水平滚动条消失需要什么?此外,标题在移动设备上不会缩小到 320px 标记处,即使在代码编辑器中测试时它会缩小......???

这是我的 CodePen 的链接:https://codepen.io/IDCoder/pen/Xevyqq

这是我的 HTML 代码:

<html>
  <title></title>
  <head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
  <body>
    <div class="Main">
        <div class="Header Box">
          <div id="One"> 
            <img src="https://s1.postimg.org/5ape0obb0f/Ethersonic_Header_Test_a.jpg" width="1000" height="166"/>
          </div>      
        </div>

        <div class = "Second Row">
          <div class="About Us">
          <h1>Technology you can trust</h1>

  <p>Ethersonic Technologies has been servicing Chicago and the surrounding suburbs since April 2008. We are a Certified (VOSB) Veteran Owned Small Business with the Federal Government and a member of the BB​B (Better Business Bureau) with an A+ rating.</p>

  <p>Ethersonic specializes in Computer  diagnosis and repair, Security Camera Installation and repair and Network Installation and Repair</p>
        </div>
        <div class="BBB VOSB">
          <img src="https://s1.postimg.org/284vrnvfsv/BBB_and_VOSB_Logos.jpg" width="200" height="124"/>
        </div>
      </div>
        <div class="Third Row">
          <div id="Computers">
        <img src="https://s1.postimg.org/1dv3jss5yn/asus-reveals-the-rog-g11-gaming-desktop-491165-2.jpg" width="300" height="203"/>
            <p>
              Desktop and Laptop Repair
            </p>
          </div>
          <div id="Security">
          <img src="https://s1.postimg.org/8j0ejlsp7j/Security_Cameras.jpg
" width="300" height="201"/>
            <p>
             Security Camera Systems | <br>
Access Controls Systems |
Intercom Systems 
            </p>
          </div>
          <div id="Networking">
          <img src="https://s1.postimg.org/7ymhfqjm3z/Networking_2.jpg" width="300" height="220"/>
            <p>
             Network Installation |
Low Voltage Cabling 
            </p>
          </div>
      </div>
      <div class="Fourth Row">
        <div class="container">
          <div id="slider">
      <figure>
 <img src="https://s1.postimg.org/7uayt1hjf3/378c87079dddbfe1d78c9f892695626fl-m0xd-w1020_h770_q80.jpg"/>
<img src="https://s1.postimg.org/11px6krrfz/extralarge.jpg"/>
<img src="https://s1.postimg.org/6yehdl959r/gen_Mid.09186160_1.jpg"/>
<img src="https://s1.postimg.org/8tr267l5fz/ISaly9e2xn0r8r1000000000.jpg"/>
<img src="https://s1.postimg.org/11px6krz5r/ISinl3p5sts48e1000000000_1.jpg"/>
</figure>        
      </div>
        </div>
        <div>
      <div class ="Slideshow Description">
        <p>
          We are Installation Experts! <br>
Offering Customized Installation for Both Residentials and Commercial Properties
        </p>
      </div>
    </div>
   </div>
      <div class="Footer">
         <div>
 © Ethersonic Technologies LLC. All rights reserved.
        </div
   </div>
  </body>
</html>

这是我的 CSS 代码:

.Main{
  display: grid;
  grid-template-columns: 1/1r;
  /*background-color: green;*/
  margin: 5px 125px 0 75px;
  grid-gap: 25px;
} 


.Header.Box {
  display: grid;
    grid-template-columns: 100%;
  /*background-color: red;*/
  grid-gap: 15px;
}



.About.Us{
  font-size: 15px
}

.BBB.VOSB{
  text-align: right;
  padding-right: 10px;
}

#One, #Two, #Three {
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 5px;  
}

.Second.Row{
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 15px;
}

.Third.Row{
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 15px;
  /*background-color: green;*/
}

#Computers, #Security, #Networking{
   /*border-style: solid;*/
    border-color: black;
  border-width: 1px;
  border-radius: 15px;
  padding: 5px;
  text-align: center;

}


.Fourth.Row{
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 25px;
  /*background-color: blue;*/
}

.container{
  max-width: 500px; 
  /*background-color: blue;*/

}

@keyframes slidy {
0% { left: 0%; }
20% { left: 0%; }
25% { left: -100%; }
45% { left: -100%; }
50% { left: -200%; }
70% { left: -200%; }
75% { left: -300%; }
95% { left: -300%; }
100% { left: -400%; }
}

body { margin: 0; } 
div#slider { overflow: hidden; }
div#slider figure img { width: 20%; float: left; }
div#slider figure { 
  position: relative;
  width: 500%;
  margin: 0;
  left: 0;
  text-align: left;
  font-size: 0;
  animation: 30s slidy infinite; 
}


.Slideshow.Description{
  font-size: 1.25em;
}

.Footer{
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 25px;
  text-align: center;
  padding-bottom: 20px;  
  padding-top: 20px;
  font-size: 17px;
  /*background-color: yellow;*/
}



 @media (min-width: 500px){
.Header.Box{ 
  display: grid;
    grid-template-columns: 1fr;
    } 
}

  #One img{
    width: 100%;
    height: auto;    
  }


@media (min-width: 500px) {
.Second.Row{
  display: grid;
    grid-template-columns: 1fr 1fr;
  }

}

@media (max-width: 320px) {
h1{
  display: grid;
    grid-template-columns: 1fr 1fr;
  font-size: h2;
  }

}

@media (max-width: 320px) {
#One img{
  display: grid;
    grid-template-columns: 1fr;
  width: 200px;
  height: auto;
  }

}


@media (min-width: 500px) {
.Third.Row{
  display: grid;
    grid-template-columns: 1fr 1fr 1fr;
  }

}

@media (min-width: 500px) {
.Fourth.Row{
  display: grid;
    grid-template-columns: 1fr 1fr;
  }

}

最佳答案

您有两个问题导致了这种情况。

第一个问题是specificity .你有很多硬编码widths在您的图像中(例如 <img src="https://s1.postimg.org/5ape0obb0f/Ethersonic_Header_Test_a.jpg" width="1000" height="166" /> )。这是 1000px 的硬编码宽度;远不止 320px 的媒体查询可以处理。相反,我建议使用基于百分比的宽度,例如 100% .

另请注意,虽然媒体查询确实会比常规 CSS 选择器增加更多的特异性(并因此覆盖它),但内联 width像这样的属性具有尽可能高的特异性。

可以通过添加 !important 来覆盖它到您的媒体查询 width声明,但我强烈建议改为删除图像中的固定宽度,并使用新的 CSS 选择器来定位图像。

第二个问题是你的硬编码 margin .Main 上的属性, 它增加了 75px 的边距在左边,和125px在右侧。当然,这可能适用于大屏幕,但在只有 320px 的设备上总的来说,您将几乎三分之二的宽度渲染为基本无用。您应该显着减少这些边距,或者使用 percentage-based values 这样他们就会适应。

另请注意 320px对于标准的移动媒体查询来说太小了;几乎您唯一会接触到的手机就是旧黑莓手机。我强烈建议您查看 a list of media queries 的 CSS 技巧 用于标准设备。大多数平板电脑的标准是 768px , 大多数手机的标准是 576px .

这是一个更新的片段,包含所有硬编码的 widthheight删除了属性,一个简单的 img添加声明以确保它们不会超出其父级的范围:

img {
  max-width: 100%;
  width: 100%;
} 

img {
  max-width: 100%;
  width: 100%;
}

.Main {
  display: grid;
  grid-template-columns: 1/1r;
  /*background-color: green;*/
  margin: 5px 125px 0 75px;
  grid-gap: 25px;
}

.Header.Box {
  display: grid;
  grid-template-columns: 100%;
  /*background-color: red;*/
  grid-gap: 15px;
}

.About.Us {
  font-size: 15px
}

.BBB.VOSB {
  text-align: right;
  padding-right: 10px;
}

#One,
#Two,
#Three {
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 5px;
}

.Second.Row {
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 15px;
}

.Third.Row {
  display: grid;
  grid-template-columns: 100%;
  grid-gap: 15px;
  /*background-color: green;*/
}

#Computers,
#Security,
#Networking {
  /*border-style: solid;*/
  border-color: black;
  border-width: 1px;
  border-radius: 15px;
  padding: 5px;
  text-align: center;
}

.Fourth.Row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 25px;
  /*background-color: blue;*/
}

.container {
  max-width: 500px;
  /*background-color: blue;*/
}

@keyframes slidy {
  0% {
    left: 0%;
  }
  20% {
    left: 0%;
  }
  25% {
    left: -100%;
  }
  45% {
    left: -100%;
  }
  50% {
    left: -200%;
  }
  70% {
    left: -200%;
  }
  75% {
    left: -300%;
  }
  95% {
    left: -300%;
  }
  100% {
    left: -400%;
  }
}

body {
  margin: 0;
}

div#slider {
  overflow: hidden;
}

div#slider figure img {
  width: 20%;
  float: left;
}

div#slider figure {
  position: relative;
  width: 500%;
  margin: 0;
  left: 0;
  text-align: left;
  font-size: 0;
  animation: 30s slidy infinite;
}

.Slideshow.Description {
  font-size: 1.25em;
}

.Footer {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  grid-gap: 25px;
  text-align: center;
  padding-bottom: 20px;
  padding-top: 20px;
  font-size: 17px;
  /*background-color: yellow;*/
}

@media (min-width: 500px) {
  .Header.Box {
    display: grid;
    grid-template-columns: 1fr;
  }
}

#One img {
  width: 100%;
  height: auto;
}

@media (min-width: 500px) {
  .Second.Row {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 320px) {
  h1 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    font-size: h2;
  }
}

@media (max-width: 320px) {
  #One img {
    display: grid;
    grid-template-columns: 1fr;
    width: 200px;
    height: auto;
  }
}

@media (min-width: 500px) {
  .Third.Row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
  }
}

@media (min-width: 500px) {
  .Fourth.Row {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}
<html>
<title></title>

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

<body>
  <div class="Main">
    <div class="Header Box">
      <div id="One">
        <img src="https://s1.postimg.org/5ape0obb0f/Ethersonic_Header_Test_a.jpg" />
      </div>
    </div>

    <div class="Second Row">
      <div class="About Us">
        <h1>Technology you can trust</h1>

        <p>Ethersonic Technologies has been servicing Chicago and the surrounding suburbs since April 2008. We are a Certified (VOSB) Veteran Owned Small Business with the Federal Government and a member of the BB​B (Better Business Bureau) with an A+ rating.</p>

        <p>Ethersonic specializes in Computer diagnosis and repair, Security Camera Installation and repair and Network Installation and Repair</p>
      </div>
      <div class="BBB VOSB">
        <img src="https://s1.postimg.org/284vrnvfsv/BBB_and_VOSB_Logos.jpg" />
      </div>
    </div>
    <div class="Third Row">
      <div id="Computers">
        <img src="https://s1.postimg.org/1dv3jss5yn/asus-reveals-the-rog-g11-gaming-desktop-491165-2.jpg" />
        <p>
          Desktop and Laptop Repair
        </p>
      </div>
      <div id="Security">
        <img src="https://s1.postimg.org/8j0ejlsp7j/Security_Cameras.jpg
" />
        <p>
          Security Camera Systems | <br> Access Controls Systems | Intercom Systems
        </p>
      </div>
      <div id="Networking">
        <img src="https://s1.postimg.org/7ymhfqjm3z/Networking_2.jpg" />
        <p>
          Network Installation | Low Voltage Cabling
        </p>
      </div>
    </div>
    <div class="Fourth Row">
      <div class="container">
        <div id="slider">
          <figure>
            <img src="https://s1.postimg.org/7uayt1hjf3/378c87079dddbfe1d78c9f892695626fl-m0xd-w1020_h770_q80.jpg" />
            <img src="https://s1.postimg.org/11px6krrfz/extralarge.jpg" />
            <img src="https://s1.postimg.org/6yehdl959r/gen_Mid.09186160_1.jpg" />
            <img src="https://s1.postimg.org/8tr267l5fz/ISaly9e2xn0r8r1000000000.jpg" />
            <img src="https://s1.postimg.org/11px6krz5r/ISinl3p5sts48e1000000000_1.jpg" />
          </figure>
        </div>
      </div>
      <div>
        <div class="Slideshow Description">
          <p>
            We are Installation Experts! <br> Offering Customized Installation for Both Residentials and Commercial Properties
          </p>
        </div>
      </div>
    </div>
    <div class="Footer">
      <div>
        © Ethersonic Technologies LLC. All rights reserved.
      </div>
    </div>
</body>

</html>

希望对您有所帮助! :)

关于css - 为什么我的响应式网页上有水平滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47084311/

相关文章:

android - 响应式网站 - 只有 Android 会在加载时立即开始连续放大

python - 无法创建着色器缓存条目 - 在通过其 Css 选择器定位元素时出错

html - 我如何绝对定位一个 div 并使其保持在其原始顶部位置,并且其上方的 dom 元素是 float 的?

html - Bootstrap : how to not have text-right in certain size?

html - 视口(viewport)标签在 Chrome 上不起作用

html - 移动平板电脑的响应式布局

html - 媒体查询、居中视口(viewport)和倾斜边距

ajax - 从浏览器获取原始 CSS 文件内容

javascript - 构建框架/工具时的类命名指南

css - 导航栏的引导媒体查询不起作用