防止父div中的孩子垂直溢出

时间:2018-03-13 18:47:23

标签: javascript html css

我需要你的帮助。我做了一个天气应用程序,它在桌面上看起来不错但是当我在我的手机上测试时,白盒所持有的内容是垂直出界的。我试过溢出方法,它没有工作,所以将容器高度定义为100%和100vh,它也没有工作?任何解决方案的想法?

这是我部署的应用程序: https://sleepy-badlands-75629.herokuapp.com/#

这是代码:

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" type="text/css "href="../stylesheets/main.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/weather-icons/2.0.9/css/weather-icons.css">
  <script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
</head>
<body>
  <div class="appBox">
    <div class="container">
      <header class="text-center">
        <h1>Local Weather App</h1>
      </header>
      <section class="geolocation">
        <h1>Today's weather in your location:</h1>
        <h2><span id="location"></span></h2>
      </section>
      <section class="temperature">
        <h1>
          <span class="tempDisp"></span>
          <a id="fahrenheit" class="selected" href="#">&#8457;</a> |
          <a id="celsius" href="#">&#8451;</a>
        </h1>
      </section>
      <section class="condition">
        <h1><span class="conDisp"></span></h1>
        <div class="highLow col">
          <p>
            <span class="high"></span>&deg;/
            <span class="low"></span>&deg;
          </p>
        </div>
        <div class="humidity col">
          <p><i class="wi wi-humidity"></i> </p>
        </div>
        <div class="wind col">
          <p>
            <i class="wi wi-small-craft-advisory"></i>
            <span class="direction"></span><span class="speed"></span>
          </p>
        </div>
      </section>
      <section class="forecast">
        <h1>5 Day Forecast</h1>
      </section>
    </div>
  </div>
  <script type="text/javascript" src="../src/app.js"></script>
</body>
</html>

CSS:

@import url('https://fonts.googleapis.com/css?family=Raleway:400,800');

*{
  box-sizing: border-box;
}

body{
  width: 100%;
  height: 100vh;
  /* overflow:hidden; */
  font-family: raleway;
  text-align: center;
  background: no-repeat center center fixed;
  background-size: cover;
  -webkit-background-size: 100%;
    -moz-background-size: 100%;
    -o-background-size: 100%;
    background-size: 100%;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
}

.appBox{
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  bottom: 8px;
  border-radius: 8px 8px 0 8px;
  background: rgba(255,255,255,0.2);
}

.container{
  width: 80%;
  margin: 0 auto;
}

a{
  text-decoration: none;
  transition: all 1s;
  color: black;
}

a:hover{
  color: purple;
}

.selected{
  color: blue;
  cursor: default;
  pointer-events:none;
}


header h1{
  text-align: center;
}

.temperature{
  font-size: 2em;
}

.condition p{
  font-size: 1.3em;
}

.col{
  font-size: 1em;
  display: inline-block;
  width:20%;
  /* margin: 5px; */
  /* padding: 10px; */
  border: 1px solid black;
  border-radius: 10px;
  background: rgba(250,250,250, 0.3);
  -webkit-box-shadow: -2px 7px 27px 1px rgba(0,0,0,0.75);
-moz-box-shadow: -2px 7px 27px 1px rgba(0,0,0,0.75);
box-shadow: -2px 7px 27px 1px rgba(0,0,0,0.75);
}

.forecast{
  width:100%;
  margin: 20px;
}

.forecast h1{
  background: rgba(255,255,255,0.3);
  border: 1px solid white;
  margin-bottom: 0;
}
.fcol{
  display: inline-block;
  width:20%;
  background: rgba(255,255,255,0.3);
  border: 1px solid white;
}

@media(max-width:700px){
  /* .appBox{
    height: 100vh;
  } */
  .col{
    /* display:block; */
    width: 35%;
    margin: 10px auto;
  }
}

2 个答案:

答案 0 :(得分:1)

.forecast课程中,margin: 20px;正在较小的宽屏幕中将forecast元素推离中心。在较大的范围内,它并不那么引人注目,但是一旦你的房地产缩小,20px的碰撞变得更加明显。

我使用开发者控制台将其重新分配给margin: auto;,它似乎可以解决问题。您的元素也应该与此更改保持一致,因为它是嵌套元素。

.forecast {
width: 100%;
margin: auto;
}

应该这样做!

答案 1 :(得分:0)

由于您的问题是内容超出范围,为什么不将内容大小相对或使用css媒体查询。

您可以使用vh设置相对于视口高度的内容大小。

您还可以使用类似@media screen and (max-width: 480px)的内容来确定设备何时小到足以导致内容超出范围,然后在此处为内容编写新的大小。

相关问题