0

I have a fixed element without width and height,just contain some flexible text. When I want to center there text vertically and horizontally, I failed. How to achieve? Thanks :)

There is a link be used to reference.

body{
  background: #f63;
}
div{
  background: #069;
  color:#fff;
  position: fixed;
  top: 0;
  left: 50%;

}
Community
  • 1
  • 1
Todd Mark
  • 1,847
  • 13
  • 25

2 Answers2

0

Try this

div {
  background: #069;
  color: #fff;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Felix A J
  • 6,300
  • 2
  • 27
  • 46
0

This can easily achieve by css3 transform properties

**vertically Middle**

div{
  background: #069;
  color:#fff;
  position: fixed;
  top: 50%;
  transform:translateY(-50%);
}

For Horizontal you need width or else

div{
      background: #069;
      color:#fff;
      position: fixed;
      top: 50%;
      transform:translateY(-50%);
      transform:translateX(-50%);
     //comes horizontally center as per elements width
      left:50%;
      right:0;
    }
Vivek Vikranth
  • 3,265
  • 2
  • 21
  • 34