Align the icon and text both vertically and horizontally in the center

I have a collection of links:

    <ul>
        <li><a href="#" class="icon26 icon1">Link 1</a></li>
        <li><a class="icon26 icon2">Link 2</a></li>
    </ul>

Each link comes with an icon. My goal is to accomplish two things: 1) Center both the image and text within the LI tag 2) Fill up the A tag so it covers the entire LI tag

This is what I'm attempting to do:

I can achieve either 1 or 2 separately, but not both simultaneously - any suggestions?

Currently, I'm using an existing sprite solution for the link - this approach may not be optimal, but can be adjusted if a solution is found. Here's the CSS snippet:

.icon26:before
{
    background:url(/assets/images/sprite26.png) no-repeat 0 0;
    content:"";
    display:block;
    float:left;
    height:2.6rem;
    width:2.6rem;
}

.icon1:before
{
    background-position:-32px -2px;
}

And here's the rest of the CSS for the UL structure:

#nav ul
{
    width:100%;
}

#nav ul li
{
    float:left;
    width:33.33%;
}

#nav a
{
    display:block;
    line-height:4.6rem;
}

#nav a:before
{
    margin:1rem 0.5rem 1rem 0;
}

Answer №1

Yes, it is entirely achievable. Simply remove the float property and opt for a combination of display: inline-block along with vertical-align: middle.

Check out the fiddle here.

Take a look at the relevant code snippet:

.icon26:before {
    background: url(/assets/images/sprite26.png) no-repeat 0 0;
    content: "";
    display: inline-block; /* Instead of using float: left */
    vertical-align: middle; /* Align inline blocks vertically */
    height: 2.6rem;
    width: 2.6rem;
}

#nav ul li {
    float: left;
    width: 33.33%;
    text-align: center; /* Center inline items horizontally */
}

Answer №2

To achieve the desired result, set the CSS property for the <A> element to be

position: absolute; top: 0; left: 0;
. Ensure that both the icon and link are center-aligned just like you have already done. Following these steps should produce the desired outcome.

Answer №3

To vertically center align content, use the display: table-cell; property and set width: 1% for full-width menu items.

.nav {
  list-style: none;
  padding-left: 0;
}
.nav > li {
  display: table-cell;
  vertical-align: middle;
  width: 1%;
}
.nav > li > a {
  display: block;
  padding: 10px;
  text-align: center;
}

Check out an example here: http://codepen.io/neilhem/pen/ramzXG

Please note that this method may have issues in Safari 8.0

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

Looking to retrieve HTML elements based on their inner text with queryselectors?

I am looking to extract all the HTML divs that contain specific HTML elements with innerText = ' * ' and save them in an array using Typescript. If I come across a span element with the innerText= ' * ', I want to add the parent div to ...

Hover effect on Angular Material Stepper

Is there a way to apply CSS styles to a specific step (Angular material stepper) when hovering over it? I have attempted to set the styles on the elements .mat-step-header and mat-step, as well as applying a custom CSS class, but so far nothing has seeme ...

What steps should I take to customize the design of my Stripe subscription payment page?

I am having trouble with the alignment of the subscription payment page, which is currently displaying all information in a single line. I would like to format it like a traditional payment form with card number, CVV, and expiry on separate lines rather th ...

Positioned in the middle is a collection of left-aligned divs

While many questions address similar issues, the focus is always on having all items centered (accomplished by using display:inline-block). However, my requirement is to have the last line of items floated neatly left and only center the container itself ( ...

Explore dual functionality options for a button in Ionic 3

I'm currently developing an app using the Ionic 3 framework. It's a simple calculator app that requires two input fields and a button for calculation. Upon pressing the button, the variables will be computed. Below is my HTML code snippet: <i ...

An innovative tool for discovering how different colors will complement each other

Design may not be my strong suit, but there are times when I need to add a border line or a shade of gray to a background color. Is there a tool available where I can specify background color: #343434 color: #asdfgh and visually see how it will look wit ...

Using d3.js to dynamically change the color of svg elements based on their data values

I was searching for a way to dynamically color SVG rectangles based on values from a dataset. If I were to create rectangles for each data entry, how could I adjust the rectangle's color according to the data value? Here is what I currently have: // ...

Switch button while moving cursor

I'm struggling with getting this 2048x512 image, which has 4 stages of transition, to work properly. https://i.sstatic.net/1PBvX.png While I know how to switch it to the final stage on hover, I can't seem to figure out how to incorporate a trans ...

Challenge with CSS3 Selectors

I'm struggling to understand why this code works: input[ type = "text" ]:last-of-type:focus{ border:1px solid red; } but this code doesn't work: input[ type = "checkbox" ]:last-of-type:checked{ border:1px solid red; } The example given with t ...

Using jquery to update the overall quantity of likes

Utilizing jquery, html, and php, I have successfully implemented a like button on my page. However, I am currently facing an issue with displaying the total number of likes without refreshing the page. Upon clicking the like button, the jquery and php scr ...

Dispatching correspondence with attached documents and photographs

Is there a way to send an email with an HTML file chosen by the user using (Fileupload), which contains images that I want to embed in the body? I am looking for a method to directly embed images in the body of the email. ...

Is it advisable to adjust the type of input control according to the drop down choice made?

I am currently working on creating an edit screen for users to update rows of data. One of the fields is a dropdown menu, while another field is an input labeled 'value'. Depending on the selection in the dropdown menu, I need to display differen ...

Tips for guaranteeing that HTML elements maintain distinct ids when duplicated within a design

Looking for a solution to ensure unique IDs in a piece of HTML code within a DTL for loop. {% for job in work_experience %} <div class="work-exp-item"> <h4 id="exp-title" class="body-text">{{ job.title }}</h4> <h5 id="exp-comp ...

Attribution of image in footer

The image above the footer is not displaying properly. Screenshot: Image appears below the footer https://i.stack.imgur.com/RvSqI.png Source Code .footer-area { background-position: center; position: relative; z-index: 5; } .footer-area::before { ...

Adjust Title Padding Specifically for Mobile Devices

When viewing my blog page on mobile, I noticed that the titles of my blog posts are overlapping with the teasers. To fix this issue, I attempted to add margins to the bottom of the post titles specifically for mobile: https://i.stack.imgur.com/BHi3C.png H ...

What is the best method for incorporating an Angular Component within a CSS grid layout?

I recently started learning Angular and am currently working on a project that requires the use of a CSS grid layout. However, I'm facing an issue with inserting a component inside a grid area specified by grid-area. My attempt to achieve this in app ...

Determining if a component is nested within itself in Angular 6 is a crucial task

My goal is to develop a custom Angular component for a nested navigation menu with multiple levels. Below is an example of how the menu structure looks: app.component.html <nav-menu> <nav-menu-item>Section 1</nav-menu-item> <nav- ...

The function attached to the `click` event of `#cart-continue` is not invoking

I'm currently working on implementing a navigation to a specific anchor tag when the user clicks the continue button on the cart page. This project involves a mobile application built with Cordova, and the function call is done using jQuery. Here is ...

Ways to stop custom Mailchimp form from redirecting upon submission

I have integrated a custom Mailchimp signup form into a webpage I am developing. The form works well, but I would prefer to avoid the automatic redirect to a new tab upon submission, which displays a confirmation message from Mailchimp about joining the ma ...

Change the destination of an iFrame upon user click

Is it possible to redirect an iFrame to a different HTML page when clicked by the user? I have an iFrame that is essentially an HTML page. When I click on the iFrame, I want it to take me to another HTML page. This is my code: h1 { ...