How to horizontally center a floating element of a variable width?

前端 未结 8 699
萌比男神i
萌比男神i 2020-12-02 06:51

How to horizontally center a floating element of a variable width?

Edit: I already have this working using a containing div for the floating element and

8条回答
  •  北荒
    北荒 (楼主)
    2020-12-02 07:49

    This works better when the id = container (which is the outer div) and id = contained (which is the inner div). The problem with the highly recommended solution is that it results in some cases into an horizontal scrolling bar when the browser is trying to cater for the left: -50% attribute. There is a good reference for this solution

            #container {
                text-align: center;
            }
            #contained {
                text-align: left;
                display: inline-block;
            }
    

提交回复
热议问题