2

I made a simple example to test this out. I have one wrapper div with two other div elements inside it set to display: inline-block;. The two inner div elements fall on the same line, but how do I get them centered on the half of the main div they belong to? For example, the blue box in the middle of the left side of the main div and the red box in the middle of the right side of the main div. Code and screenshot below.

Also, the inspector shows a width of 204px for the main-box div and even when I set padding and margin to 0 there's still a gap on the bottom between the blue/red boxes and the border of the main-box. How do I get rid of the gap?

.box-test {
  height: 200px;
  display: inline-block;
  width: 30%;
  box-sizing: border-box;
}

#blue {
  background-color: blue;
}

#red {
  background-color: red;
}

#main-box {
  text-align: center;
  border: 1px solid black;
}
<div id="main-box">
  <div id="blue" class="box-test"></div>
  <div id="red" class="box-test"></div>
</div>
David Thomas
  • 249,100
  • 51
  • 377
  • 410
jpanknin
  • 115
  • 2
  • 3
  • 11
  • possible duplicate of : https://stackoverflow.com/questions/17905827/why-does-my-image-have-space-underneath (for the vertical space) – Temani Afif Sep 01 '18 at 19:32
  • possible duplicate of : https://stackoverflow.com/questions/5078239/how-do-i-remove-the-space-between-inline-block-elements (for the horizontal space) – Temani Afif Sep 01 '18 at 19:33

4 Answers4

4

Use flexbox and margin:auto on both elements and they will get centred like you want and you will also get rid of all the whitespace issues:

.box-test {
  height: 200px;
  margin:auto;
  width: 30%;
  box-sizing: border-box;
}

#blue {
  background-color: blue;
}

#red {
  background-color: red;
}

#main-box {
  border: 1px solid black;
  display:flex;
}
<div id="main-box">
  <div id="blue" class="box-test"></div>
  <div id="red" class="box-test"></div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
1

What you should use is a flexbox for the wrapper. When defining space-around for the 'horizontal alignment' you will get what you want.

For more details on flexbox see here

* {
  box-sizing: border-box;
}

#main-box {
  border: 1px solid black;
  display: flex;
  justify-content: space-around;
}
.box-test {
  height: 200px;
  width: 30%;
}

#blue {
  background-color: blue;
}

#red {
  background-color: red;
}
<div id="main-box">
  <div id="blue" class="box-test"></div>
  <div id="red" class="box-test"></div>
</div>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

You can use flexbox with property justify-content: space-around on the wrapper.

.box-test {
  height: 200px;
  width: 30%;
}

#blue {
  background-color: blue;
}

#red {
  background-color: red;
}

#main-box {
  display: flex;
  justify-content: space-around;
  border: 1px solid black;
}
<div id="main-box">
  <div id="blue" class="box-test"></div>
  <div id="red" class="box-test"></div>
</div>
Alexandre Annic
  • 9,942
  • 5
  • 36
  • 50
-1
#main-box {
  text-align: center;
  border: 1px solid black;
  font-size:0;
}

Why it is so? Please read this:

https://css-tricks.com/fighting-the-space-between-inline-block-elements/

Removing whitespace between HTML elements when using line breaks

https://jsfiddle.net/evzckd3w/

SpiRT
  • 632
  • 7
  • 14