0

I can't figure this out, it's suposed to put the boxes in the middle of it's container, but I can't make them move.

The idea is to center the inside the wrapper and to place them horizontally in the middle without having to fuzz around with margins or paddings and using veritcal-align.

#wrapper {
  width: 1000px;
  height: 1000px;
}

#container {
  width: 900px;
  height: 900px;
  text-align: center;
  display: inline-block;
  background-color: lightblue;
}

.box {
  width: 200px;
  height: 200px;
  margin: 0 auto;
  display: inline-block;
  vertical-align: middle;
  background-color: lightgreen;
  border: 1px solid grey;
  margin: 10px;
}
<div id="wrapper">
  <div id="container">
    <div class="box">BOXES</div>
    <div class="box">BOXES</div>
    <div class="box">BOXES</div>
  </div>
</div>
BiP00
  • 29
  • 7

1 Answers1

0

I think you are looking for flexbox.

I have adapted your jsfiddle to fit https://jsfiddle.net/ke4w58ra/

The folowing code is what I have changed to your #content element.

display: flex;
justify-content: center;
align-items: center;
gap: 5px;

Essentially, setting the elements to display in the center horisontally (align-items) and vertically (justify-content). With a gap of 5px to space the boxes out.

For more information, look here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Below is the integrated form of the JSFiddle

#container{
    width: 100vw;
    height: 100vh;
    
    text-align: center;
    display: inline-block;
    background-color: lightblue;

    display: flex;
    justify-content: center;
    align-items: center;

    gap: 5px;
}

.box{
    width: 200px;
    height: 200px;
    
    margin: 0 auto;
    
    background-color: lightgreen;
    border: 1px solid grey;
    margin: 10px;
    
    display: flex;
    align-items: center;
    justify-content: center;
}

body {
  margin: 0px;
}
<body>
    <div id="container">
        <div class="box">BOXES</div>
        <div class="box">BOXES</div>
        <div class="box">BOXES</div>
    </div>
</body>
UnReal G
  • 106
  • 2
  • 8
  • it works, but the question is to see if something is missing with my code to just use vertical align, as it should work. – BiP00 Nov 11 '20 at 03:22
  • 1
    Personally, I would'nt use vertical align as it has a tendancy to break under many circumstances and is non-flexble to other display sizes (fixed vs dynamic content viewing) – UnReal G Nov 11 '20 at 03:29
  • 2
    @BiP00 , the behaviour of [`vertical-align`](https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align) is a little different to what you expect. *Note* Middle : "Aligns the middle of the element with the _baseline_ plus half the x-height of the parent." NOT the middle of the parent. – Jon P Nov 11 '20 at 03:52
  • @JonP thnak you very much, got it now. – BiP00 Nov 11 '20 at 11:33