I have a div 200 x 200 px. I want to place a 50 x 50 px image right in the middle of the div.
How can it be done?
I am able to get it centered horizontally by using text-align: center
for the div. But vertical alignment is the issue..
I have a div 200 x 200 px. I want to place a 50 x 50 px image right in the middle of the div.
How can it be done?
I am able to get it centered horizontally by using text-align: center
for the div. But vertical alignment is the issue..
I love jumping on old bandwagons!
Here's a 2015 update to this answer. I started using CSS3
transform
to do my dirty work for positioning. This allows you to not have to make any extra HTML, you don't have to do math (finding half-widths of things) you can use it on any element!Here's an example (with fiddle at the end). Your HTML:
With accompanying CSS:
What I do a lot these days is I will give a class to things I want centered and just re-use that class every time. For example:
css
This way, I will always be able to center something in it's container. You just have to make sure that the thing you want centered is in a container that has a
position
defined.Here's a fiddle
BTW: This works for centering BIGGER divs inside SMALLER divs as well.
In CSS do it as:
I've been trying to get an image to be centered vertically and horizontally within a circle shape using hmtl and css.
After combining several points from this thread, here's what I came up with: jsFiddle
Here's another example of this within a three column layout: jsFiddle
CSS:
HTML:
thanks to everyone else for the clues.
I used this method
Typically, I'll set the
line-height
to be 200px. Usually does the trick.This works for me :