Is there an issue with the vertical alignment of two adjacent empty inline blocks?

body {
  border: 2px solid black;
  height: 100px;
}

div {
  vertical-align: middle;
  display: inline-block;
  width: 50px;
  height: 50px;
}

.a {
  border: 2px solid red;
  margin-top: 20px;
}

.b {
  border: 2px solid green;
}
<!DOCTYPE html>
<html>

<body>

  <div class="a">a</div>
  <div class="b">b</div>dsfdg

</body>


</html>

What is the reason for both blocks not being aligned to the center? Why is there a peculiar gap between the top borders of the body and block b? Isn't something like this anticipated without explicitly adding top margin to block b?

body {
  border: 2px solid black;
  height: 100px;
}

div {
  vertical-align: middle;
  display: inline-block;
  width: 50px;
  height: 50px;
}

.a {
  border: 2px solid red;
  margin-top: 10px;
}

.b {
  margin-top: 10px;
  /*should not block b come automatically 10 px down without adding top margin to it */
  border: 2px solid green;
}
<!DOCTYPE html>
<html>

<body>

  <div class="a"></div>
  <div class="b"></div>dsfdg

</body>

</html>

Answer №1

Allow me to address your inquiry, as it may provide assistance to others as well. In this particular situation, the margin pertains to the element itself, meaning that any adjustments are relative to the size of the element, which includes the margin.

Here are a few examples:


Answer №2

To address this issue, simply using `vertical-align: middle` is not sufficient as the elements do not have a reference point for determining the middle of the section. To establish that reference point, you can utilize a structure involving `display: table-cell;` and `display: table;`. While an alternative approach involves using margins, it may not be as reliable as other methods.

body {
  border: 2px solid black;
  height: 100px;
  display: table-cell;
vertical-align: middle;
}

div {
  vertical-align: middle;
  display: inline-block;
  width: 50px;
  height: 50px;
}

.a {
  border: 2px solid red;
  margin-top: 10px;
display: table-cell;
vertical-align: middle;
}

.b {
  margin-top: 10px;
  /*should not block b come automatically 10 px down without adding top margin to it */
  border: 2px solid green;
display: table-cell;
vertical-align: middle;
}

.c {
display: table-cell;
vertical-align: middle;

}

html {
display: table;
width: 100%;
}
<!DOCTYPE html>
<html>

<body>

  <div class="a"></div>
  <div class="b"></div>
  <div class="c">dsfdg</div>

</body>

</html>

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

Override CSS properties / prevent inheritance

I'm currently facing a challenge in Wordpress where I need to insert a link quickly, but there are intricate styles applied to all the anchor links within a specific section. Here is a snippet of the CSS selector and styles being used: div.content-ro ...

Use one tag to abbreviate multiple times

As I create a step-by-step guide, I want the audience to be able to hover over any word in the text and see its definition. However, I only want to define abbreviations once. Is there a method in html or css that allows me to achieve this? ...

Counter for the type="range" HTML input

I recently read an article discussing the possibilities of displaying ticks in Chrome and Firefox for a number input with the type "range". I was intrigued to find out more about this feature and came across a helpful discussion on Stack Overflow. ...

Glowing semi-opaque about spotify?

Recently, I decided to challenge myself by recreating the Spotify homepage using only pure Javascript and SCSS as a way to test my front-end development skills. You can view my progress so far at this link, although please note that it's still a work ...

Adding link styles to the <a> tag in Bootstrap 4 Alpha 3: A simple guide

The following code is quite simple: <a>This is a link</a> In a demonstration of Bootstrap 4 Alpha 2 here, the link appears blue. When you hover over it, it turns dark blue. However, in a demonstration of Bootstrap 4 Alpha 3 here, it displays ...

Assistance needed for disabled JavaScript

I'm currently working on a small application where I need to display some text wrapped in 3 divs. The goal is to only show one div at a time and provide users with prev and next buttons to switch between them. However, when JavaScript is disabled, I w ...

transition effect of appearing and disappearing div

Having trouble creating a fade out followed by a fade in effect on a div element. The fade out happens too quickly and the fade in interrupts it abruptly. Here is the JavaScript code: $('#fillBg').stop(true,false).fadeTo(3000, 0); $("#fillBg"). ...

What is the method for importing a saved HTML into a WebView?

My WebView is supposed to load manipulated HTML saved in /klw.html. I am using the code mWebView.loadUrl("file:///android_asset/klw.html");. However, when testing it on the emulator, I keep getting a "Webpage not available" error. What could be causing thi ...

Is there a way to relocate the play again button below the box?

How can I ensure that my "Play Again" button moves up to align perfectly under the black border box? The black border box is styled with a class named .foot .button { background: rgb(247, 150, 192); background: radial-gradient(circle, rgba(247, 1 ...

Creating a static line or separator based on the content using HTML and CSS

Is there a way to adjust the height of a line or divider based on the content of either the left or right section? For instance, if we have a left panel and a right panel, and we draw a line or divider on the left side panel, is it possible for the line ...

Style the content in the TD to be aligned to the left only when an INPUT element

Can CSS be used to left-align the contents of a TD cell when it contains an Input? Specifically, can the Input itself be left-aligned? <td> <input ....Left Align Me... /></td> ...

What's the reason for the mousewheel functioning in Chrome but not in Firefox?

I am currently using CSS to enable scrolling up and down a popup page with just the mousewheel, but I'm facing an issue with it not working in FireFox. Surprisingly, it works fine in Chrome with the use of overflow-x:hidden; and overflow-y:auto; prope ...

Firefox experiencing glitchy rotation in CSS animations

I attempted to create a rotating div using a simple code snippet: <!DOCTYPE html> <html> <head> <style> .spinner { width: 100px; height: 100px; margin: auto; border-radius: 50%; border-bottom: 1px solid #f00; animation ...

Using jQuery to dynamically update the CSS of one table column depending on the attribute of another column

Welcome to my HTML table <!DOCTYPE html> <html> <body> <table> <thead> <th>Title1</th> <th>Title2</th> </thead> <tbody> <tr> <td><input type="checkbox" id=0 /></td> ...

How can I center an image next to text on two lines, that is compatible with IE7?

I am trying to align an image (logo) with text in two lines - the website title and a brief description. Here is my attempt using HTML: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional ...

Incorporating a stationary sidebar onto your website

My goal is to create a fixed side menu similar to the one on this website: Below is my HTML code: <!-- Scroll TEST --> <ul id="followTab"> <li> <a class="newsletter" href="newsletter/" title="Subscribe to ..."> ...

Tips for inserting database table parameters into CSS style

Is there a way to define CSS style parameters in a database table (MySQL) and then use them to style elements such as a menu? Currently, I am aware that we can use variables within styles like this: <div id="content-inner" style="<?php echo $this-&g ...

The drop-down list unexpectedly closes at the most inconvenient moment

I am looking to create a search input with a drop-down list. The requirement is for the list to close when the focus or click is anywhere except the search input. I have added a function listClose() to the "blur" listener, but now I am unable to capture t ...

Code snippets to reduce excess white space on a website using HTML and CSS

On my website, there are multiple tabs, each containing content from an HTML file. When a user clicks on Tab1, boxes with images are displayed. The layout can be seen in the following Code Demo: Code Demo here There seems to be extra space before and afte ...

Apply CSS styles from a text area using v-html

I am currently working on developing a unique WYSIWYG application where users have the ability to write CSS in a textarea field and view its direct impact on the HTML content displayed on the page. As I was experimenting with different approaches, I attemp ...