CSS center display inline block?

后端 未结 9 1694
误落风尘
误落风尘 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:16

    You don't need to use "display: table". The reason your margin: 0 auto centering attempt doesn't work is because you didn't specify a width.

    This will work just fine:

    .wrap {
        background: #aaa;
        margin: 0 auto;
        width: some width in pixels since it's the container;
    }
    

    You don't need to specify display: block since that div will be block by default. You can also probably lose the overflow: hidden.

    0 讨论(0)
  • 2020-11-22 09:20

    I just changed 2 parameters:

    .wrap {
        display: block;
        width:661px;
    }

    Live Demo

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题