0

I'm trying to center the div{class="informationbox"} so that the div's inside will always be centered in the page, No matter how many divs there is inside.

.informationbox{
  left: 0;
  right: 0;
  margin: 0 auto;
  display:block;
  position: absolute;
}
<div class="informationbox">
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
</div>

2 Answers2

3

Just add text-align center to the informationbox and remove all other css. Note that this ONLY works because (all) your content is inline (your divs have display: inline-block).

.informationbox{
   text-align: center;
}
<div class="informationbox">
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
</div>

WARNING: When you use this solution, you might end up with unwanted space between the inline-block elements. This is due to the fact that there is a 'space' (the character) between them. This can be solved by opening the next div IMMEDIATELY after closing the previous one.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60
1

.informationbox{
  left: 0;
  right: 0;
  margin: 0 auto;
  display:block;
  position: absolute;
  display: flex;
  justify-content: space-around; /*or "center"*/
}
<div class="informationbox">
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
        <div style="display: inline-block;">
            <img src="https://c1.staticflickr.com/1/342/19541848580_7f925817c9_b.jpg" />
            <p>Something</p>
            <p>Wall of Text</p>
        </div>
</div>
ixpl0
  • 748
  • 5
  • 15