Absolute Centering a DIV with CSS Only

A
Published in: CSS

Use this snippet to position a div at the absolute center of your browser window, both horizontally and vertically. Replace the is-Fixed class with is-Responsive for responsive web design. The snippet was originally shared on CodePen.

<div class="Center-Container">
 <div class="Absolute-Center is-Fixed">
 </div>
</div>


<style>
/*
////////////////////////////////////////
Absolute Centering
////////////////////////////////////////
*/
.Absolute-Center {
 height: 50%; /* Set your own height: percents, ems, whatever! */
 width: 50%; /* Set your own width: percents, ems, whatever! */
 overflow: auto; /* Recommended in case content is larger than the container */
 margin: auto; /* Center the item vertically & horizontally */
 position: absolute; /* Break it out of the regular flow */
 top: 0; left: 0; bottom: 0; right: 0; /* Set the bounds in which to center it, relative to its parent/container */
 background-color: #000;
}

/* //////////////////////////////////////// */
/* Make sure our center blocks stay in their container! */
.Center-Container { position: relative; }

/* //////////////////////////////////////// */
/* Fixed floating element within viewport */
.Absolute-Center.is-Fixed {
 position: fixed;
 z-index: 999;
}

/* //////////////////////////////////////// */
/* Min/Max Width */
.Absolute-Center.is-Responsive {
 width: 60%;
 height: 60%;
 min-width: 200px;
 max-width: 400px;
 padding: 40px;
}
</style>
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: CSS

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube