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

怎麼設定css字體單行居中

藏色散人
發布: 2023-01-04 09:38:18
原創
3146 人瀏覽過

設定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中文網其他相關文章!

相關標籤:
css
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板