-1

As the title states, I am not able to center my content vertically. When I inspect the divs, I have this bit of empty space below my divs and I don't know where that is coming from. Perhaps that's the problem, since when I go into the dev tool styles and put in <line-height: 3> it centers. I don't wish to hardcode things like this, and flex normally works, but I'm totally stumped here.

If you need to see anything else, please let me know!

.table-page-nav-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
 }

.arrow-btn {
  text-align: center;
  width: 50px;
  height: 50px;
  border-radius: 4px;
  box-shadow: 0 5px 5px rgba(0,0,0,0.2);
}

.page-index-inner {
  text-align: center;
  width: 244px;
  height: 50px;
  border-radius: 4px;
  box-shadow: 0 5px 5px rgba(0,0,0,0.2);
  margin: 0px 20px;
}
<div class="table-page-nav-wrapper">
    <div class="arrow-btn"><</div>
    <div class="page-index-inner">Why won't I center correctly?</div>
    <div class="arrow-btn">></div>
</div>

enter image description here

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
LovelyAndy
  • 841
  • 8
  • 22

2 Answers2

2

It is centered correctly. The outer div is centered vertically. What you want to achieve is centering the contents of the div vertically.

So either you make the inner div a flex container itself by adding display: flex to it and then vertically centering the contents with align-items: center or you could just give the inner div a line-height of 50px since the height of it already hard-coded. This will only work with a text without line-breaks.

Dom
  • 734
  • 3
  • 8
1

As @DOM explained the code is technical correct.

The common missunderstanding is, that flexbox works on the immediate inner elements of the wrapping flexbox-container but not on the childs or the content of this inner elements.

As centered inner grids are typical css tasks: additional to @DOM's solution here is a general css helper class I use and which does the trick. Just add it to div.table-page-nav-wrapper:

.center_HV,
.center_HV > div {
    display: flex;
    justify-content: center;
    align-items: center;                
}

That works on line breaks too.

Brebber
  • 2,986
  • 2
  • 9
  • 19