How can I margin-top a div?

Asked

Viewed 189 times

0

Well, the thing is, I have several Ivs, which are contained in a div of id="container". Some, I want them to be higher up than others, but I wanted to make margin-top, of all of them for one point, and not for others. That is, whenever I margin-top, it is relative to the container div.

I don’t know if it’s possible to do that, if that’s how I can do it?

Thank you.

  • I don’t quite understand your doubt, is you referring to this problem http://answall.com/q/189800/3635 ?

1 answer

1


The best thing would be to get the internal Ivs into position absolute:

#container {
  position: relative; /* importante */
}
div.inner {
  width: 100px;
  height: 50px;
  position: absolute;
}
div.inner:nth-child(even) {
  background-color: blue;
}
div.inner:nth-child(odd) {
  background-color: green;
}
div.inner:nth-child(1) {
  margin-top: 0;
}
div.inner:nth-child(2) {
  margin-top: 10px;
}
div.inner:nth-child(3) {
  margin-top: 20px;
}
div.inner:nth-child(4) {
  margin-top: 30px;
}
div.inner:nth-child(5) {
  margin-top: 40px;
}
<div id="container">
  <div class="inner">div1</div>
  <div class="inner">div2</div>
  <div class="inner">div3</div>
  <div class="inner">div4</div>
  <div class="inner">div5</div>
</div>
  

  • Thanks Miguel, can you just explain to me what Nth-Child is?

  • In this case it’s the same as saying, I want the first/second/third... div with the class inner @Gonçalo

  • All Ivs need to have the same class in order to work?

  • Of course not @Gonçalo, that was just to make the example easier, but if you want to style one by one with css you have to find a way to select each one, by id, class as long as you can select them so much etc....

  • So the important aspects to make it work are just having the relative, in the main div and Absolute, in the ones that are inside right? I’m sorry about that, but I’m learning css eheh

  • Exactly @Gonçalo, it’s not a gift. Are you Portuguese? hehe

  • Yap am ahaha, this css, and the responsibilities shuffles me all lol

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.