4

I'm looking for some syntax like this:

.centered{
    left: calc(50% - current-width/2)
}

Basically, a way to reference the current-width of the target element so that I can have a flexible-width item centered. Does this exist?

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • `left: 50%; transform: translateX(-50%);` https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translateX – Michael Coker Jul 18 '17 at 17:50

1 Answers1

6

One answer is to move it left and then translate it:

.centered {
    position: absolute;
    left: 50%;
    transform: translate(-50%);
}

But a better way is to use Flexbox on the parent element:

.centeredParent {
    display: flex;
    justify-content: center;
}
Rene van der Lende
  • 4,992
  • 2
  • 14
  • 25
i7nvd
  • 1,318
  • 2
  • 10
  • 24