Ways of distinguishing the variance between vertical align: -0.125em and vertical align: middle explained

Upon coming across the code vertical-align: -0.125em;, I begin to ponder about the contrasts between this and vertical-align: middle.

Answer №1

vertical-align:middle is used to align the middle of an element with the baseline plus half the x-height of its parent.

This means it positions the middle of the element at a specific distance relative to the baseline and x-height of the parent.

To illustrate, consider this example:

.box {
  font-size:50px;
  background:
    linear-gradient(green,green) 0 46px/100% 2px no-repeat;
}

.box > span {
  display:inline-block;
  width:30px;
  height:30px;
  background:
    linear-gradient(black,black) center/100% 2px no-repeat,
    linear-gradient(red,red) 0 calc(50% + 0.5ex)/100% 1px no-repeat,
    yellow;
  vertical-align:middle;
}
<div class="box">
Some text j <span></span>
</div>

In contrast, vertical-align: -0.125em shifts the baseline of an element by a specified length above its parent's baseline, allowing for negative values as well.

With this setting, the baseline of the element is offset from its parent's baseline by the defined length.

Here's an example demonstrating this concept:

.box {
  font-size:50px;
  background:
    linear-gradient(green,green) 0 46px/100% 2px no-repeat;
}

.box > span {
  display:inline-block;
  font-size:20px;
  width:30px;
  height:30px;
  background:
    linear-gradient(black,black) 0 18px/100% 2px no-repeat,
    linear-gradient(red,red) 0 calc(18px - 0.125em)/100% 1px no-repeat,
    yellow;
  vertical-align:-0.125em;
}
<div class="box">
Some text j <span>aq</span>
</div>

It's important to note that these two alignment methods may appear similar in certain scenarios, but they are distinct in definition and context. They should not be considered interchangeable despite potential visual alignment similarities.

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 CSS dropdown navigation bar designed for mobile devices functions properly, however, it is not visible on iPhones

After building this website from scratch using only HTML and CSS, I encountered an issue with the dropdown menu not appearing on iPhones. Despite testing it extensively across different browsers and devices, the problem persists on Apple products. While i ...

Floating CSS containers

Apologies for not including a picture. You can view the design I am trying to achieve here. I am attempting to create two boxes with a third box below them, and another box on the right side of the layout. All enclosed within a larger container. Everythin ...

Format six images in a horizontal row for desktop and ensure they resize appropriately for mobile viewing

I am looking to arrange images of different sizes in rows depending on the device orientation. For desktop, I want 6 images with proper margins, 2 images in a line for portrait, and 4 images for landscape orientation. Any extra images should be placed in a ...

What is the best way to align text in the middle of a div using Foundation framework?

It seems like I may be either overanalyzing this or overlooking a small detail. The task at hand is to align text in the center within a div using Foundation. Here is the current code that I am working with: [1]<div class="row"> [2] <div class ...

Initiate the typeahead drop down menu by pressing the downward key and then navigate through the options within the drop down

Utilizing the bootstrap typeahead feature within my angular project has been a game-changer. I needed a way to activate the typeahead drop down by pressing the down key when no text had been entered yet. By following guidance from this resource, I was able ...

Cards in Bootstrap 4 not adhering to sticky positioning as an alternative to affix

I have created a webpage for my thymeleaf-based Spring Boot project with various cards. Here is the code snippet: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Card</title> <meta charset="utf-8"> ...

How can I create a CSS animation that fades in text blocks when hovering over an image?

I have been trying to implement CSS3 transitions to create a hover effect where the text and background color fade in when hovering over an image. Despite my attempts with various selectors and transition types, I can't seem to achieve the desired res ...

Shift the items in the unordered list to the left

I am currently working on a carousel design. I have already finished coding the HTML and CSS, but I am unsure of how to shift the elements one by one to the right or left while ensuring that 7 pictures are always visible in the viewport. .seccion { ...

Dissipating visuals / Effortless website navigation

Do you have experience working with HTML or are you looking for specific code examples? I've noticed some websites with sliders that feature clickable images fading in as soon as the page loads. I tried searching for information on how to achieve thi ...

The styles applied to the Angular 5 Component host element are not being reflected correctly in jsPlumb

As a beginner in Angular >2 development, I am excited to build my first application from scratch. One of the features I'm trying to implement is drawing flow charts using jsPlumb. However, I have encountered an issue where the connectors are not be ...

Is there a way to collapse child elements in a hover sidebar using Vuetify?

My project includes a sidebar that opens when I hover over it with the mouse. Everything works fine, but within the sidebar, there is a collapse dropdown menu. When I open this menu and then move the mouse away from the sidebar and back again, the collapse ...

One portion of the page is not appearing on the mobile version

I'm having an issue with one of the sections on my website not displaying correctly in mobile view. I've checked within WooCommerce and the PHP file, but everything appears to be fine. I attempted to override the section using !important, as well ...

Are CSS class names the secret sauce of web design?

Imagine we have a 'Car' bean containing information about the color of a car, and we want to display the color name in that specific color along with an image of the car next to it. Currently, the bean handles the conversion of the color name to ...

Issues with JQuery: Cannot display element using its ID

There is a hidden element that should be displayed upon an action trigger. Sample code: <a href="#Comment" class="btn btn-control" id="Commentbtn"> <svg class="olymp-comments-post-icon"><use xlink:hr ...

The main menu is not displaying on mobile devices when in portrait orientation

Currently delving into the world of CSS/HTML for mobile devices, I've encountered an issue with my site. It looks great on desktop, but on a mobile device, the menu disappears. However, when switched to landscape mode, the menu magically reappears. An ...

Tips for positioning div elements with css tables

Is it possible to align the div elements in such a way that the first column element spans two rows, while the second column elements are stacked on top of each other using CSS tables? Below is a snippet of my HTML code: <html> <head><link ...

unable to decrease the gap between the rows of the table

I'm currently developing an application that involves a table with rows and columns, along with a checkbox container arranged as shown in the image below: https://i.stack.imgur.com/QoVrg.png One issue I'm facing is getting the letters A, B, C, ...

Accurately aligning a distorted backdrop image

I'm currently working on a design for a profile page where the header background is a blurred version of the profile picture. However, I am facing some difficulties with the overlay in my implementation. You can see an example of what I'm talkin ...

Unable to display the outside table border in CSS styling

Lately, I've been relying on 'border = 1' in my HTML for styling, but I've come to the realization that using CSS would be more efficient. So, I went ahead and created a simple border class like this... .basicborder table, .basicborder ...

Customize the appearance of all QProgressBars using Qt stylesheets in your C++ program

Struggling to figure out how to dynamically apply a stylesheet to multiple or all widgets in Qt Creator 4.1 (Qt 5.7) using C++. For instance, if I have 3 progress bar widgets, currently I am setting the same stylesheet for each one individually with CSS: ...