[html] How to center a "position: absolute" element

Here is easy and best solution for center element with “position: absolute”

_x000D_
_x000D_
 body,html{_x000D_
  min-height:100%;_x000D_
 }_x000D_
 _x000D_
 div.center{_x000D_
 width:200px;_x000D_
 left:50%;_x000D_
 margin-left:-100px;/*this is 50% value for width of the element*/_x000D_
 position:absolute;_x000D_
 background:#ddd;_x000D_
 border:1px solid #999;_x000D_
 height:100px;_x000D_
 text-align:center_x000D_
 }_x000D_
 _x000D_
 
_x000D_
<style>_x000D_
_x000D_
</style>_x000D_
_x000D_
<body>_x000D_
 <div class='center'>_x000D_
  should be centered verticaly_x000D_
 </div>_x000D_
</body>
_x000D_
_x000D_
_x000D_

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to css-position

How does the "position: sticky;" property work? How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3? CSS z-index not working (position absolute) Relative div height How can I make the contents of a fixed element scrollable only when it exceeds the height of the viewport? How to position the Button exactly in CSS How to make fixed header table inside scrollable div? Absolute positioning ignoring padding of parent Have a fixed position div that needs to scroll if content overflows How to make div fixed after you scroll to that div?

Examples related to centering

Flutter: Trying to bottom-center an item in a Column, but it keeps left-aligning Centering in CSS Grid Bootstrap 4 Center Vertical and Horizontal Alignment Center a column using Twitter Bootstrap 3 How to horizontally align ul to center of div? How to center a <p> element inside a <div> container? Using margin:auto to vertically-align a div How to center body on a page? How to center a "position: absolute" element How to center a button within a div?