What causes the vertical alignment of one inline div to impact a different inline div when changed?

Three rectangular divs with solid borders are displayed inline, each assigned the classes left, center, and right.

When the left class has a style of vertical-align: top and the right class has a style of vertical-align: middle, they are positioned as follows:

https://i.sstatic.net/DWXhY.png

Adding vertical-align: bottom to the center class does not affect the center block at all. Instead, it shifts the right block up as if no vertical-align rules were applied. What CSS properties are causing this behavior?

https://i.sstatic.net/nUEXy.png

View JSfiddle Before

View JSfiddle After

Answer №1

It's an interesting issue to consider. The key is to understand that vertical-align: middle behaves differently compared to other alignments because it is affected by the lowercase letters of the parent element. To demonstrate this, I encapsulated the divs within a wrapper:

.wrapper {
  margin: 0;
  padding: 0;
  height: 120px;
  width: 500px;
}

div {
    display:inline-block;
    height:100px;
    width:25px;
    border:1px solid black;
}

.left{
  vertical-align: top;
}

.center{
}

.right{
  vertical-align: middle;
}
<div class="wrapper">
  sddsfsdfdsfdsfdsfdsfdsfdsfdsaa
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
</div>

This clearly illustrates how the right div, with vertical align: middle, is positioned in relation to the lowercase letters.

Moving on to the second example:

.wrapper {
  margin: 0;
  padding: 0;
  height: 120px;
  width: 500px;
}
div {
    display:inline-block;
    height:100px;
    width:25px;
    border:1px solid black;
}

.left{
  vertical-align: top;
}

.center{
  vertical-align: bottom;
}

.right{
  vertical-align: middle;
}
<div class="wrapper">
  test test test test test
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
</div>

In this case, everything follows the expected rules - the items are aligned based on the baseline, and the middle aligns with the letters of the parent container.

For more in-depth information about this behavior, you can refer to 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

What is the best method for showcasing two images side by side in a column layout?

I need to showcase 2 images in a row using AngularJS, with the next two images in the following row and so forth. Img1 Img2 Img3 Img4 This is my code: <section id="content"> <div class="content-wrap"> <div class="container clearfix "& ...

Is there a way to pause the animation when scrolling back to the top in the animate.css library?

Currently, I have integrated the react-animate-on-scroll package which is using the animate.css library to animate text on a web page. However, I am only looking to bring about the animation effect when scrolling down and not while scrolling up. Is there ...

Add navigation bar to every page - using HTML and CSS

Is there a way to smoothly insert HTML codes into all website pages without compromising the CSS design? I've attempted using but found it messy. Is there a simpler method to insert the HTML file into other files? The HTML file I'm trying to lo ...

What is the best way to modify the size of the letter background?

I'm facing an issue with a word that has a background color. While using the CSS property background-color: blue, allows me to give the word a background color and display: inline-block helps in sizing it exactly to the word, the problem arises when I ...

Overlay text on an image using a CSS grid

I'm working on a CSS grid layout where I want to overlay text on an image. Below you can see the code I currently have without the text overlay. .sbp-item12 { grid-row: 6 / 7; grid-column: 9/13; height: 250px; } <div ...

Utilizing background image CSS class repetitively

I have a button background class that I want to reuse multiple times. How can I keep all the common styles separate and only change the URL multiple times? Here is my CSS: .menuBtnColour{ position: relative; //common top: 0;//common left: 0;/ ...

Is there a way to incorporate page animations when utilizing the <a href> tag in HTML?

I have created several HTML files. On the main page, I simply inserted some code like this: <a href="new.html> <img src="img/button" id="buttonid"> </a> When I click the button, it takes me to the new.html page. I would like ...

Could you provide guidance on how to toggle the visibility of a div using the click event of a button or link located on a different HTML page in Angular?

Is there a way to change the visibility of a div on a different page when a button or link is clicked? For example, I have a button on the "main.html" page that, when clicked, should display a hidden div on the "header.html" page. How can I achieve this? ...

Issues with navigation menu functionality in IE8

When I click on the navigation menu in IE8, the menus overlap with each other. This issue does not occur in other browsers. Here is the link to my site: Below is the code for the navigation menu: li id="<?php echo ($epage =='ehome') ? ' ...

Top method for identifying "abandoned words" within text that has been wrapped

Our content and design teams have a specific request to prevent paragraphs from ending with an "orphan word" - a single word on the last line of text that has wrapped onto multiple lines. The designer's proposed solution is to adjust the margins sligh ...

Submenu Positioning Problem Identified in Firefox Display

I am currently working on a menu design for a website and encountered an unusual issue while testing it in Firefox. In the provided fiddle link, you can view the menu layout where hovering over the information button should display a submenu directly below ...

linking a div within a navigation bar

I have implemented a template from bootstrap. Here is the navigation bar where you can find the about section. Inside it, there is a bootstrap button: <button type="button" class="btn btn-light">Light</button> When I click on this button, the ...

Shift the plus-minus icon on the bootstrap accordion all the way to the right side

I'm struggling to adjust the position of the plus-minus icon on a Bootstrap accordion to the far right, but my current code isn't producing the desired result. Here's the CSS snippet: .mb-0 > a:before { float: right !important; ...

CSS3 Feature: Chevron-shaped banner with jagged edges

I'm currently working on building a unique chevron-style banner using CSS3, but I've run into some aliasing problems specifically in Chrome. Despite trying to utilize the -webkit-backface-visibility: hidden attribute, I still can't seem to r ...

The CSS on the page does not update when called within a JavaScript function

Just joined and this is my first question, so please go easy :). I've encountered a problem with changing the CSS of elements and their refresh. I'm working on a webforms application with AJAX requests. The issue I'm facing is that when som ...

The image refuses to align to the left side below a floated paragraph

Hey there, I'm working on some CSS and I've run into an issue. I have two paragraphs and I want to place a picture below the paragraph on the left side. The left paragraph is longer than the one on the right, which seems to be causing the picture ...

Div refuses to clear floats

Working on an app at and encountering an issue with the main container not clearing floats, causing overflow on the content. Attempted to use this CSS: .clearfix:after { content:""; display: table; clear: both; } Added the class to the main ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

What is the best way to enable an element to overflow within a block-level element?

Is it possible to have vertical scrolling in a bootstrap table while also allowing certain elements to expand and overflow the entire table on hover? I am facing an issue where setting tbody { display: block } for vertical scrolling prevents the expanding ...

Prevent Angular from automatically scrolling to the top when subscribing to a timer

I have a real-time updating list of orders that is scrollable. This is the main component, where the list gets updated every 2 minutes with the following setup: ngOnInit(): void { this.internalError = false; this.subscription = timer(0, 120 * 1000) ...