Getting started
Backgrounds
Centering
Margins
Media queries
Floats
Typography
Flexbox
Cascading and specificity
Animations
Selectors
Colors
Box mode
Transiti
Length units
Pseudo-elements
Positioning
2D transforms
Tables
Browser support prefixes
Normalizing browser styles
Padding
Layout control
Filter property
Comments
Cursor styling
Box shadow
Custom properties, variables
Shapes for floats
Grid
Border
Functions
3D Transforms
Counters
Single element shapes
Opacity
Performance
Columns
Inline-block layout
Inheritance
CSS Image Sprites
Clipping and masking
List styles
Outlines
Structure and formatting of a css rule
Fragmentation
Overflow
CSS Object Model (CSSOM)
Feature queries
Stacking context
Internet Explorer hacks
Block formatting context
Vertical centering
Object Fit and Placement
Multiple columns
CSS design patterns
Contributors

Centering with Position absolute

suggest change

HTML:

<div class="wrapper">
  <img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>

CSS:

.wrapper{
   position:relative;
   height: 600px;
}
.wrapper img {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}

If you want to center other then images, then you must give height and width to that element.

HTML:

<div class="wrapper">
  <div class="child">
     make me center
  </div>
</div>

CSS:

.wrapper{
   position:relative;
   height: 600px;
}
.wrapper .child {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  width: 200px;
  height: 30px;
  border: 1px solid #f00;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents