When using CSS to vertically align elements, it only works properly if a <span>

a {
    display: block;
    padding: 0 10px;
    /* background: rgba(0,0,0,0.5); */
    height: 100%;
    border-left: 1px solid rgba(0,0,0,0.1);
    text-decoration: none;
    color: white;
    background-color:rgba(0,0,0,0.5);
    height:100px;
}

img {
    max-height: 72px;
    max-width: 598;
    margin: 0;
    padding: 0;
        vertical-align: middle;
            width: 40px;
    height: 40px;
        border-radius: 50%;

}

.img_frame {
    display: inline-block;
    vertical-align: middle;
    height: 100%;
        background-color: black;

}
<a href="">
  <span class="img_frame"></span>
  <img class="size-40X40 img-round" src="https://yt3.ggpht.com/-zvxnrUIYjg8/AAAAAAAAAAI/AAAAAAAAAAA/vnVVaPAvxOE/s900-c-k-no-mo-rj-c0xffffff/photo.jpg">  
</a>

If I remove the span tag, the image does not align to the center. Even replacing the span with another tag like a p tag results in the same issue...

I would appreciate an explanation of these properties. Thank you so much in advance!

Answer №1

vertical-align on inline / inline-block elements, such as images and text, aligns the elements together rather than with their parent container. To achieve this, you can utilize flexbox. See the updated snippet below:

a{
    display: block;
    padding: 0 10px;
    /* background: rgba(0,0,0,0.5); */
    height: 100%;
    border-left: 1px solid rgba(0,0,0,0.1);
    text-decoration: none;
    color: white;
    background-color:rgba(0,0,0,0.5);
    height:100px;
    display: flex;
    align-items: center;
}
img{
    max-height: 72px;
    max-width: 598;
    margin: 0;
    padding: 0;
        vertical-align: middle;
            width: 40px;
    height: 40px;
        border-radius: 50%;

}

.img_frame {
    display: inline-block;
    vertical-align: middle;
    height: 100%;
        background-color: black;

}
<a href="">
  
  <img class="size-40X40 img-round" src="https://yt3.ggpht.com/-zvxnrUIYjg8/AAAAAAAAAAI/AAAAAAAAAAA/vnVVaPAvxOE/s900-c-k-no-mo-rj-c0xffffff/photo.jpg">  
</a>

Answer №2

CSS has a property called vertical-align that determines how elements placed next to each other on a line are aligned. To make this property work effectively, the elements should be positioned along a baseline and be inline or inline-block elements.

For more information, you can visit: https://css-tricks.com/almanac/properties/v/vertical-align/

In simpler terms, the vertical-align property specifies the positioning of inline (or inline-block) elements in relation to one another.

If you have an image and need another element like a span with height: 100%;, the span element plays the role of a helper.

span {vertical-align: middle;} and img {vertical-align: middle;} essentially mean: Center both elements must be on the same line.

a {
    display: block;
    padding: 0 10px;
    /* background: rgba(0,0,0,0.5); */
    height: 100%;
    border-left: 1px solid rgba(0,0,0,0.1);
    text-decoration: none;
    color: white;
    background-color:rgba(0,0,0,0.5);
    height:100px;
}

img {

    max-height: 72px;
    max-width: 598;
    margin: 0;
    padding: 0;
    vertical-align: middle;
    width: 40px;
    height: 40px;
    border-radius: 50%;

}

.helper {

    display: inline-block;
    height: 100%;
    vertical-align: middle;
}
<a href="#">

    <span class="helper"></span>
    
    <img class="size-40X40 img-round" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRSJoLHGrCss93oyQO7wXQr02WZbL83EiJFxza5pe-gN3o_jaK7jw">
    
</a>

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

The image fails to load once the AI-generated image is complete and the response process has concluded

After a long hiatus from development, I am diving back in and acknowledging the rustiness of my skills. Any syntax or formatting errors encountered are preemptively apologized for. Currently, I am immersed in a small web app project that involves taking a ...

Challenges arise when utilizing the foundation 6 grid for image overlays within a React application

Having some trouble using the foundation 6 xy grid for the first time and aligning three images with text overlays at the bottom of each image. I am struggling to make the background of the text fill the full width of the image while also ensuring it is re ...

Angular framework does not trigger the button click event

