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 do I centre a div horizontally and vertically using css styles

Featured Replies

Hi I was wondering how do you centre a div using css style horizontally and vertically ? I have been trying and trying I can get it one way but not the other any help ?

If that doesn't work, then your next option is to use negative margins and absolute positioning. For example, if you had a 480 x 230 box, you would use the following code:

 

#center {
   width:480px;
   height:230px;
   position:absolute;
   left:50%;
   top:50%;
   margin-left:-240px;
   margin-top:-115px;
 }

 

That would then be centralised, the negative margins always have to be half of the total width or height.

  • Author

If that doesn't work, then your next option is to use negative margins and absolute positioning. For example, if you had a 480 x 230 box, you would use the following code:

 

#center {
   width:480px;
   height:230px;
   position:absolute;
   left:50%;
   top:50%;
   margin-left:-240px;
   margin-top:-115px;
 }

 

That would then be centralised, the negative margins always have to be half of the total width or height.

 

Thank you Traxor :)

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.