8

I'm trying to center horizontally an element: jsfiddle

<div id="parent">
    <div id="child">
</div>

On the child I have now the following css which doesn't center:

transform: translateX(50%);

Unfortunately the 50% is taken from the #child and not the parent. Is there a way to center this element using transforms (I know I can center it using, for example 'left')?

Here is the full listing of css

#parent {
    width: 300px
    height: 100px;
    background-color: black;
    border: 1px solid grey;
}
#child {
    height: 100px;
    width: 20px;
    -webkit-transform: translateX(50%);
    background-color:red;
}
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333

4 Answers4

24

You were missing a position relative on your parent, position absolute on the child and the left and top values to help offset the child.

DEMO http://jsfiddle.net/nA355/6/

#parent {
    width: 300px;
    height: 100px;
    background-color: black;
    border: 1px solid grey;
    position: relative;
}
#child {
    position: absolute;
    height: 100px;
    width: 20px;
    left: 50%;
    top: 50%;
    -webkit-transform: translateX(-50%) translateY(-50%);
    -moz-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
    background-color:red;
}
Erik Rybalkin
  • 1,143
  • 12
  • 21
2ne
  • 5,766
  • 9
  • 30
  • 51
  • In my situation I want to move #child around using transform. Mixing absolute position and transform makes it complicated. For example, given your solution, how would you move the child 10px to the left ? Thats why I was looking for a solution without absolute positioning. – Jeanluca Scaljeri Dec 12 '13 at 09:40
  • margin-left: -10px; ? – 2ne Dec 12 '13 at 12:54
3

You can do it by display flex and justify-content is center.

Example: http://jsfiddle.net/ugw9o3qp/1/

#parent {
    display: flex;
    justify-content: center;
    width: 300px;
    height: 100px;
    background-color: black;
    border: 1px solid grey;
}
#child {
    height: 100px;
    width: 20px;
    background-color:red;
}
Akegvd
  • 91
  • 5
1

If you don't want to set the width of child-element

#child {
    display: flex;
    justify-content: center;
}
user1891758
  • 379
  • 4
  • 5
0

I use this technique to horizontally align some elements in situations where "text-align:center;" does not work correctly.

 position:relative; 
 left:50%;
 transform:translateX(-??em) /*or -??px or other unit of measure */

... "??" has to be tweaked depending on how wide the element is. I know it's kinda hacky, but seems to work on the screens I've tried it on so far.

In your fiddle, applying this technique, I get:

#parent {
    width: 300px
    height: 100px;
    background-color: black;
    border: 1px solid grey;
}
#child {
    position:relative;
    height: 100px;
    width: 20px;
    left:50%;
    transform: translateX(-10px); /*or  transform: translateX(-50%);*/
    -webkit-transform: translateX(-10px);/*or  -webkit-transform: translateX(-50%); */
    -ms-transform: translateX(-10px);/*or -ms-transform: translateX(-50%);*/
    background-color:red;
}

which exactly centers the child div horizontally.

whitebeard
  • 1,071
  • 14
  • 19