How can I center a <a> vertically within a list that has varying heights?

Help Needed: Alignment Issue with Anchor Tags in List

I am facing a challenge with vertically aligning the anchor tags inside my list. The number of list items varies based on the pages of my website, so I have used display flex on the list and flex: 1 on the list items. However, the anchor tags within the list items are aligned to the top, giving an awkward look due to the uniform height set by 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>

I have considered options like adding a span inside the anchor tag or using jQuery, but they seem complex and may cause issues on other sites where this code is applied.

UPDATE I tried fixing the alignment issue with the following CSS, but it does not work on iPhones:

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: '';
}

Answer №1

Position the items vertically by using pseudo elements and setting the anchor tags in your list items to display as inline-block:

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: '';
}

Check out the code snippet below for more details:

*,
*: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>

Feel free to review this solution and provide feedback. Thank you!

Flexbox Approach:

You may also try changing the styling of your list items in your code as shown below to center the anchor elements using flexbox techniques, especially for iPhone devices:

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

Take a look at the updated 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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with hashtag in regular expressions

Looking for a way to identify and link hashtag words with an anchor tag? Here's a snippet showcasing the concept: <p class="display"></p> var content = "I enjoy #blueSky. My favorite color is #green. #sky is amazing"; ...

What is the best way to initiate a collapsed jQuery toggle?

Is there a way to make a jQuery toggle collapsed by default? I've looked at some examples, but none of them seemed to fit my specific setup and I couldn't quite figure them out. Here's the HTML code: <table border="0" width="100%" alig ...

Trouble with Bootstrap 5 Dropdown Menu failing to drop down

I am attempting to utilize a dropdown menu on my Wordpress site with Bootstrap, but it is not working as expected. If I manually add the class "show" to my dropdown-menu div, the menu displays, but the button does not function. These are the scripts being ...

Choosing particular contenteditable divisions using jQuery

Consider the following HTML structure for a specific type of blog post editor: <div class="entry"> <div class="title" contenteditable="true"> <h2>Title goes here</h2> </div> <div class="content" contenteditable ...

Unresponsive JavaScript on specific element IDs

I'm experimenting with making three lines perform different animations: line 1 rotating 45deg and translating, line 2 becoming opaque, and line 3 rotating -45deg and translating. Check out my JS Fiddle here <a href="#"><div id="menu" onclic ...

Enlarge the input field when it is selected

Is there a way to expand a search box towards the left instead of the right when focused? The code provided below expands the search box to the right. HTML: <input type="text" placeholder="search"> CSS: input[type="text"] { background: #444; ...

Partially filling circles for a personalized coin-slider experience

Looking to create a unique coin-slider that changes the filled factor of a coin as it moves through a specific range of images. For example, when scrolling through 10 images, the filled part of the coin would transition from 0 to 1 depending on which image ...

Retrieve the value of a variable by using either an HTTP GET or POST request

Here's the HTML code snippet that I'm working with: <head> <h1>Sample Page</h1> </head> <body> <form method="POST" action=""> Enter Keyword <input type="text" name="key"> ...

Display or conceal elements based on the screen size using Bootstrap 3 classes

While this may seem straightforward for some, the reality is quite different. I have two images that need to be displayed based on screen size. In my HTML code, I used classes like hidden-md, hidden-sm, and hidden-xs assuming they would only show on larg ...

Ways to display a specific section on an HTML page using AngularJS

Main page content <div class="row"> <div class="col-md-3"> link 1 link 2 link 3 . . . </div> </div> <div class="col-md-9"> <div ng-view></div> </div& ...

Accordion-style elements arranged in a grid format, smoothly displacing surrounding content

I am facing an issue with a grid of accordions, as demonstrated in the codesandbox linked below. The problem arises when I open one accordion, causing all the accordions in the row below to shift down. Ideally, only the accordion directly below should be p ...

Separation between dropdown menu subcategories

I have searched through various discussions but haven't found a solution that addresses my specific issue, especially at a beginner level. I would really appreciate some guidance. The problem I'm facing involves my drop-down menu displaying gaps ...

Developing web applications using Express.js and Jade involves connecting CSS stylesheets

I'm currently in the process of developing a web application using Express.js along with some Bootstrap templates. I utilized jade2html for conversion, and while the page loads successfully, it lacks any styling. The following code snippet is what I ...

Modifying the color of a specific div using jQuery

I am attempting to develop a function that changes the background color of a div when a user clicks on it and then clicks on a button. The value needs to be saved as a variable, but I'm having trouble getting it to work because it keeps saying that th ...

Modifying Label text dynamically using jQuery on Click event

My website HTML contains the following code snippet: <script src="js/jquery.flexslider.js"></script> <script src="js/jquery.rings.js"></script> The contents of jquery.rings.js are as follows: $('input[type="image"]') ...

Pass data from the view to the controller in CodeIgniter using an array

Take a look at: user_data received from the controller (Database middleware values) <?php $ID['IDS'] = array_column($user_data, 'ID'); print_r($ID); // print_r($ID)=Array( [IDS] => Array([0] => 0 [1] => ABC ...

Achieving perfect alignment in a CSS grid for form elements vertically

One of my current projects involves creating a form layout using HTML and CSS that follows a Material Design style: <div class="form-group"> <input type="text" id="feedName" name="name" value={nameValue} onChange={this.handl ...

What is the most efficient way to evenly separate a CSS3 gradient?

Is there anyone who can assist me in achieving an even gradient break? body { height: 2500px; background: -webkit-linear-gradient(#000000 1%,#FFFFFF 1%,#FFFFFF 13%,#2ecc71 13%,#2ecc71 30%,#3498db 30%,#000000,#FFFFFF,#34495e); } I've been att ...

Modifying HTML text with JavaScript according to the content of the currently selected div

Greetings! This is my first time posting a question, and I wanted to mention that I am relatively new to javascript/jquery. I have a gallery that displays image details on hover through text, while clicking on the image triggers a javascript function that ...

A step-by-step guide on accessing and displaying local Storage JSON data in the index.html file of an Angular project, showcasing it on the

I am facing an issue with reading JSON data from localStorage in my Angular index.html file and displaying it when viewing the page source. Below is the code I have attempted: Please note that when checking the View page source, only plain HTML is being ...