Read more

How to horizontally center absolute positioned container with CSS

Deleted user #6
March 27, 2013Software engineer

Note: We have card with all CSS centering options. You probably want to head over there and get an overview over what techniques are available for your use case and browser requirements.


Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Horizontally centering a static element in CSS is normally handled by setting the left and right margins to auto, for example:

// SASS
$container_width: 100px
.container
  width: $container_width
  margin: 0 auto

However, this won’t work on an absolutely positioned element. But there is a solution:

// SASS
$container_width: 100px
.container
  width: $container_width
  position: absolute
  left: 50%
  margin-left: floor($container_width / 2) * -1
Posted to makandra dev (2013-03-27 17:24)