There seems to be a vertical centering issue with the image - there is more whitespace above it

This example demonstrates an issue with centering images inside an anchor tag (within a div). I find it perplexing - why does the first image have more space above it than below? Can anyone assist me in resolving this?

Here is the HTML code:

<div id="parent">
    <ul class="other-images">
        <li>
            <a href="#">
                <img src="http://www.abc.net.au/radionational/image/4220698-3x2-700x467.jpg">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="http://us.123rf.com/400wm/400/400/malopes/malopes0901/malopes090100089/4185805-beatiful-spring-landscape-with-grass-and-sky--portrait-orientation.jpg">
            </a>
        </li>
        <li>
            <a href="#">
                <img src="http://resources2.news.com.au/images/2012/09/09/1226468/512770-images.jpg">
            </a>
        </li>
    </ul>
</div>

And here is the CSS code:

#parent{
    background: gray;
    width: 500px;
    height: 500px;
    padding: 10px;
}

.other-images{
    display: block;
    padding: 0;
    margin: 0 -2%;
    overflow: hidden;
}

.other-images > li:nth-of-type(8n+1) {
    clear: both;
}

.other-images > li:nth-of-type(n) {
    clear: none;
}

.other-images > li {
    display: inline-block;
    height: 35px;
    float: left;
    margin: 0 2% 4%;
    width: 9%;
}

.other-images > li > a {
    background: red;
    display: inline-block;
    border: 1px solid #aaa;
    height: 100%;
    width: 100%;
    line-height: 35px;
    text-align: center;
}

img{
    max-width:100%;
    max-height:100%;
    display: inline-block;
    vertical-align: middle;
}

Answer №1

When it comes to aligning items vertically, there are several solutions available. One option is the approach you've taken, setting a line-height for a div and using vertical-align:middle for an image within it.
Another technique involves setting the anchor with display:table and the image with display:table-cell, both using vertical-align middle.

For more useful information on this topic, check out this resource

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

Restrict the Three.js Raycaster to a designated section within the HTML without using offsets

As I dive into using Three.js and the Raycaster for object picking, my HTML structure consists of a Navigation Bar in the head section and the rendering area in the body section. However, I encountered an issue where there was an offset in the Raycaster d ...

Interactive HTML and CSS tree structure viewing experience

I am looking to create a responsive tree diagram, here is a basic example: https://jsfiddle.net/ppnfpggx/2/ However, I am facing some challenges with the responsive layout. Particularly on smaller devices, it should appear like this:https://i.sstatic.net/ ...

Reposition the CSS content

In order to achieve the desired effect of having the text in the middle of the div both vertically and horizontally, you can try using the following code: .servicecircle { width: 204px; height: 204px; background-image: url('secret.png&a ...

PHP - Unable to store the input content

I've encountered an issue with my website where I need to create a form for users to submit new information or news. Below is the code snippet that I have: <?php include "0begin.php"; $title=$_POST["title"]; isset($title) or $title=$_GET["tit ...

Float from the bottom and then rise up

I'm attempting to use CSS to make code from further down in the HTML appear above code from a section above it. Here is how my HTML structure looks: <div class="showbelow"> show below </div> <div class="showabove"> show above < ...

issues with the styling and scripts within the jsp document

During my project work, I encountered an issue with the front end as shown in the image below. https://i.sstatic.net/gLIUr.png Upon loading the project, all styles and scripts disappear completely. How can I resolve this issue promptly? I attempted to up ...

Experiencing trouble with aligning input fields in a Bootstrap layout

When working on a user interface design, I wanted to have different containers with various input type elements. However, I encountered an issue where the datetime picker in one row was not aligning properly with labels above and below it. <div class=" ...

Detecting Triple Taps on Mobile Devices

I've been trying to incorporate a triple-tap escape feature similar to The Trevor Project's Website. It functions flawlessly on laptops and desktops with a mouse. However, I'm struggling to detect the triple tap on mobile browsers because th ...

Vertical Dragging Division of Space

I am looking to create a draggable division that separates two areas vertically, similar to the images below: https://i.sstatic.net/AC9Gt.pnghttps://i.sstatic.net/xA4cX.png I found an online example of draggable divs and customized it to suit my needs. I ...

Styling <Link> component with styled-components: A step-by-step guide

Utilizing the Link component from @material-ui/core/Link in my TypeScript code was initially successful: <Link href="#" variant="body2"> Forgot? </Link> However, I am exploring the transition to styled-components located in a separate file. ...

Can you tell me what is creating the space between the elements in this form?

For an example, take a look at this link: I am puzzled by the gap between the form-group and the button, as well as between the different form-groups. When using Bootstrap 4 with flexbox activated, all my elements (inputs and buttons) collapse without any ...

Retrieving specific data from an SQL database using PHP

My goal is to create a select form with fields populated from an SQL database containing a table of stores. I used a method to retrieve distinct store names and populate the select tool with them. echo '<select name="StoreName" />'."\ ...

Ways to identify the image shown during operating hours?

I have previously asked this question and received helpful answers. Now, let's consider a business that operates from 9:00 AM to 5:00 PM PST time (California), closed on Saturdays and Sundays. How can I make adjustments for this scenario? The script ...

Running a Node.js child process upon clicking an HTML button

I need to create a basic webpage where clicking a button will open the command prompt. This is my goal. Below are the HTML and Node.js code snippets. test.html <html> <head> </head> <body> <p>guru</p> <form a ...

Having trouble making an HTML5 video work on Android? It seems to play fine when clicking the play button,

It has come to my attention that html5 video on Android devices is unable to autoplay. Currently, the video only plays on the device when the user manually clicks on the play button. <video width="640px" height="360px" src="media/video/Moments_of_Every ...

Is there a way to adjust the scrolling speed of a background image to be slower than the rest of

I'm searching for an example similar to this: Are there any efficient javascript solutions available? I've had difficulty implementing the code I've come across so far. ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

Intersecting interactions: Mouse events on flex boxes

I am facing a challenge in my React application where I have two panels with buttons displayed using flex properties. Below is a simplified version of the HTML/SCSS code that illustrates the setup: <div class="bottom-panel left"> <butt ...

Revamping the Bootstrap admin template

Is there a way to customize this Bootstrap 3 admin template? https://getbootstrap.com/docs/3.3/examples/dashboard/ I want to make some changes like removing the top fixed navbar and shifting everything up by 50px (as defined in dashboard.css). However, I ...

Is it possible to eliminate the input border within React-Bootstrap?

Struggling to remove the input borders of the Form.Control component in React-Bootstrap. Despite searching for answers, nothing has been able to solve this issue. I've attempted redefining the .css classes "input" and "form-control", trying to set th ...