Aligning hyperlink with full-height image within a table cell (navigation buttons)

I am currently working on creating a traditional navigation bar that occupies 20% of the screen width and consists of 3 buttons. Each button is supposed to have an icon that is centered both vertically and horizontally. Additionally, I want the buttons to be fully clickable across their entire size. However, here is the issue I am facing: When I set the height of the <a> tag to 100%, the icons within the buttons shift towards the upper border. On the other hand, without setting the height:100%, the button does not take up the full size of the table cell. Below is a snippet of my HTML:

<div id="vertical-navigation">       
     <table class="navigation-table">
        <tbody data-bind="foreach: router.navigationModel">
                <tr>
                  <td class="navigation-item">
                      <a href="#1"><img class="navigation-image" src="http://jsfiddle.net/img/logo.png"/></a>
                  </td>
                </tr>
                                <tr>
                  <td class="navigation-item">
                      <a href="#1"><img class="navigation-image" src="http://jsfiddle.net/img/logo.png"/></a>
                  </td>
                </tr>
                                <tr>
                  <td class="navigation-item">
                      <a href="#1"><img class="navigation-image" src="http://jsfiddle.net/img/logo.png"/></a>
                  </td>
                </tr>
        </tbody>
    </table>
</div>

and this is the corresponding CSS:

    html, body, #vertical-navigation {
    height: 100%;
}

#vertical-navigation
{
    width:19%;
    float:left;
    min-height: 100%;
    border-right: 2px solid #082037;
    background-color: #023a6f;
}

.navigation-table{
    height: 100%;
    width: 100%;

}
.navigation-image{
    display: block;
    margin: 0 auto;
    width:67%;
}
table{
    border:0px; /* border="0" */
    border-collapse:collapse; /* cellspacing="0" */
}
.navigation-button
{
    background:none;
    border:none;
    font-size:1em;
    color:blue;
}
a {
    display:block;

}
.active {
    background-color: #044889;
}

.navigation-item
{
    box-shadow: inset 0px 0px 11px -1px rgba(0,0,0,0.7);

    background-position: center;
    background-repeat: no-repeat;
    background-size: 67%;
}

For a shortened version of this code on jsfiddle, click here. Despite encountering issues with height:100% for <a> on jsfiddle, I am seeking advice on how to resolve this dilemma.

Answer №1

If you're looking to display icons as inline images with vertical alignment, you can achieve this using the following CSS code:

Check out the FIDDLE example here

Custom CSS:

.navigation-image{
    display:inline-block;
    margin: 0 auto;
    width:67%;
    vertical-align:middle;
}
.navigation-item a {
    float:left;
    height:100%;
    width:100%;
    text-align:center;
    
}
.navigation-item a:before { 
    content:"";
    display:inline-block;
    height:100%;
    vertical-align:middle;    
}

** This code snippet utilizes a pseudo element to vertically center the image consistently.

Answer №2

Check this out: http://example.com

I made some adjustments, like removing the image tags and turning the A element into a block with 100% width/height:

By doing this, the link will occupy the entire space and can be activated by clicking anywhere within it.

Then, I used the image from the removed image-tag as a background-image:

