CSS3字型與文字效果的範例程式碼分享

來源:互聯網
上載者:User

CSS3允許我們使用自訂字型

也多了一些不錯的文字效果

自訂字型

使用自訂字型需要使用@font-face規則
當然首先你要有自訂文字檔案

<p class="demo">Payen S.Tsung</p>
@font-face {    font-family: myDIYfont; /*自訂字型名*/    src: url('Ginga.ttf'); /*字型檔*/}.demo {    font-family: myDIYfont;}

如果我們還有粗體格式的字型檔
可以這樣用

@font-face {    font-family: myDIYfont;    src: url('Ginga_bold.ttf');    font-weight: bold;}.demo {    font-family: myDIYfont;}

@font-face各個瀏覽器有相容問題
我們可以在src屬性後添加format格式像這樣src: url('Ginga.ttf') format('truetype');
瀏覽器安全色性:

  • url(‘font.eot?#iefix’) format(‘embedded-opentype’), /* IE6-IE8 */

  • url(‘ont.woff’) format(‘woff’), /* chrome、firefox */

  • url(‘font.ttf’) format(‘truetype’), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/

  • url(‘font.svg#fontname’) format(‘svg’); /* iOS 4.1- */

文字效果

CSS3加了很多文字效果但是都不常用
介紹兩個

文本邊界換行word-wrap

<p class="demo">honorificabilitudinitatibus</p>
.demo {    width: 100px;    height: 100px;    border: 1px solid black;}

瀏覽器認為這是一個單詞,所以沒有換行
對於所有非中日韓文本的斷行規則我們可以使用word-wrap強制換行

.demo {    width: 100px;    height: 100px;    border: 1px solid black;    word-break: keep-all;}

屬性值還有normal(預設)表示瀏覽器預設規則,keep-all表示空格和連字號換行
其實兩個是一樣的

文本陰影text-shadow

這個屬性和box-shadow很像但還不太一樣
只不過它是文本的陰影而不是盒陰影
同樣很耗效能要少用
屬性值有水平陰影距離,垂直陰影距離,模糊半徑(選填),顏色(選填)
也可以定義多個陰影
區別與box-shadow它沒有陰影大小和投影模式

.demo {    width: 100px;    height: 100px;    border: 1px solid black;    word-break: break-all;    text-shadow: 10px 10px 2px red,                 20px 20px 3px blue;    }

通過這個屬性我們可以讓文本看起來更酷炫

<p class="demo">this is a text</p>
.demo {    text-shadow: 2px 2px 5px;}

嗯沒有了就這些~

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.