【问题标题】:Why Are There Horizontal Scrollbars on My Responsive Webpage为什么我的响应式网页上有水平滚动条
【发布时间】:2017-11-03 05:04:02
【问题描述】:

我的网站是响应式的,那么为什么会有水平滚动条?我对此感到困惑!水平滚动条消失需要什么?此外,标头在移动设备上不会缩小到 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;
  }

}

【问题讨论】:

    标签: css responsive-design media-queries


    【解决方案1】:

    您有两个导致此问题的问题。

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

    还请注意,虽然媒体查询确实会比常规 CSS 选择器添加更大的特异性(并因此覆盖它),但像这样的 inline width 属性需要尽可能高的特异性。

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

    第二个问题是您在.Main 上硬编码的margin 属性,它在左侧添加了75px 的边距,在右侧添加了125px。当然,这可能适用于大屏幕,但在总共只有 320px 的设备上,你几乎渲染了三分之二的宽度基本上没有用。您应该显着降低这些边距,或者使用 percentage-based values 以便它们适应。

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

    这是一个更新的 sn-p,删除了所有硬编码的 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>

    希望这会有所帮助! :)

    【讨论】:

    • 非常感谢您的帮助!天才!我要试试这个!此外,我在 Screenfly.com 上使用了移动模拟器,智能手机尺寸从 320 像素及以上开始,从 Blackberry 8300 到 iphone 3/4 和三星 Galaxy S2。我什至测试了一个开发者的网站,它在 176 x 220 像素的摩托罗拉 Razr 上响应非常好!我嫉妒天堂!哈哈
    • 没问题!祝您修改顺利;如果这些更改对您有所帮助,请不要忘记mark this answer 作为公认的解决方案。我当然可以理解,有一些很棒的小型设备网站,​​但是这些设备的使用率非常低。这取决于您是否要进行移动优先开发,但我建议仅对 ~400px 以下的任何内容进行微调 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    相关资源
    最近更新 更多