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.

Centering an image inside a div?

Featured Replies

Hey guys,

 

Having a bit of an issue - how can I center an image inside a div? I'm talking so it sits exactly in the middle both horizontally and vertically...?

 

Any help much appreciated :)

#container { position: relative; width: 400px; height: 300px; }

#image { width: 200px; height: 100px; position: absolute; top: 50%; margin-top: -50px; left: 50%; margin-left: -100px; }

 

<div id="container">

<img id="image" src="image.jpg" alt="description">

</div>

 

The widths and heights are whatever you want but the position: relative, position: absolute, left: 50%; top: 50% and margins are critical; negative margins are always half the width and height of the image.

  • Author

Ah that worked perfect! Legend :)

No, you don't have to state the size the image with width and height if it's already the correct size, but the negative margins must be correct for the image size, either for the original image size or the size as defined by width and height.

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.