-3

I have six divs in my html document.

My problem is that last div has some top space which needs to be removed.

This is not a duplicate question.

This has nothing to do with inline-block spacing.

Please help.

<!DOCTYPE html>
<html>
<style>
  body {
    border: 1px solid;
    padding: 0;
    margin: auto;
    height: 500px;
    width: 500px;
  }
  div {
    padding: 0;
    margin: 0;
    border: 1px solid;
    display: inline-block;
    width: 25%;
    height: 25%;
    position: relative;
  }
  div:first-child {
    background: black;
  }
</style>

<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div>
  </div>
  <div>
    <div></div>
  </div>
  <script>
    var myDivs = document.querySelectorAll("div");
    myDivs[2].style.backgroundColor = "blue";
  </script>
</body>

</html>
srikanth_k
  • 2,807
  • 3
  • 16
  • 18

5 Answers5

4

This is a problem with inline-block. Use vertical-align: top to fix, as the default is baseline:

<!DOCTYPE html>
<html>
<style>
  body {
    border: 1px solid;
    padding: 0;
    margin: auto;
    height: 500px;
    width: 500px;
  }
  div {
    padding: 0;
    margin: 0;
    border: 1px solid;
    display: inline-block;
    width: 25%;
    height: 25%;
    position: relative;
    vertical-align: top;   /* This is the fix */
  }
  div:first-child {
    background: black;
  }
</style>

<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div>
  </div>
  <div>
    <div></div>
  </div>
  <script>
    var myDivs = document.querySelectorAll("div");
    myDivs[2].style.backgroundColor = "blue";
  </script>
</body>

</html>
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
3

Just a small change in your css:

div {
padding: 0;
margin: 0;
border: 1px solid;
display: inline-block; /* Removed this */
width: 25%;
height: 25%;
position: relative;
float:left; /* Added this */
}
Pranjal
  • 1,088
  • 1
  • 7
  • 18
0

add to body font-size: 0; and add to your div

font-size: 12px;
    box-sizing: border-box;
    vertical-align: top;

about Box Sizing

Demo

<!DOCTYPE html>
<html>
<style>
  body {
    border: 1px solid;
    padding: 0;
    margin: auto;
    height: 500px;
    width: 500px;
font-size: 0;
  }
  div {
    padding: 0;
    margin: 0;
    border: 1px solid;
    display: inline-block;
    width: 25%;
    height: 25%;
    position: relative;
font-size: 12px;
box-sizing: border-box;
vertical-align: top;
  }
  div:first-child {
    background: black;
  }
</style>

<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div>
  </div>
  <div>
    <div></div>
  </div>
  <script>
    var myDivs = document.querySelectorAll("div");
    myDivs[2].style.backgroundColor = "blue";
  </script>
</body>

</html>
Rohit Azad Malik
  • 31,410
  • 17
  • 69
  • 97
0

You can try absolute positioning. Add position: absolute; to div:first-child

<!DOCTYPE html>
<html>
<style>
  body {
    border: 1px solid;
    padding: 0;
    margin: auto;
    height: 500px;
    width: 500px;
  }
  div {
    padding: 0;
    margin: 0;
    border: 1px solid;
    display: inline-block;
    width: 25%;
    height: 25%;
    position: relative;
  }
  div:first-child {
    position: absolute;
    background: black;
  }
</style>

<body>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div>
    <div></div>
  </div>
  <script>
    var myDivs = document.querySelectorAll("div");
    myDivs[2].style.backgroundColor = "blue";
  </script>
</body>

</html>
Pugazh
  • 9,453
  • 5
  • 33
  • 54
0

In html, spaces, tabulations and linebreak (while formatting your html document) are transcribe by a space.

So if you want no space between your div you should put comments between them :

  body {
    border: 1px solid;
    padding: 0;
    margin: auto;
    height: 500px;
    width: 500px;
  }
  div {
    padding: 0;
    margin: 0;
    border: 1px solid;
    display: inline-block;
    width: 25%;
    height: 25%;
    position: relative;
  }
  div:first-child {
    background: black;
  }
<!DOCTYPE html>
<html>

<body>
  <div></div><!--
  --><div></div><!--
  --><div></div><!--
  --><div></div><!--
  --><div>
  </div><!--
  --><div>
    <div></div>
  </div>
  <script>
    var myDivs = document.querySelectorAll("div");
    myDivs[2].style.backgroundColor = "blue";
  </script>
</body>

</html>
Damien
  • 3,322
  • 3
  • 19
  • 29