-4

I'm trying to center a div element, but when I use this:

position: relative;
float: right;
right: 50%;
margin: 0 auto;

I get the following:

enter image description here

It just centers the right edge of the element and everything else goes to left.

Any ideas?

Chris Spittles
  • 15,023
  • 10
  • 61
  • 85
Einaras
  • 21
  • 4
  • 2
    Welcome to stackoverflow. Please take the time to read "[How to Ask](http://stackoverflow.com/help/how-to-ask)". – 099 Aug 25 '15 at 14:39

2 Answers2

3

Just use:

margin-left: auto;
margin-right: auto;

Nothing else.

Jeroen de Jong
  • 337
  • 1
  • 6
0

You can try below code:

For Horizontal:

margin:0 auto;

For Horizontal and vertical:

position:absolute;
top:50%;
left:50%; 
tranform:translate(-50%, -50%);
Pradeep Pansari
  • 1,287
  • 6
  • 10