This question already has answers here:
How to center multiple inline-block elements with CSS?
                                
                                    (5个答案)
                                
                        
                                3年前关闭。
            
                    
我需要将一个没有宽度的div元素居中(它从内部div获得宽度。

<html>
<head>
    <title></title>
    <style type="text/css">
    .outer {
        padding: 10px;
        /*margin: auto; (doesn´t work)*/
        background-color: #000;
        display: inline-block;
    }
    .inner {
        width: 400px;
        height: 400px;
        text-align: center;
        background-color: #CCC;
    }
    </style>
</head>
<body>

    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
</html>


任何想法如何解决此问题而无需设置宽度?

最佳答案

您可以在身上或外部

这将使div在水平和垂直方向上居中对齐。

position: absolute;
top:50%;
left: 50%;
transform: translate3d(-50%,-50%, 0);

关于html - 没有宽度的元素的CSS边距,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36973837/

10-11 22:12
查看更多