Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to vertically align an image in css

Featured Replies

I need some help about vertical alignment of a picture only.

There is vertical-align in css but it vertically aligns an image according to text.

I only want to vertically align an image inside a div.

I have searched a lot but no useful information was found.

So please help me.

Just do this:

 

img.center {
 margin:0 auto;
}

<img class="center" sr="image.png" alt="image" />

img.center {
 margin:0 auto;
}

<img class="center" sr="image.png" alt="image" />

 

The will align it horizontally Traxor. It should be:

 

img.center {
 margin:auto 0;
}

<img class="center" sr="image.png" alt="image" />

Do you need to define a set height to use auto on the vertical axis?

 

Another alternative, although not a great solution would be to do something like this

 

img.center{
position:relative;
top:50%;
margin-top: -123px /* insert a number that is half the height of the image */
}

 

that would allow for a variable height, although you may be able to use auto in conjucntion with a variable height, i just don't know :p

 

That seems to work in most browsers, but of course not Internet Explorer. So, we add an equal amount of font-size and use a filter to reset non-Internet Explorer browsers:

 

 

great trick! i've never actually seen that ie 'hack' before but i'll certainly find use for it. thanks :-)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.