Currently, I am in the process of creating a web app and working on designing the register page. My experience with Angular is still at a beginner level. The following code snippet is from the resiger.component.ts file: /** * To create a new User ...

Make sure to keep the <div> element nested within the image

I'm working on creating a photo album that displays images in full screen and allows users to navigate using arrows. Check out my progress here: https://jsfiddle.net/q49wzey6/ Here's the HTML code I'm using: <div class="main"> &l ...

Opening a Text File via an ASPX Web Page

Currently, I am immersed in a Web Pages project. The goal is to retrieve text from a Text File and display it within a div element. To achieve this, I utilized the ajax xmlhttprequest method, following a tutorial available at http://www.w3schools.com/ajax/ ...

HTML Date Form: Setting the Start Date to Tomorrow with JavaScript

Is there a way to restrict the start date to tomorrow's local date? I'm having trouble with the code below: <form method="POST"> <div> <label for="s2">pickup_date</label> ...

Deactivate an option within an angularjs multi-select menu

Currently, I am utilizing an angularjs multiselect box which is displayed below: https://i.stack.imgur.com/w6ffN.png Here is the html code snippet: <select multiple ng-options="Language.LangID as Language.LangName for Language in AllLanguages " ng-mo ...

Leveraging CSS keyframes to create dynamic animations

I am facing an issue with CSS while designing my website. I reached out to the plugin developer for help through a ticket submission, but unfortunately, I have not received a response yet. On my homepage at , I have utilized CSS keyframes to create a text ...

What is the best way to input individual students' CA and exam scores into distinct fields and then calculate their total scores in a separate text field?

As a beginner in jQuery, I am looking for guidance on creating a script that calculates the Total score, Grade, Remark, and Position based on the user input of CAT score and EXAM score. The result should be displayed dynamically once both scores are entere ...

Troubleshooting issues with the IE flexible box model functionality

Working on incorporating the flexible box model into my website has been a bit challenging. It seems to function well in webkit-based browsers like Chrome and Safari, but I'm running into issues with Mozilla, Opera, and most notably Internet Explorer. ...

Using a DIV to contain a FileUpload Control in an ASPX page

Scenario: I have an ASPX page (Page A) without a form element, which contains a DIV displaying another ASPX page (Page B) with a FileUpload Control inside a form element. Requirement: Submit the form to upload the file without refreshing or navigating awa ...

Correcting the reference to "/" (root) for assets after relocating the site to a subdirectory

I currently have a website located in the public_html directory, where all assets (images, css, js, etc) are referenced using /asset_folder/asset. The "/" at the beginning ensures that the browser starts from the root and navigates through the directories. ...

`Creating a fluid MySQL table using JavaScript`

One of the challenges I'm facing involves working with a MySQL table that consists of 3 columns: MySQL db table +--+----+-------------+ |id|data|display_order| +--+----+-------------+ |0 |0 |2 | +--+----+-------------+ |1 |1 |1 ...

react elements are consistently positioned at the bottom of the webpage

I am currently in the process of developing an airline reservation system using the MERN stack. I have successfully implemented the navbar, however, whenever I try to add a new div or element, it appears outside of the page and requires additional styling ...

Obtain information about a div element while scrolling

Looking to enhance my social feed page by adding a view count feature for each post. The challenge is figuring out how to keep track of views as the user scrolls down the page. Any suggestions? ...

Mounted class not initiating transition in Vue.js

After attempting to apply a class to an element in the mounted lifecycle, I noticed that the transition effect was not taking place. The element would immediately display in its final state: However, when I used setTimeout to delay the class change, the t ...

Tips for implementing fluid transitions between mouse X and Y coordinates using JavaScript

I recently developed a function that enables a DOM element to follow the mouse cursor. You can check out the code here. Currently, I am looking for suggestions on how to add a nice animation to this feature. Ideally, I want to incorporate a slight delay w ...

Adjusting canvas context position after resizing

I've been experimenting with using a canvas as a texture in three.js. Since three.js requires textures to have dimensions that are powers of two, I initially set the canvas width and height to [512, 512]. However, I want the final canvas to have non-p ...

Using single and double quotes in combination with brackets in PHP: a guide

My struggle lies in using single and double quotes along with brackets in PHP $nestedData[] = '<a href="JavaScript:newPopup('loghistory.php?logid='.$row['user_id'].'')"> History('.$countqry.')</a>&a ...

Steps for creating a basic JQuery function for dividing integers

My website constantly updates with real-time data from our PI server, refreshing every 10 seconds. I've been given the task to take two existing data points already on my page and calculate their division, then visualize the new result on a graph. Due ...