The alignment of the vertically centered item using line-height is not perfectly centered

Can you explain why the green item in this line-height and inline-block setup is a few pixels below the middle? Shouldn't there be an equal 15px above and below?

.container{
  height: 45px; 
  line-height: 45px; 
  background-color: red; 
  display: inline-block
}

.item{
  height: 15px; 
  width: 15px; 
  background-color: green; 
  vertical-align: middle; 
  display: inline-block
}
<div class="container">
  <div class="item">
  </div>
</div>

I understand that there are various methods for aligning items vertically, such as using JavaScript, absolute positioning, and more. I am not looking for a general solution to how to vertically align a div.

Answer №1

It's not the line-height causing the issue here, but rather the vertical-align: middle property. This property attempts to align the box with any text that could potentially be inside its parent container. The positioning of the inner box depends on the font-size of the text within the parent container. You can adjust the position of the box by increasing the font-size of the parent:

.container{
  height: 45px; 
  width: 100%;
  line-height: 45px; 
  font-size: 45px;
  background-color: red; 
  display: inline-block
}

.item{
  height: 15px; 
  width: 40px; 
  background-color: green; 
  vertical-align: middle; 
  display: inline-block;
}
<div class="container">
job
  <div class="item">
  </div>
</div>

The text is positioned closer to the bottom of the container in this example (the "j" extends beyond the container while the "b" does not).

Similarly, you can move the box closer to the center by decreasing the font-size. If you want to achieve optimal centering using this approach, set the font-size to 0 on the container:

.container{
  height: 45px; 
  width: 100%;
  line-height: 45px; 
  font-size: 0px;
  background-color: red; 
  display: inline-block
}

.item{
  height: 15px; 
  width: 40px; 
  background-color: green; 
  vertical-align: middle; 
  display: inline-block;
}
<div class="container">
job
  <div class="item">
  </div>
</div>

Answer №2

Updating your look could make a difference

.box {
    background-color: #ff0000;
    display: table-cell;
    height: 45px;
    vertical-align: middle;
}
.element {
    background-color: #008000;
    display: table-cell;
    height: 15px;
    vertical-align: middle;
    width: 15px;
}

Answer №3

To ensure proper functionality, utilize divisible dimensions and eliminate the vertical align attribute.

https://jsfiddle.net/guc6uxc7/

.container{
 height: 42px; 
 line-height: 42px; 
 background-color: red; 
 display: inline-block
 }

 .item{
 height: 12px; 
 width: 12px; 
 background-color: green; 
 display: inline-block;
 }

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

Issues with CSS rendering in the Chrome browser are causing display problems

After successfully creating an iFrames page tab on Facebook that looks great in Firefox and Internet Explorer, I encountered an issue with the div positioning in Chrome. You can view the page here: Oddly enough, this is the only page experiencing problems ...

Steps to retrieve the final page number from ngx-pagination with Angular

Is there a way to utilize Custom templates within ngx-pagination in order to ensure that the first and last buttons function properly when clicked? Currently, I have utilized pagination-template to accomplish this... How can I dynamically determine the la ...

PHP code failing to save form information into database

I am experiencing difficulties with inserting form data into my database. The connection to the database is successfully established without any errors, but no information is being entered. Below is my code - any assistance would be greatly appreciated. ...

JavaScript onclick event triggers only the second audio to play

Does anyone have experience with adding sound effects to a website? I'm attempting to have a positive sound play when the correct image is clicked and a negative sound play when the wrong image is clicked. However, regardless of which image I click on ...

What is the process for choosing a particular input element based on its size and type?

Among my pair of sets containing input type=text elements, each group is uniquely styled through CSS. In the first set, the length adjusts based on CSS using the width property. Conversely, in the second set, the size attribute determines the length of the ...

Having trouble with your CSS dropdown menu?

I've been struggling to implement a dropdown menu on my website following an online tutorial. Despite following the steps, I can't seem to get it working. It's frustrating because I want to move on to backend development. Hoping someone can ...

