1

I'd like to vertically align my anchor tags inside my list, but there is a problem.

The number of list items are variable to the number of pages my website has. So I set a display flex on the list and flex: 1 on the list items. Every list item contains an anchor tag to redirect to that certain page. It looks weird because they are vertically aligned to the top of the list item (while the list items all have the same height to fill up the 100% height of the thanks to flex).

*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html {
  font-family: "Helvetica", sans-serif;
  font-size: 2em;
}
div {
  height: 100vh;
  width: 100vw;
}
ul {
  display: -webkit-flex;
  display: flex;
  flex-flow: column nowrap;
  height: 100%;
  list-style: none;
}
li:first-child {
  background-color: #35CE93;
}
li:nth-child(2) {
  background-color: #3A999F;
}
li:last-child {
  background-color: #59ADEB;
}
li {
  display: block;
  -webkit-flex: 1;
  flex: 1;
  flex-direction: row-reverse;
  width: 100%;
  text-align: center;
}
a {
  color: inherit;
  text-decoration: none;
}
<div>
  <ul>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
  </ul>
</div>

One solution would be to have another span inside the anchor tag and put the anchor tag to a relative position and the span on absolute. But I can't edit the way the list shows up because I use it on mulitple sites that don't need a span or it will bug.

I could force the spans to go inside the list items with jQuery but I rather not do that.

UPDATE adding this will fix it on computers, but not on iPhone:

a {
  color: inherit;
  text-decoration: none;
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
a:before {
  display: inline-block;
  vertical-align: middle;
  height: 100%;
  content: '';
}
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45

1 Answers1

1

Align them vertically using pseudo elements while using inline-block for the anchor tags in your li as below:

a {
  color: inherit;
  text-decoration: none;
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}

a:before {
    display: inline-block;
    vertical-align: middle;
    height: 100%;
    content: '';
}

snippet below:

*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html {
  font-family: "Helvetica", sans-serif;
  font-size: 2em;
}
div {
  height: 100vh;
  width: 100vw;
}
ul {
  display: -webkit-flex;
  display: flex;
  flex-flow: column nowrap;
  height: 100%;
  list-style: none;
}
li:first-child {
  background-color: #35CE93;
}
li:nth-child(2) {
  background-color: #3A999F;
}
li:last-child {
  background-color: #59ADEB;
}
li {
  display: block;
  -webkit-flex: 1;
  flex: 1;
  flex-direction: row-reverse;
  width: 100%;
  text-align: center;
}
a {
  color: inherit;
  text-decoration: none;
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
a:before {
  display: inline-block;
  vertical-align: middle;
  height: 100%;
  content: '';
}
<div>
  <ul>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
  </ul>
</div>

check this out and let me know your feedback. Thanks!

Flexbox solution:

Also check if changing the li in your code as per below will work in iphone to center the a using flexbox techniques:

li {
  display: flex;
  -webkit-flex: 1;
  flex: 1;
  flex-direction: row-reverse;
  width: 100%;
  text-align: center;
  justify-content: center;
  align-items: center;
}

snippet below:

*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html {
  font-family: "Helvetica", sans-serif;
  font-size: 2em;
}
div {
  height: 100vh;
  width: 100vw;
}
ul {
  display: -webkit-flex;
  display: flex;
  flex-flow: column nowrap;
  height: 100%;
  list-style: none;
}
li:first-child {
  background-color: #35CE93;
}
li:nth-child(2) {
  background-color: #3A999F;
}
li:last-child {
  background-color: #59ADEB;
}
li {
  display: flex;
  -webkit-flex: 1;
  flex: 1;
  flex-direction: row-reverse;
  width: 100%;
  text-align: center;
  justify-content: center;
  align-items: center;
}
a {
  color: inherit;
  text-decoration: none;
}
<div>
  <ul>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
    <li>
      <a href="#">test</a>
    </li>
  </ul>
</div>
kukkuz
  • 41,512
  • 6
  • 59
  • 95