1

I'm trying to make an array of "buttons" with links. It works fine when the text is only one line, but if the text wraps to two lines, the lines get left justified. I'd rather have both lines centered.

.brand-wrap {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 500px;
    height: 100%;
    font-family: Montserrat, verdana, sans-serif;
}
.brand {
    font-size: 14px;
    width: 31%;
    height: 50px;
    background-color: #eee;
    border: solid 3px white;
    display: flex;
    justify-content: center;
    align-items: center;
}
.brand A {
    text-decoration: none;
    color: #6f6f6f;
    line-height: normal;
}
a {
    outline: none;
    border: none;
}
<div class="brand-wrap">
  <div class="brand"><a href="#">Short</a></div>
  <div class="brand"><a href="#">words</a></div>
  <div class="brand"><a href="#">gets centered</a></div>
  <div class="brand"><a href="#">Both horizontally</a></div>
  <div class="brand"><a href="#">and vertically</a></div>
  <div class="brand"><a href="#">But long sentences get left-justified</a></div>
</div>  
Leif Neland
  • 1,416
  • 1
  • 17
  • 40

2 Answers2

1

Try this:

.brand-wrap {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 500px;
  height: 100%;
  font-family: Montserrat, verdana, sans-serif;
}

.brand {
  font-size: 14px;
  width: 31%;
  height: 50px;
  background-color: #eee;
  border: solid 3px white;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}

.brand A {
  text-decoration: none;
  color: #6f6f6f;
  line-height: normal;
}

a {
  outline: none;
  border: none;
}
<div class="brand-wrap">
  <div class="brand"><a href="#">Short</a></div>
  <div class="brand"><a href="#">words</a></div>
  <div class="brand"><a href="#">gets centered</a></div>
  <div class="brand"><a href="#">Both horizontally</a></div>
  <div class="brand"><a href="#">and vertically</a></div>
  <div class="brand"><a href="#">But long sentences <b>DON'T</b> get left-justified</a></div>
</div>

The problem with your code was that because it overflowed, the container expanded to the maximum possible width, making it appear as if the text wasn't center-aligned. Adding a text-align:center fixes that problem.

Endothermic_Dragon
  • 1,147
  • 3
  • 13
-1

You can try adding justify-content: center; or align-items: center; on your flexbox container!

FelixACR
  • 89
  • 2