August 20, 200917 yr Hi all, I'm sure there is already a thread regarding vertical centring but I can't seem to find it! I need to vertically centre an image inside a div. I know the size of the image but not the div. so: <div id="var_size"> <img src="images/img_1.jpg" width="400" height="210" alt="image_1" /> </div> Can anyone help?
August 20, 200917 yr padding:auto 0; auto sets the top and bottom pad, while 0 is the left and right you could also try margin:auto 0;
August 20, 200917 yr Author padding:auto 0; auto sets the top and bottom pad, while 0 is the left and right you could also try margin:auto 0; That would be a logical way to do it as it works with horizontal centring but unfortunately vertical centring doesn't work that way!
August 20, 200917 yr Try the position: absolute method of centering:- <div style="position: relative; width: 530px; height: 200px; background-color: #c0c0c0; margin: auto;"> <img style="position: absolute; top: 50%; left: 50%; width: 300px; height: 100px; margin-top: -50px; margin-left: -150px; src="image.jpg" alt="description"> </div> The div is the container which must have a height and position: relative. The image has height: 100px; top: 50%; margin-top: -50px; where the margin-top is always negative half the image height. The code is for centering both ways.
August 20, 200917 yr Author Try the position: absolute method of centering:- <div style="position: relative; width: 530px; height: 200px; background-color: #c0c0c0; margin: auto;"> <img style="position: absolute; top: 50%; left: 50%; width: 300px; height: 100px; margin-top: -50px; margin-left: -150px; src="image.jpg" alt="description"> </div> The div is the container which must have a height and position: relative. The image has height: 100px; top: 50%; margin-top: -50px; where the margin-top is always negative half the image height. The code is for centering both ways. Doesn't that require me to know the size of the div? That's the issue I'm having; I know the size of the image but the size of the containing div varies!
August 20, 200917 yr Does it have to be an image in the html? as if it could be a background image you could do this.... #divimage { background-image: url(image.jpg); background-repeat: no-repeat; background-position: center center; } Although looking at whickham's code all you will need to know is the size of the image, as all you are doing is deducting half the size of the width and height of the image from the top and left margins to make sure that it is dead center. break down of Whickhams code within the image tag (naughty naught for inline styling, but nice bit of coding ) : position: absolute; - positions the image to set coordinates within the relative positioned div top: 50%; - percentage from the top of the div to place the image left: 50%; - percentage from the left of the div to place the image width: 300px; - total image width height: 100px; - total image size margin-top: -50px; - half the height of the image as a negative margin so that the image will be set spot on vertically center margin-left: -150px; -half the width of the image as a negative margin so that the image will be set spot of horizontally center.
August 20, 200917 yr I didn't realise that you don't need a height for the containing div, but if the image is the only item in a container, the container will be the same height as the image so the image won't need centering! Usually there is some kind of container. I often use inline styles when replying to questions as it easier for me and shows what's required more clearly; I expect people to know about stylesheets.
August 20, 200917 yr Hey, Its like Dizi said, do you really need the img on your HTML our can you embedd it to CSS? Regards, Flex
August 20, 200917 yr I didn't realise that you don't need a height for the containing div, but if the image is the only item in a container, the container will be the same height as the image so the image won't need centering! Usually there is some kind of container. In theory you shouldn't need a height...in certain browsers *cough IE cough* that might not be the case and a height might be required...but in theory so long as the div doesn't rely on floated content to give the height it should work
August 21, 200917 yr Author Does it have to be an image in the html? as if it could be a background image you could do this.... #divimage { background-image: url(image.jpg); background-repeat: no-repeat; background-position: center center; } It would be better if I could get the image in the HTML as a) it's not a layout image and b)it'll be dynamically outputted, I imagine I could use inline CSS (as that's still in the HTML so it can easily be pulled from a database) but as you've already mentioned that's not good practice. Although looking at whickham's code all you will need to know is the size of the image, as all you are doing is deducting half the size of the width and height of the image from the top and left margins to make sure that it is dead center. break down of Whickhams code within the image tag (naughty naught for inline styling, but nice bit of coding ) : position: absolute; - positions the image to set coordinates within the relative positioned div top: 50%; - percentage from the top of the div to place the image left: 50%; - percentage from the left of the div to place the image width: 300px; - total image width height: 100px; - total image size margin-top: -50px; - half the height of the image as a negative margin so that the image will be set spot on vertically center margin-left: -150px; -half the width of the image as a negative margin so that the image will be set spot of horizontally center. I have tried this and it doesn't seem to work for me, perhaps as you say it's because I've using floated content! I've somewhat realised my problem is a little odd without me giving you a bit more context: I didn't realise that you don't need a height for the containing div, but if the image is the only item in a container, the container will be the same height as the image so the image won't need centering! Usually there is some kind of container. I basically have two columns within a DIV, the left column has variable textual content so the containing DIV has an unknown height, this column is floated left so the image below it becomes a second column on the right. so: <div id="container"> <div id="left_column"> <p>Variable textual content</p> </div> <img src="image.jpg" /> </div> Thinking about it this might help explain what I mean, the first one is where I'm at, the second is where I want to be:
August 21, 200917 yr Have a look at this:- http://www.wickham43.com/forumposts/scaz090821.html The divs have no height. The left_column is not floated in my example. If the left_column div has a float, Firefox assumes that the container has no height, so a div with clear: both has to be added at the end of the #container div.
August 21, 200917 yr Author Have a look at this:- http://www.wickham43.com/forumposts/scaz090821.html The divs have no height. The left_column is not floated in my example. If the left_column div has a float, Firefox assumes that the container has no height, so a div with clear: both has to be added at the end of the #container div. Awesome, that's done it for me, I wasn't aware floated elements stop the height of its container from being calculated! Thanks very much for you help!!
Create an account or sign in to comment