問題:大盒子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; }