CSS3 文本效果
CSS3中包含幾個(gè)新的文本特征。
在本章中您將了解以下文本屬性:
text-shadow
box-shadow
text-overflow
word-wrap
word-break
瀏覽器支持
CSS3的文本陰影
CSS3中,text-shadow屬性適用于文本陰影。
您指定了水平陰影,垂直陰影,模糊的距離,以及陰影的顏色:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> h1 { text-shadow: 5px 5px 5px #FF0000; } </style> </head> <body> <h1>Text-shadow effect!</h1> <p><b>注意:</b> Internet Explorer 9 以及更早版本的瀏覽器不支持 text-shadow屬性.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
CSS3 box-shadow屬性
CSS3中CSS3 box-shadow屬性適用于盒子陰影
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div { width:300px; height:100px; background-color:yellow; box-shadow: 10px 10px 5px #888888; } </style> </head> <body> <div></div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
接下來(lái)給陰影添加顏色
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div { width: 300px; height: 100px; padding: 15px; background-color: yellow; box-shadow: 10px 10px grey; } </style> </head> <body> <div>This is a div element with a box-shadow</div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
接下來(lái)給陰影添加一個(gè)模糊效果
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div { width: 300px; height: 100px; padding: 15px; background-color: yellow; box-shadow: 10px 10px 5px grey; } </style> </head> <body> <div>This is a div element with a box-shadow</div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
你也可以在::befor和::after兩個(gè)偽元素中添加陰影效果
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> #boxshadow { position: relative; -moz-box-shadow: 1px 2px 4px rgba(0, 0, 0,0.5); -webkit-box-shadow: 1px 2px 4px rgba(0, 0, 0, .5); box-shadow: 1px 2px 4px rgba(0, 0, 0, .5); padding: 10px; background: white; } /* Make the image fit the box */ #boxshadow img { width: 100%; border: 1px solid #8a4419; border-style: inset; } #boxshadow::after { content: ''; position: absolute; z-index: -1; /* hide shadow behind image */ -webkit-box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3); -moz-box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3); box-shadow: 0 15px 20px rgba(0, 0, 0, 0.3); width: 70%; left: 15%; /* one half of the remaining 30% */ height: 100px; bottom: 0; } </style> </head> <body> <div id="boxshadow"> <img src="rock600x400.jpg" alt="Norway" width="600" height="400"> </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
陰影的一個(gè)使用特例是卡片效果
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.card { width: 250px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); text-align: center; } div.header { background-color: #4CAF50; color: white; padding: 10px; font-size: 40px; } div.container { padding: 10px; } </style> </head> <body> <h2>卡片</h2> <p>box-shadow 屬性用來(lái)可以創(chuàng)建紙質(zhì)樣式卡片:</p> <div class="card"> <div class="header"> <h1>1</h1> </div> <div class="container"> <p>January 1, 2016</p> </div> </div> </body> </html>
文字卡片
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.polaroid { width: 250px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); text-align: center; } div.container { padding: 10px; } </style> </head> <body> <h2> 卡片</h2> <p>box-shadow屬性可以用來(lái)創(chuàng)建紙質(zhì)樣式卡片:</p> <div class="polaroid"> <img src="rock600x400.jpg" alt="Norway" style="width:100%"> <div class="container"> <p>Hardanger, Norway</p> </div> </div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
CSS3 Text Overflow屬性
CSS3文本溢出屬性指定應(yīng)向用戶如何顯示溢出內(nèi)容
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> div.test { white-space:nowrap; width:12em; overflow:hidden; border:1px solid #000000; } </style> </head> <body> <p>以下 div 容器內(nèi)的文本無(wú)法完全顯示,可以看到它被裁剪了。</p> <p>div 使用 "text-overflow:ellipsis":</p> <div class="test" style="text-overflow:ellipsis;">This is some long text that will not fit in the box</div> <p>div 使用 "text-overflow:clip":</p> <div class="test" style="text-overflow:clip;">This is some long text that will not fit in the box</div> <p>div 使用自定義字符串 "text-overflow: >>"(只在 Firefox 瀏覽器下有效):</p> <div class="test" style="text-overflow:'>>';">This is some long text that will not fit in the box</div> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
CSS3的換行
如果某個(gè)單詞太長(zhǎng),不適合在一個(gè)區(qū)域內(nèi),它擴(kuò)展到外面:
CSS3中,自動(dòng)換行屬性允許您強(qiáng)制文本換行 - 即使這意味著分裂它中間的一個(gè)字:
CSS代碼如下:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.test { width:11em; border:1px solid #000000; word-wrap:break-word; } </style> </head> <body> <p class="test"> This paragraph contains a very long word: thisisaveryveryveryveryveryverylongword. The long word will break and wrap to the next line.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
CSS3 Word Breaking
CSS3 Word Breaking屬性指定換行規(guī)則:
CSS代碼如下:
實(shí)例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> <style> p.test1 { width:9em; border:1px solid #000000; word-break:keep-all; } p.test2 { width:9em; border:1px solid #000000; word-break:break-all; } </style> </head> <body> <p class="test1"> This paragraph contains some text. This line will-break-at-hyphenates.</p> <p class="test2"> This paragraph contains some text: The lines will break at any character.</p> <p><b>注意:</b> word-break 屬性不兼容 Opera.</p> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
屬性 | 描述 | CSS |
---|---|---|
hanging-punctuation | 規(guī)定標(biāo)點(diǎn)字符是否位于線框之外。 | 3 |
punctuation-trim | 規(guī)定是否對(duì)標(biāo)點(diǎn)字符進(jìn)行修剪。 | 3 |
text-align-last | 設(shè)置如何對(duì)齊最后一行或緊挨著強(qiáng)制換行符之前的行。 | 3 |
text-emphasis | 向元素的文本應(yīng)用重點(diǎn)標(biāo)記以及重點(diǎn)標(biāo)記的前景色。 | 3 |
text-justify | 規(guī)定當(dāng) text-align 設(shè)置為 "justify" 時(shí)所使用的對(duì)齊方法。 | 3 |
text-outline | 規(guī)定文本的輪廓。 | 3 |
text-overflow | 規(guī)定當(dāng)文本溢出包含元素時(shí)發(fā)生的事情。 | 3 |
text-shadow | 向文本添加陰影。 | 3 |
text-wrap | 規(guī)定文本的換行規(guī)則。 | 3 |
word-break | 規(guī)定非中日韓文本的換行規(guī)則。 | 3 |
word-wrap | 允許對(duì)長(zhǎng)的不可分割的單詞進(jìn)行分割并換行到下一行。 | 3 |