center something with css not using flex

This is how you can center something in another element if you dont want to use the flex method

You will need the relative position on the outer element, and the absolute on the center one unless you want it to be centered across the whole screen. 

Demo

CSS

.center {
  top : 50%;
  left : 50%;
  transform : translate(-50%,-50%);
  position:absolute;
}
.center-test-wrap {
  position:relative;
  width:400px;
  height:400px;
  border-radius:5px;
  background:teal;
  color:#FFF;
}

HTML

<div class='center-test-wrap'>
  <div class='center'>
  This should be in the center.
  </div>
</div>
This should be in the center.

Other Items in css
Page Views

This page has been viewed 169 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