Incorrect table alignment table table

I am trying to just center the text horizontally and vertically using the DIV and table display type, but it doesnโ€™t work in IE8 or Firefox.

Below is the CSS that I use, and that is all that is on the html page.

@charset "utf-8"; /* CSS Document */ html, body { background-color:#FFFFFF; font-family:Arial, Helvetica, sans-serif; margin: 0; padding: 0; padding-top: 5px; } div.Main { background-color:#FFFFFF; border-collapse:collapse; width:800px; margin-left:auto; margin-right:auto; } div.MainHeader { color:#C00000; font-size:18pt; font-weight:bold; text-align:center; width:800px; } div.BlackBox { background-color:#000000; color:#FFFF00; display:table-cell; float:left; font-size:18pt; font-weight:bold; height:191px; text-align:center; vertical-align:middle; width:630px; } div.BlackBoxPicture { background-color:#000000; float:right; height:191px; margin-top:auto; margin-bottom:auto; text-align:right; vertical-align:bottom; width:170px; } 

What am I doing wrong?

+45
html css alignment
Oct 23 '10 at 17:51
source share
10 answers

I think the table-cell should have a display:table parent element.

+39
Oct 23 '10 at 17:55
source share

Here is how I do it:

CSS

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; display: table } #content { display: table-cell; text-align: center; vertical-align: middle } 

HTML:

 <div id="content"> Content goes here </div> 

Cm

Vertical Centering Example

and

CSS: centering things .

+36
May 27 '12 at 9:17
source share

You can vertically align the floating element in a way that works on IE 6+. It also does not need full tabular markup. This method is not entirely clean - it includes wrappers, and there are a few things you need to know, for example. if you have too much text pulling a container, but that's pretty good.




Short answer: You just need to apply display: table-cell to the element inside the moved element (table cells do not float) and use a backup with position: absolute and top for the old IE.




Long answer: Here is a jsfiddle showing the basics . The important material is generalized (with a conditional comment adding the .old-ie class):

  .wrap { float: left; height: 100px; /* any fixed amount */ } .wrap2 { height: inherit; } .content { display: table-cell; height: inherit; vertical-align: middle; } .old-ie .wrap{ position: relative; } .old-ie .wrap2 { position: absolute; top: 50%; } .old-ie .content { position: relative; top: -50%; display: block; } 

Here's a jsfiddle that intentionally throws out minor errors using this method. Please note how:

  • In standard browsers, content that exceeds the height of the wrapper element stops centered and begins to descend the page. This is not a big problem (it probably looks better than crawling the page), and it can be avoided by avoiding too much content (note that if I did not miss something, overflow methods like overflow: auto; do not look Work)
  • In old IE, the content element is not stretched to fill the available space - height is the height of the content, not the container.

These are fairly small limitations, but worth understanding.

Code and idea adapted from here

+9
Aug 28 2018-11-11T00:
source share

An element created as follows will be vertically aligned to the middle:

 .content{ position:relative; -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); transform: translateY(-50%); top:50%; } 

However, the parent element must have a fixed height. See this script: https://jsfiddle.net/15d0qfdg/12/

+4
Feb 26 '16 at 10:02
source share

In my case, I wanted to center in the parent container with the position: absolute.

 <div class="absolute-container"> <div class="parent-container"> <div class="centered-content"> My content </div> </div> </div> 

I had to add some positioning for the top, bottom, left and right.

 .absolute-container { position:absolute; top:0; left:0; bottom:0; right:0; } .parent-container { margin: 0; padding: 0; width: 100%; height: 100%; display: table } .centered-content { display: table-cell; text-align: center; vertical-align: middle } 
+4
Oct 30 '16 at 10:43
source share

Since you are still using float ...

try removing the "float" and wrap it with display: table

example:

 <div style="display:table"> <div style="display:table-cell;vertical-align:middle;text-align:center"> Hai i'm center here Lol </div> </div> 
+3
Mar 06 '13 at 8:07
source share

see this bit: http://jsbin.com/yacom/2/edit

should set the parent to

 display:table-cell; vertical-align:middle; text-align:center; 
+3
Apr 24 '14 at 8:29
source share

Once the vertical alignment brake floats, it is better to avoid them.

+2
Jan 22 '15 at
source share

Set the height for the parent element.

+1
Jul 16 '13 at 15:57
source share

Swim it with another shell without using display: table; , she works:

 <div style="float: right;"> <div style="display: table-cell; vertical-align: middle; width: 50%; height: 50%;">I am vertically aligned on your right! ^^</div> </div> 
+1
Dec 03 '13 at 14:52
source share



All Articles