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. 

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

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 796 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.

โค๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ•น

Random Quote
Treasure every moment that you have and treasure it more because you shared it with someone special, special enough to spend your time...and remember that time waits for no one.
Unknown