Bootstrap box grids

I'm attempting to create grids similar to the image provided below IMAGE LINK: However, I am experiencing an issue with DIV E being positioned almost right next to DIV D Below is the code I have so far: <div class="container"> <div cl ...

Utilizing the req.body object in a GET request with Node.js

I'm facing an issue with a form that utilizes the GET method. I have an input named 'a' and when handling the request on the server side (using Node.js), I need to access req.body.a to search for 'a' in the database. However, it se ...

Creating a full-width header in React: A step-by-step guide

I'm facing an issue with the header I created - I want it to be 100% width of the screen, but when I decrease the screen size, it doesn't scale accordingly. Here's my current CSS: <Box style={{ backgroundColor: "RGB(46 ...

Implementing inline scrolling using CSS

My objective is to have each article added to the container div, "inlineExample", load to the right without any vertical overflow. The CSS should be able to handle each additional article dynamically as they are added to the right. <div id="inlineExamp ...

What is the proper way to showcase thumbnails in different sizes?

Currently, this is what I have: https://i.sstatic.net/VOC2z.png The display looks optimal with high-resolution landscape photos. This is the HTML and CSS code in use: <div class="upload-thumb ng-scope visible"> <span class="delete-media"&g ...

What are some ways to enhance the opacity of a Material UI backdrop?

I'm looking to enhance the darkness of the material UI backdrop as its default appearance is not very dark. My goal is to make it dimmer and closer to black in color. ...

Troubleshooting Issue: Ionic 3 and Angular ngForm not transmitting data

Attempting to create a basic crud app with ionic 3, I am encountering an issue where new data cannot be inserted into the database. The problem seems to lie in the PHP server receiving an empty post array. Below is my Ionic/Angular code: Insert.html < ...

Centering items in Material UI Grid

I'm currently grappling with understanding Material UI's grid system and implementing it in React.js, and I find it a bit perplexing. My goal is to center the spinner loader and text irrespective of viewport size. While I can manage to place the ...

The Bootstrap website appears visually appealing on desktop browsers, but the div elements are failing to display correctly on mobile devices

After using Bootstrap to create a multi-row, multi-column site, I encountered an issue with resizing on mobile devices. The divs misalign, stack on top of each other, content disappears, and images flatten to the point of invisibility. How can I maintain t ...

Tips for setting up the Top Menu and Left Side Menu in your system

You're looking to incorporate both the Top Menu and Left Side Menu. The top menu should remain fixed at the top of the page. Implementing the Left Side Menu below the Top Menu has been a challenge. It's currently covering the top menu, which is ...

Is it possible for browsers to provide autocomplete support for login forms that are loaded via ajax?

I'm having an issue with the autocomplete feature in browsers (IE & FF) not working for my login form. In my webapp using CakePHP and jQuery, I have a login form inside a div that is loaded via AJAX to allow visitors to login or register without relo ...

Is it possible to transfer the style object from PaperProps to makeStyles in Material UI?

I am working with a Material UI Menu component and attempting to customize its border. Currently, I am only able to achieve this customization by using PaperProps inline on the Menu element. However, I already have a makeStyles object defined. Is there a ...

Sequentially animate objects with a fade-out and fade-in effect

Attempting to achieve a fade-out, fade-in animation using JavaScript and CSS. Here is the CSS animation code: @keyframes fade-in{ 0%{ opacity: 0%; } 100%{ opacity: 100%; } } @keyframes fade-out{ 0%{ opacity: 100%; } 100%{ opacity: 0%; } } Impleme ...

Customizing the Mui Theme in a Unique Way

After generating an MUI component from Mui DatePicker, I encountered the following code: <span class="my-theme-MuiTypography-root my-theme-MuiTypography-caption my-theme-MuiDayCalendar-weekdayLabel css-1mln09i-MuiTypography-root-MuiDayCalendar-week ...