It’s also possible to do it using div’s and CSS without using px sizes or tables:
<div class="contendor" style="border: 1px solid green; margin: 0; padding: 0; display: table">
<div class="contendor-secundario" style="border: 1px solid yellow; margin: 0; padding: 0; display: table-row">
<div class="columna1" style="border: 1px solid red; margin: 0pt; padding: 0pt; vertical-align: middle; display: table-cell">columna uno</div>
<div class="columna2" style="border: 1px solid blue; margin: 0; padding: 0; display: table-cell">columna dos,
la cual es bastante
más alta que la anterior,
y además la podemos hacer
todavía más alta
y la columna 1 ya no se centra</div>
</div>
</div>
what happen when you have different image size but whatever the image sizes you want all them in center. normally in jquery gallery case. i am struggling with this one from last two days. any help will be highly appreciated.
Hi, I have posted about 50 post to my blog. Now I have to change my template blog with a new one. I have upload the picture too.
The problem with my new template is my picture that I have uploaded before its not in the position I wanted. I have to re-upload the picture so its fix to my new template. Is there any can help me with this. So the picture that I have upload before its force to (300X300 pixel). Thank you before.
I LOVE YOU!! Ahem, thanks heaps. I was trying to figure out how to place a graphic using absolute position on a centered body container. I realized you made the graphic centered by ‘left: 50%’ and then using my container width I divided by 2 and altered the measurements to get a perfect fit ;). ‘left: 50%’ is a very important element when using absolute position. Dammit, if I only knew this before then I wouldn’t change my design and css code :(. Oh well, you live and learn :D
I want an image in the background of my webpage (top center). Different image on every page.
On top of that I want to have a 1000px wide Div in the center of the page with the content.
I want it all to stay in the middle when I resize the browser-window.
How?
Thank you, Matthew Buchanan! This is perfect — nice and clean, and works for varying image sizes. It works for me in Firefox, Chrome, Safari, Opera, and IE 8 and 9.
Thanks for the tutorial, I am trying to place a paragraph below the centralised image, but when I do this the paragraph remains at the top left hand side of the browser window.
/* Make the height 100% */
#container ul li{
display:inline-block;
height:100%;
}
/* Then vertical-align the crap outta it */
#container ul li img{
vertical-align:middle;
}
/* NOTE: The images don’t have fixed sizes either :) This works in Firefox, been using it for a while so I must have cross browser tested it but can’t remember anymore. Give it go! */
Hi Chris – or any genius out there, is there a “responsive” way of doing this? So when you resize your browser using “img{max-width:100%;height:auto}” for the images to resize, is there a way to center it inside a div both vertically and horizontally?
Hi.. Thanks for the tut. I have been referring to css-tricks.com for a ling time for all sorts of different things, and as a developer, your site has been a great resource and helps speed up development of some of my projects. So big thumbs up! :)
You can also use the line-height method; just as you would to vertically align a single line of text.
Set the image’s container to a specific height and also give it a line-height with the same value as the height. Then give the image inside the container a display: inline-block or just inline.
If you also want the image to be centered, since it’s already got a display: inline, you can give the container a text-align: center which will make the image centered, too.
I have tried it over one of my project and found that if i use html5 doctype than your css background-image technique wont work in chrome. I have to remove the html5 doctype for working.
Best practices Chris :)
These solution depends on ux, as my observation this xperience for under construction page. And your this technique is best practice for it:
html {
width:100%;
height:100%;
background:url(logo.png) center center no-repeat;
}
One my calculated suggestion image size should 206px square because this will works responsively as well :)
Here is my technique for text content but not for lt ie8:
I have found that this CSS seems to do the job, it works when you replace the dimensions of the image class with percentages rather than pixels, it seems to work out the actual pixels itself, it’s quite simple:
There is little math behind it, simply make the top percentage half of whatever you take off the height, in my case I subtracted 20% meaning the top position is 10%. I don’t know if it will work correctly for everybody’s desires, but I have had no problems with it since!
It is a really great article about image and text alignment in the horizontal and vertical middle.
Here I am given small example for image and text align middle in div
Just use change width and height, remove ‘bottom’ or whatever to stick it to a side.
Update your answer or get a job at McDonalds.
#element {
position: absolute;
margin: auto;
bottom: 0;
left: 0;
top: 0;
right: 0;
width: 960px;
height: 450px;
}
function c() {
var a = (window.innerHeight - 600) / 2;
//replace window.innerHeight with parent div width
var b = (window.innerWidth - 800) / 2;
//replace window.innerWidth with parent div width
document.getElementsByTagName("body")[0].style.padding = a + "px" + " " + b + "px";
//replace body with parent div width
}
setInterval(c, 100); //if window size changes. can remove, but need to call function at least once.
It’s also possible to do it using div’s and CSS without using px sizes or tables:
It’s explained here, but in spanish :(
You could use flex (fiddle):
But the flex prooerty does not support on IE versions
explination using google page translator.
This is called “The dead center”, or am I wrong?
Thanks, this helps a lot
It’s explained in English here:
http://www.wpdfd.com/editorial/thebox/deadcentre4.html
This was exactly what I needed, thank you.
i was seeing the example, will these work (?) if the container has some margin top.
good one! but the problem is that if you re size the browser you can’t scroll to the top. i.e if i have a menu on top i cant view it!
Why does everyone in the universe write background shorthand wrong?
This is the correct way and the only way that works in ALL browsers that support background shorthand:
background: #FFF url() repeat fixed left top;
/rant
thanks, i agree with you, WHY
Craig, syntactically, you can have your properties in any arrangement:
background: #fff url() fixed left top;
is essentially the same as
background: url() fixed 0 0 #fff;
either way you look at it, it’s doing the same exact thing.
nice information, thanks!
Thanks. This helped me a lot.
Brillant!!
Right? I love this. This made me so happy:
Vertical and right aligned fixed div relative to viewport! #fuckyeah!
With FF, no fixed image size:
IE does not understand ‘top’ in %… :(
Best solution for me. Thanks.
Hi,
Thanks a lot frnd! :)
I was searching for the same thing from a long time and now I got the one…
Thanks all for the trick.. I was searching for that for some time..
YOU ARE A KING MAN
I would advice THIS PAGE for anyone whos searching for a solution regarding absolute centering of Image inside a DIV.
WOOOOW!
what happen when you have different image size but whatever the image sizes you want all them in center. normally in jquery gallery case. i am struggling with this one from last two days. any help will be highly appreciated.
ummm hmmm only javascipt can probably do that i think maybe? O.o
something like:
should center an image with id=”myimage”
given an absolute position of :
and if you wanted to do a bunch of them you would have to make an array maybe and cycle through them? O.o
anyone else?
well done! TY!
Hi there
May I know how you made this website? The layout is just awesome
???
What about a pure CSS solution for vertically centering an image in a div that is POSITION:FIXED
???
Thanks a lot!
Hi, I have posted about 50 post to my blog. Now I have to change my template blog with a new one. I have upload the picture too.
The problem with my new template is my picture that I have uploaded before its not in the position I wanted. I have to re-upload the picture so its fix to my new template. Is there any can help me with this. So the picture that I have upload before its force to (300X300 pixel). Thank you before.
What do you do when the size of the image is unknown?
Here’s a simple jQuery code that checks the image size and then centers it… perfect for when you don’t know what the size of the image would be.
https://github.com/devasaur/vlign.js/blob/master/vlign.js
Good attempt to make new people love css.Keep it up!
I LOVE YOU!! Ahem, thanks heaps. I was trying to figure out how to place a graphic using absolute position on a centered body container. I realized you made the graphic centered by ‘left: 50%’ and then using my container width I divided by 2 and altered the measurements to get a perfect fit ;). ‘left: 50%’ is a very important element when using absolute position. Dammit, if I only knew this before then I wouldn’t change my design and css code :(. Oh well, you live and learn :D
Thanks again and much appreciated.
CHEERS :)
ah – some very handy tricks which definitely helped me out. thanks for sharing!
I want an image in the background of my webpage (top center). Different image on every page.
On top of that I want to have a 1000px wide Div in the center of the page with the content.
I want it all to stay in the middle when I resize the browser-window.
How?
This worked for me to align vertically center. I can’t use half height bacuase i dont know the height. So i used percentage
Saw this one recently:
This one is awesome for image tag.
this one works.. but you have to put a height in the container . that’s bad for me then..
my solution works in ie8
img{display: block; margin: 0 auto;}
Thank you, Matthew Buchanan! This is perfect — nice and clean, and works for varying image sizes. It works for me in Firefox, Chrome, Safari, Opera, and IE 8 and 9.
That’s a new one on me, and incredibly helpful. I wonder why it works?
This is awesome!
Hey Matthew it worked for me to center image both vertically and horizontally within a container. Thanks :)
Flawless! Thanks Matthew!
Bravo Matthew
and thank you.
Thank’s you Matthew, it works for me!
Thank you so much. This worked for me on Edge, Chrome and Firefox.
This is my idea for this
Thanks for the tutorial, I am trying to place a paragraph below the centralised image, but when I do this the paragraph remains at the top left hand side of the browser window.
Is there a way around this?
Based on Chris’ table approach, this worked great for me…
html, body, #wrapper {
height:100%;
width: 100%;
margin: 0;
padding: 0;
border: 0;
}
#wrapper td {
vertical-align: middle;
text-align: center;
}
.content {
width: 600px;
height: 400px;
background-color: red;
margin-left: auto;
margin-right: auto;
}
And the html…
<table id=”wrapper”>
<tr>
<td>
<div class=”content”>content…</div>
</td>
</tr>
</table>
Great article! I love your site, it’s bookmarked : )
I use your method of absolute centering in all my websites.
/* Make the height 100% */
#container ul li{
display:inline-block;
height:100%;
}
/* Then vertical-align the crap outta it */
#container ul li img{
vertical-align:middle;
}
/* NOTE: The images don’t have fixed sizes either :) This works in Firefox, been using it for a while so I must have cross browser tested it but can’t remember anymore. Give it go! */
Better and complete: style for a extensible box with title text and one image inside.
The position: relative and the width are not an obligation.
div.frameStretch {color: #061f4e; font-family: “mafonte”, Times New Roman, Times, serif; font-weight: normal; line-height: 1.35; text-align: justify; margin-right: auto; margin-left: auto; padding: 4px 8px; position: relative; top: 32px; width: 832px; overflow: hidden; border-width: 24px;
-moz-border-image: url(yourimageborder.png) 24 stretch; -moz-border-radius: 24px;
-webkit-border-image: url(yourimageborder2.png) 24 stretch; -webkit-border-radius: 24px;
-o-border-image: url(.yourimageborder.png) 24 stretch; -o-border-radius: 24px;
-ms-border-image: url(yourimageborder.png) 24 stretch; -ms-border-radius: 24px;
border-image: url(yourimageborder.png) 24 fill stretch; border-radius: 24px;
box-pack: center; box-shadow: 0px 1px 3px #122c53; text-shadow: -1px -1px 1px #cccccc, 1px 1px 1px #ffffff;}
/* — For the title in the box — */
div.frameStretch h2 {font-size: 36px; font-weight: bold; line-height: 24px; margin: 0; padding: 0 0 12px;}
/* — To one image inside, vertical align, at right of the box —*/
div.frameStretch img {list-style-position: inside; display: block; margin-top: auto; margin-right: -10px; margin-bottom: auto; padding: 8px 0 8px 8px; position: relative; right: 0; width: 240px; height: 238px; float: right; vertical-align: middle; border-style: none;}
That background image is simply a brilliant kiss technique.
Just wanted to drop a line, to say thanks a lot for this and so many other useful posts.
Hello,
it’s uses basically a how center images , and it can take a server timestamp to start with. The css can also be easily modified to one’s own likings.
http://www.phphunt.com/111/how-to-center-images
Hi Chris – or any genius out there, is there a “responsive” way of doing this? So when you resize your browser using “img{max-width:100%;height:auto}” for the images to resize, is there a way to center it inside a div both vertically and horizontally?
Thank you!
is this supported in IE 7, IE6?
Hi.. Thanks for the tut. I have been referring to css-tricks.com for a ling time for all sorts of different things, and as a developer, your site has been a great resource and helps speed up development of some of my projects. So big thumbs up! :)
thx !
You can also use the line-height method; just as you would to vertically align a single line of text.
Set the image’s container to a specific height and also give it a line-height with the same value as the height. Then give the image inside the container a display: inline-block or just inline.
If you also want the image to be centered, since it’s already got a display: inline, you can give the container a text-align: center which will make the image centered, too.
Blamo!
Thanks a lot, this worked!
.Absolute-Center {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
veeeeeeeeeeeeeeeeery nice
thank you. ;)
This help me a lot. Many thanks!
I always use:
img.center {
display: block;
margin-left: auto;
margin-right: auto;
}
Yes, Same to you. I think this is very nice.
img.center {
display: block;
margin-left: auto;
margin-right: auto;
}
Hardcore table emulator :D ( demo )
img.center {
display: block;
margin-left:auto;
margin-right:auto;
margin-top:2px;
margin-bottom: 2px;
padding: 2px;
}
This also will work
This one is genius. No messing up with container div. Thank you.
Here’s a LESS version of the inline technique I’ve been using. Edit the first height / width and let LESS do the rest!
Here’s an even simpler mixin version that I’ve used for just vertically centering elements.
Start with a default like so. In my case I set it to 120px.
Then you can adjust the height as needed.
.vertically-center(80px);
I have tried it over one of my project and found that if i use html5 doctype than your css background-image technique wont work in chrome. I have to remove the html5 doctype for working.
The HTML5 doctype is definitely not the problem. There is something else going on there.
Best practices Chris :)
These solution depends on ux, as my observation this xperience for under construction page. And your this technique is best practice for it:
One my calculated suggestion image size should 206px square because this will works responsively as well :)
Here is my technique for text content but not for lt ie8:
Check my demo, http://jsbin.com/rexuk/2/
Good thought bro :)
I have found that this CSS seems to do the job, it works when you replace the dimensions of the image class with percentages rather than pixels, it seems to work out the actual pixels itself, it’s quite simple:
.container {
text-align: center;
width: WHATEVER.px;
height: WHATEVER.px;
}
.image {
width: 80%;
height: 80%;
position: relative;
top: 10%
}
There is little math behind it, simply make the top percentage half of whatever you take off the height, in my case I subtracted 20% meaning the top position is 10%. I don’t know if it will work correctly for everybody’s desires, but I have had no problems with it since!
Check out this animated, scriptless gallery demo that applies these principles to images of varying sizes, without specifying size:
Scriptless css-only gallery
The best of the solutions are the simplest. Awesome. Thank you.
img {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.container{width:300px; border:1px solide #000; text-align:center; vertical-align:middle;}
.container:before{height:100%; display:inline-block; content:””; text-align:center; vertical-align:middle;}
it will work in all browser.
.image-center {
text-align: center;
}
.image-center:before {
content:”;
width: 1px;
height: 100%;
}
.image-center:before,
.image-center img {
display: inline-block;
vertical-align: top;
margin-right: -5px;
}
Nice post.
CSS background-image Technique: works for me.
I am designing i card for a library and i have to set logo in background in i-card.
Many many thanks for posting.
I have a image div on right and text div on left, i need text to
Align the text in the center of image section.
try to guide me how to do
It is a really great article about image and text alignment in the horizontal and vertical middle.
Here I am given small example for image and text align middle in div
Just use change width and height, remove ‘bottom’ or whatever to stick it to a side.
Update your answer or get a job at McDonalds.
#element {
position: absolute;
margin: auto;
bottom: 0;
left: 0;
top: 0;
right: 0;
width: 960px;
height: 450px;
}
*{padding:0; margin:0;}
html{ height: 100vh; width:100%; display:table;}
body{ height: 100vh; width:100%; display: table-cell; vertical-align: middle; text-align: center; font-family: ‘Roboto Condensed’, sans-serif; font-weight: 400; font-size: 20px;}
Text
If you don’t know the dimensions of the image the following CSS might be a better solution:
Or if you only need to center it horizontal:
Best solution ever.
Hey!
how about transform???
Thank you! The image alignment trick did work for me :) I am so happy!
with javascript:
oops sorry the 600 and 800 should be replaced with your div’s width and height
and here
I meant //replace body with parent div