Ron
Ron

Reputation: 105

"margin-top" not working in css

In my webpage I want to keep a fixed distance between the clock and the text.

This is how I would like it to look:

enter image description here

When I reduce the size of the tab (webpage width-wise) this is what happens:

enter image description here

I would like the same to work on larger screens, but currently this works on larger screens.

Under 850px I'm using a media query which is working fine but if there's a more efficient way to do this please let me know.

main.html

    <div id = "main">
      <div id="clock"></div>
      <div id = "text">
      <div id = "greetDIV"><h1 id = "greet">Hello,''.</h1></div>

      <div id = "quoteDIV">
        <h2 id = "quote">quote</h2>
        <h2 id = "by" style="display:none;">- <a href = "" id = "byLink">by</a></h2>
      </div>
    </div>
  </div>

main.css

#quoteDIV{
  margin-top: 5%;
}


#quote{
  text-align: center;
  color:white;
  /*opacity: 0.9;*/
  font-family: 'Titillium Web', sans-serif;
  font-size: 21px;
}
#by{
  text-align: center;
  color: white;
  /*opacity: 0.9;*/
  font-family: 'Titillium Web', sans-serif;
  font-size: 21px;
  margin-top:-13px;
}

#greet{
  color: white;
  text-align: center;
  font-size: 60px;
  margin-top: 32%; /*PROBLEM!*/
  /*opacity: 0.9;*/
  font-family: 'Titillium Web', sans-serif;
}

#clock {
  width: 5em;
  height: 1.3em;
  border-radius: 1em;
  position: absolute;
  left: 0; right: 0; top: 0; bottom: 0;
  margin: auto;
  font-family: 'Titillium Web', sans-serif;
  text-align: center;
  font-size: 118px;
  color: white;
  /*opacity: 0.95;*/
  line-height: 1.2em;
  background: transparent;
  vertical-align: middle;
}

@media only screen and (min-width: 700px) and (max-width: 1000px){
  #clock{
    font-size: 100px;
  }
}

@media only screen and (min-width: 500px) and (max-width: 699px){
   #clock{
    font-size: 80px;
  }
}

@media only screen and (min-width: 200px) and (max-width: 499px){
   #clock{
    font-size: 50px;
  }
}

@media screen and (max-width: 850px) {
  #greet{
    font-size: 33px;
    margin-top: 380px;
  }
  #quoteDIV{
    margin-top: 40px;
  }
  /*.content
  {

    margin-left: 60px;
    float: center;
  }*/
}

Upvotes: 0

Views: 533

Answers (1)

Noah
Noah

Reputation: 1429

https://developer.mozilla.org/en-US/docs/Web/CSS/margin-top

Margin-top (as a percentage) is calculated in terms of the container's width. So, if you resize your screen width-wise, the margin will change.

If a fixed pixel amount isn't possible, try expressing your margin in terms of the viewport height (vh) or the font-size (em/rem).

Upvotes: 1

Related Questions