a {
    display: block;
    height: 100%;
    width: 100%;    
    box-shadow: inset 0px 0px 11px -1px rgba(0,0,0,0.7);
    background-image: url(http://example.com/img/logo.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: 67%;  
}

Additionally, regarding your question:

"Why isn't height:100% working for A in jsfiddle? Is there another approach I can take?"

The reason height:100% doesn't work is because A is an inline element. To solve this, you can convert it to a block element using "display: block" and then proceed to set the desired width/height along with other block-level properties.

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

Troubleshooting Issues with CSS3PIE and border-radius

Trying to implement CSS3PIE for my website to enable border-radius in IE8 (and older versions). The code works perfectly on all other browsers except IE. Below is the CSS I am using: #body_text_design{ border:2px solid black; background-color:#CCC ...

Upon transitioning between router pages, an error message pops up saying "Vue Socket.io-Extended: this.$socket.$subscribe is not a

I have created a basic Vue application that is designed to connect to a NodeJS server using websockets. My setup involves the use of socket.io-extended for handling the connections. After following the documentation and implementing the websocket connect ...

Unable to execute JS script to navigate back to prior page with webdriver

Here is my code snippet: JavascriptExecutor jse = (JavascriptExecutor)driver; jse.executeScript("window.history.go(-1);"); The code above isn't working. I've tried casting the webdriver instance but it doesn't work every time. I prefer not ...

Display of undefined data in Ajax success response

After receiving an array object data in the Ajax success result, I am trying to print li tags but they are showing as undefined. This is my Ajax code: $.ajax({ 'method': 'GET', 'url': base_url +'party/sel ...

At times, the Ajax response may be lacking in content. However, the response tab in Google

My goal is to retrieve responses from an AJAX call and store them in the global scope for easy access throughout my website. var getOrderStatus = getOrderStatus(), getUserData = getUserData(), orderFormReady = $.when(getOrderStatus, getUserData), ...

The deletion request using the form in Express is experiencing issues and not functioning properly

When attempting to delete data from a database using a form in node.js and express, I am encountering issues with the deletion process. It seems that there are only two methods available - get and post - and no specific delete method. router.js code rout ...

Error in the execution of the if statement within $(window).width() when it is not supposed to be

Hello everyone, I'm facing an issue with the jQuery $(window).width when using if statements. It seems that my function is still running even when the window width is smaller than 991 pixels, although my if statement specifies that it should run only ...

Video upload issue in PHP while images are successfully uploaded

I have a form that successfully uploads images, but it's not working when I try to upload a video. What changes should I make to allow video uploads through this form? $videoform = " <form action='' method='post' enctype=&a ...

Exchange selection choices among harvested selected boxes

In my project, I am working on swapping all the options between two select boxes. The specific scenario I am dealing with involves recording details of phone calls. These calls can either be inbound or outbound. For outbound calls, I have a 'caller&ap ...

Tips for aligning inline elements in the center of a webpage

My goal is to center the title in the middle of the page, taking into account its alignment with the search bar. However, I want it to be centered independently and not affected by the search bar's position. Is there a way to achieve this? To better ...

Is it possible to convert a string of elements in JavaScript into JSON format?

Within my JavaScript code, a variable holds the following information: url=http://localhost quality=100 tag="4.4, 5.5" I am interested in converting this data to JSON format using JavaScript, like this: "result": { "url": "http://localhost", "qu ...

What is the best way to dynamically adjust the select option?

I need help with handling JSON objects: [ { id: "IYQss7JM8LS4lXHV6twn", address: "US", orderStatus: "On the way", }, ]; My goal is to create a select option for the order status. If the current status is "On ...

Error uploading file to Amazon S3: Node.js issue

I encounter a problem while trying to upload a file to Amazon S3. The provided code is: const s3 = require('s3'); const client = s3.createClient({ maxAsyncS3: 100, s3RetryCount: 3, s3RetryDelay: 30 ...

Issues encountered with the delete function using distinct row identification format

I am encountering an issue with variable value in input field. To provide some context, I have client information stored in a MySQL database. Using PHP, I retrieve this data and display it in a table. Currently, I am working on implementing a delete optio ...

implement adding a division element to the DOM using the append

I am facing an issue with this particular code. The intended functionality is to create multiple divs in a loop, but it seems to be dysfunctional at the moment. Upon clicking, only one div appears and subsequent clicks don't trigger any response. < ...

Tips for stopping Infinite Scroll from loading pages when the tab is inactive

I'm currently developing a website using Bootstrap 4 and jQuery, which consists of 3 tabs, each containing masonry elements loaded on scroll with the Infinite Scroll plugin. I'm trying to figure out a way for the Infinite Scroll to only load cont ...

What is the best way to emphasize the current page within my Bootstrap <nav> menu?

Below is the Bootstrap code that defines our main menu navigation: <div class="col-xl-9 col-lg-9"> <div class="main-menu d-none d-lg-block"> <nav> ...

The required widths of table columns are not being adhered to

I'm struggling to create an HTML email that will display correctly in Outlook. I initially used list items and the list-style-image Property, but that doesn't work in Outlook. Essentially, I have a table with 2 rows. The left row has an 11 pixel ...

Using ngTable within an AngularJS application

While working on my angularjs application, I encountered an issue with ngtable during the grunt build process. It seems that the references are missing, resulting in the following error: Uncaught Error: [$injector:modulerr] Failed to instantiate module pa ...

What is the best way to prevent handleSubmit from triggering a re-render when moved to a different

Just started experimenting with React and ran into an issue that I can't seem to find a solution for anywhere. I have a basic search form that interacts with an API. If an invalid value is returned, it displays an H3 element with an error message lik ...