讓盒子里的文字居中的幾種方法


問題:大盒子div下面有一段小盒子span標簽包含的文字,怎么使其居中顯示?

<body>
    <div class="box">
        <span>我是一段文字,怎么讓我居中?</span>
    </div>
</body>

第一種方法大盒子text-align: center

.box{
            width: 300px;
            height: 300px;
            background: rgb(172, 143, 143);
            text-align: center;
}

第二種方法大盒子box 用 padding-left/padding-right,同時調整大盒子寬度,使大盒子寬度不變

.box{
            width: 250px;
            height: 300px;
            background: rgb(172, 143, 143);
            padding-left: 50px;
}

注:50px是隨便取得值,具體的值需計算。

第三種方法,小盒子用定位移動位置

.box{
            width: 300px;
            height: 300px;
            background: rgb(172, 143, 143);
            position: relative;
        }
span{
            position: absolute;
            top: 0;
            left: 30px;
        } 

第四種方法小盒子使用margin

.box{
            width: 300px;
            height: 300px;
            background: rgb(172, 143, 143);
        } 
span{
            margin-left: 30px;
        } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM