ProcolHarum
ProcolHarum

Reputation: 741

move css box to top of the screen

CSS complete newbie here. I'm trying to push my 'box' to the top of the page. I played with the css file but couldn't figure it out how to place it perfectly. I think the challenge I'm facing is the height and margin. I have copied the css from codepen here.

My objective is to move the box from the bottom to (almost) the top.

#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

Upvotes: 0

Views: 3024

Answers (3)

dale landry
dale landry

Reputation: 8610

So you need to add position: relative to your parent container, then add position absolute to the box-dynamic element. This will allow the element to be pulled out of the normal flow of the document and placed in the document relative to the closest parent with a position of relative. Then you need to position it with the left/right/top and/or bottom properties.

I use top: 60px and left: 20% in the example below.

.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>

Upvotes: 1

Anand Govind C G
Anand Govind C G

Reputation: 52

Give top :0 insted of bottom.For horizontal center give left:50%; and transform:translateX(-50%):

Upvotes: 1

StevenXXCCC
StevenXXCCC

Reputation: 278

Remove "bottom: 0" in your #box-dynamic CSS styles and replace with "left: 40%" (40% could be replaced with your customized value)

Upvotes: 1

Related Questions