将 css 框移动到屏幕顶部

时间:2021-05-14 17:46:48

标签: html jquery css

CSS 完全新手在这里。我正在尝试将我的“盒子”推到页面顶部。我玩过 css 文件,但不知道如何完美地放置它。我认为我面临的挑战是高度和利润。我已经从 codepen here 复制了 css。

我的目标是将盒子从底部移动到(几乎)顶部。

#box-dynamic {
  width: 420px;
  line-height: 25px;
  background: rgba(0, 0, 0, 0.2);
  float: left;
  position: absolute;
  bottom: 0;
  z-index: 500 !important;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize {
  height: 50px;
  line-height: 5px;
}

#box-dynamic h1 {
  margin-right: 5px;
  color: #fff;
  font-size: 20px;
  font-family: 'Roboto Condensed', sans-serif;
  margin-left: 5px;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize h1 {
  font-size: 20px;
  text-align: center;
}

#box-dynamic p {
  color: #ccc;
  font-size: 15px;
  line-height: 1.4;
  margin-left: 5px;
  font-family: 'Roboto Condensed', sans-serif;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize p {
  font-size: 0px;
}

#box-dynamic small {
  color: #ccc;
  font-size: 11px;
  font-family: 'Roboto Condensed', sans-serif;
  margin-left: 5px;
  margin-top: 0;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize small {
  font-size: 10px;
  margin-right: 5px;
  float: right;
}

#box-dynamic img {
  float: left;
  width: 0;
  height: 0;
  visibility: hidden;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize img {
  float: left;
  width: 50px;
  height: 50px;
  visibility: visible;
}
<div class="box">
  <div id="box-dynamic" onclick=panelView()>
    <h1>This is my title</h1>
    <p>few words about my paragraph so it goes like this...</p>
    <br><br>
  </div>
</div>

enter image description here

3 个答案:

答案 0 :(得分:1)

删除#box-dynamic CSS 样式中的“bottom: 0”并替换为“left: 40%”(40% 可以替换为您的自定义值)

答案 1 :(得分:1)

给顶部:0 插入底部。对于水平中心,给左:50%;和 transform:translateX(-50%):

答案 2 :(得分:1)

因此您需要将 position: relative 添加到您的父容器,然后将 position absolute 添加到 box-dynamic 元素。这将允许将元素从文档的正常流中拉出并放置在文档中 相对 位置为 的最近父元素相对。然后您需要使用 left/right/top 和/或 bottom 属性来定位它。

我在下面的示例中使用了 top: 60pxleft: 20%

.box{
  position: relative; /* added */
  width: 100vw; /* added */
  height: 100vh; /* added */
  min-height: 1080px; /* this is only to mimic a larger background like your map */
}

#box-dynamic {
  width: 420px;
  line-height: 25px;
  background: rgba(0, 0, 0, 0.2);
  position: absolute;
  z-index: 500 !important;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
  position: absolute; /* added */
  top: 60px; /* added */
  left: 20%; /* added */
  /* removed float and bottom */
}

#box-dynamic.minimize {
  height: 50px;
  line-height: 5px;
}

#box-dynamic h1 {
  margin-right: 5px;
  color: #fff;
  font-size: 20px;
  font-family: 'Roboto Condensed', sans-serif;
  margin-left: 5px;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize h1 {
  font-size: 20px;
  text-align: center;
}

#box-dynamic p {
  color: white;
  font-size: 15px;
  line-height: 1.4;
  margin-left: 5px;
  font-family: 'Roboto Condensed', sans-serif;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize p {
  font-size: 0px;
}

#box-dynamic small {
  color: #ccc;
  font-size: 11px;
  font-family: 'Roboto Condensed', sans-serif;
  margin-left: 5px;
  margin-top: 0;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize small {
  font-size: 10px;
  margin-right: 5px;
  float: right;
}

#box-dynamic img {
  float: left;
  width: 0;
  height: 0;
  visibility: hidden;
  -ms-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -webkit-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
}

#box-dynamic.minimize img {
  float: left;
  width: 50px;
  height: 50px;
  visibility: visible;
}
<div class="box">
  <div id="box-dynamic" onclick=panelView()>
    <h1>This is my title</h1>
    <p>few words about my paragraph so it goes like this...</p>
    <br><br>
  </div>
</div>

相关问题