Why is the position of `:absolute;`destroy vertical-align:middle`?


display key

The text centeris in the center of the div as shown in the code below.

.Absolute-Center { 
  display: table-cell;
  width: 100px;  
  height: 100px; 
  border:1px solid red; 
  text-align:center;
  vertical-align:middle;
  } 
<div class="Absolute-Center">
    <p>center</p>
</div>

Now add a line position:absolute;to your CSS .Absolute-Center.

.Absolute-Center { 
  position:absolute;
  display: table-cell;
  width: 100px;  
  height: 100px; 
  border:1px solid red; 
  text-align:center;
  vertical-align:middle;
  } 
<div class="Absolute-Center">
    <p>center</p>
</div>

The text centeris now not in the center of the div, why position:absolute;is this causing this?

Jim Fahad

position: absolutebreaks on display: table / table-cell, so you need to centereduse /50% of the row paddingheight element'sof that height .

Instead use flexboxcorrect like below.

use this:

.Absolute-Center {
  border: 1px solid red;
  display: flex;
  flex-flow: column nowrap;
  height: 100px;
  justify-content: center;
  position: absolute;
  text-align: center;
  width: 100px;
}
<div class="Absolute-Center">
    <p>center</p>
</div>

Related


Why vertical align:middle doesn't work on my span or div?

Brad I'm trying to vertically center one spanor divelement inside another div. But when I put it in vertical-align: middle, nothing happens. I've tried changing the properties of displayboth elements , but nothing seems to work. This is what I currently do in

Twitter Bootstrap 3 - Vertical Align Columns [Middle]

Ifch0o1 I see that bootstrap cannot fix the height of the columns in the row. For example: Image 1:1, it will be responsive in the column, but the position of the image is at the top of the row. If I have text in the headinglabel , the text has a default margi

Twitter Bootstrap 3 - Vertical Align Columns [Middle]

Ifch0o1 I see that bootstrap cannot fix the height of the columns in the row. For example: Image 1:1, it will be responsive in the column, but the position of the image is at the top of the row. If I have text in the headinglabel , the text has a default margi

Why vertical align:middle doesn't work on my span or div?

Brad I'm trying to vertically center one spanor divelement inside another div. But when I put it in vertical-align: middle, nothing happens. I've tried changing the properties of displayboth elements , but nothing seems to work. This is what I currently do in

Twitter Bootstrap 3 - Vertical Align Columns [Middle]

Ifch0o1 I see that bootstrap cannot fix the height of the columns in the row. For example: Image 1:1, it will be responsive in the column, but the position of the image is at the top of the row. If I have text in the headinglabel , the text has a default margi

Align the middle (vertical) of the image's div with CSS

Mikel I could use some help to align images (different sizes) in the middle of the container div. Made a picture where the div (grey background) is visible and the image should be aligned in the middle. As you can see, if the images are of different sizes, it

Align Bootstrap Carousel Control Middle Vertical

Manu I'm using a Bootstrap conveyor belt and I want to slide back and forth in the conveyor belt vertically using a centered v-shaped arrow. Currently, they are centered horizontally, but aligned to the top. Code: <div class="row"> <div class="col-md-12">

'Vertical Align: Middle' doesn't work

Abdullah Hamzic | http://codepen.io/abdulahhamzic/pen/rLBoOj I'm trying to vertically align a paragraph (name of flow and other info) in the middle of its div parent. I'm trying the following CSS code but it's not working: .col-sm-4{ text-align: right; ver

Why does this bold text not respect CSS vertical-align: middle

Riola I have a bunch of text and images in a row that I want to align vertically. Everything seems to work fine except that I just added some bold text and the bold text seems to be a bit lower than regular text. Here is the screenshot: Here is a simplified vi

Twitter Bootstrap 3 - Vertical Align Columns [Middle]

Ifch0o1 I see that bootstrap cannot fix the height of the columns in the row. For example: Image 1:1, it will be responsive in the column, but the position of the image is at the top of the row. If I have text in the headinglabel , the text has a default margi

Calculations in JavaScript to overcome vertical-align: middle

Maki I am trying to write a function to find image height perform calculations (200px - image height) Add CSS styles for each different thumbnail ( .imgk{margin-top:"calculated value";}) . I can't set a fixed value margin-topfor it because the images vary in s

CSS vertical align middle

Gil Peretz I know this question has been discussed many times before. But honestly, I didn't find any proper solution for this unique situation: I have the following HTML code: <div class="print-types-links"> <ul> <li> <a href="#">

Align the middle (vertical) of the image's div with CSS

Mikel I could use some help to align images (different sizes) in the middle of the container div. Made a picture where the div (grey background) is visible and the image should be aligned in the middle. As you can see, if the images are of different sizes, it

Why vertical align:middle doesn't work on my span or div?

Brad I'm trying to vertically center one spanor divelement inside another div. But when I put it in vertical-align: middle, nothing happens. I've tried changing the properties of displayboth elements , but nothing seems to work. This is what I currently do in

Why vertical align:middle doesn't work on my span or div?

Brad I'm trying to vertically center one spanor divelement inside another div. But when I put it in vertical-align: middle, nothing happens. I've tried changing the properties of displayboth elements , but nothing seems to work. This is what I currently do in

Position: Absolute and Vertical Alignment: Middle

stee1rat How can I set the third column vertical-align: middleto be the same as all the other rows in my example? I realize the problem is position: absolute, but I don't know how to fix it. https://jsfiddle.net/pr1v6Lhd/1/ Not even with top positioning .data.

CSS vertical align middle

Gil Peretz I know this question has been discussed many times before. But honestly, I didn't find any proper solution for this unique situation: I have the following HTML code: <div class="print-types-links"> <ul> <li> <a href="#">

Position: Absolute and Vertical Alignment: Middle

stee1rat How can I set the third column vertical-align: middleto be the same as all the other rows in my example? I realize the problem is position: absolute, but I don't know how to fix it. https://jsfiddle.net/pr1v6Lhd/1/ Not even with top positioning .data.

Why does this bold text not respect CSS vertical-align: middle

Riola I have a bunch of text and images in a row that I want to align vertically. Everything seems to work fine except that I just added some bold text and the bold text seems to be a bit lower than regular text. Here is the screenshot: Here is a simplified vi