CSS center display inline block?

后端 未结 9 1864
误落风尘
误落风尘 2020-11-22 08:25

I have a working code here: http://jsfiddle.net/WVm5d/ (you might need to make the result window bigger to see the align center effect)

Question

9条回答
  •  面向向阳花
    2020-11-22 09:22

    Similar to @vijayscode's answer, this will horizontally center an inline-block element (but without needing to modify its parent's styles as well):

      display: inline-block;
      position: relative;
      left: 50%; // Move the element to the left by 50% of the container's width
      transform: translateX(-50%); // Calculates 50% of the element's width and moves it by that amount across the X-axis to the left
    

提交回复
热议问题