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
Page Views

This page has been viewed 330 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
Our brains are wired to find things we are looking for - if your always cynical or waiting for things to go wrong, then your life will reflect that. On the other hand, having a positive outlook on life will bring you joy and provide you with inspiration when you least expect it ☀️🍉🌴
Unknown