首頁  >  文章  >  web前端  >  怎麼設定css字體單行居中

怎麼設定css字體單行居中

藏色散人
藏色散人原創
2020-12-30 09:28:023010瀏覽

設定css字體單行居中的方法:先建立一個div;然後在div裡寫上一個p標籤;最後透過設定「text-align: center;」屬性實作單行居中即可。

怎麼設定css字體單行居中

本教學操作環境:Dell G3電腦、Windows7系統、HTML5&&CSS3版本。

css設定字體單行居中

1、首先為了方便觀察,先建立一個div

<style>
    .app{
        width: 200px;
        height: 100px;
        border: 1px solid skyblue;
    }
</style>
<div class="app">
    
</div>

怎麼設定css字體單行居中

2、然後在div裡寫上一個p標籤,並設定它text-align: center;屬性實作單行居中。

<style>
    .app{
        width: 200px;
        height: 100px;
        border: 1px solid skyblue;
    }
    .app > p{
        text-align: center;
        overflow: hidden; 
        text-overflow:ellipsis;  
        white-space: nowrap;
    }
</style>
<div class="app">
    <p>Hello World!</p>
</div>

怎麼設定css字體單行居中

3、若是span之類的行內元素,可以為它的父級元素添加text-align: center;屬性

<style>
    .app{
        width: 200px;
        height: 100px;
        text-align: center;
        border: 1px solid skyblue;
    }
</style>
<div class="app">
    <span>Hello World!</span>
</div>

4、單行文字實作垂直居中,設定line-height和父級元素的高度相同即可。

<style>
    .app{
        width: 200px;
        height: 100px;
        text-align: center;
        border: 1px solid skyblue;
    }
    .app > span{
        line-height: 100px;
    }
</style>
<div class="app">
    <span>Hello World!</span>
</div>

怎麼設定css字體單行居中

推薦:《css影片教學

以上是怎麼設定css字體單行居中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn