純CSS文本陰影製作效果實現

來源:互聯網
上載者:User
css   CSS2中的text-shadow屬效能夠很容易的給web頁面中的文本添加陰影,但是到目前為止只有OS X的Safari瀏覽器支援它,今天,我們將為其他瀏覽器建立CSS文本陰影製作效果,包括Firefox。
討論了多年的text-shadow屬性可以讓你控制頁面元素陰影的顏色、位移量及模糊度,儘管其還未被廣泛支援,但是某些設計師已經開始決定在任意地方使用CSS text-shadows屬性了。儘管這隻是為很少數量的使用者增強性設計。
CSS Text-Shadows Safari實現
如果你使用的是Safari,你將可以看到在白色的背景上有陰影的白色文本:
This is white text, on a white background. Yet in Safari, you can read this, because of the black text-shadow.
如果你不是使用Safari,一下是效果圖:

瀏覽器通用CSS文本陰影
Firefox是個偉大的瀏覽器,但是它不支援以上的效果,所以我決定用CSS實現一個類似的效果,雖然沒有前面你看到的text-shadow屬性效果完美,但是該方法適用於更大範圍的瀏覽器,包括Safari。
This is white text, on a white background. Yet with CSS Drop Shadows, you can read this, because of the black text-shadow. HTML代碼
要添加陰影製作效果,我們在我們的例子段落中建立了個title屬性內容是需要投影的文本的內容一致。因為在該方法中我們重複了文本,所以該方法更適合在標題或文本比較短的段落中,而不適合整個頁面的陰影製作效果。 
<p class="shadow" title="This is white text, on a white background. Yet with CSS Drop Shadows, you can read this, because of the black text-shadow."><span>This is white text, on a white background. Yet with CSS Drop Shadows, you can read this, because of the black text-shadow.</span></p>我們在段落中增強了個<span>標籤用來控制正常的文本的位置以將其與產生的陰影區分開。
The CSS
CSS :before 虛擬元素用來從段落的title屬性中產生陰影,而絕對(absolute)定位用來將正常文本放置於陰影文本之上。
.shadow  { position:relative; display:block; color:#fff; }.shadow span { position:absolute; display:block; top:0px;  }.shadow:
before { display:block; padding:1px; content: attr(title); color:#666;  } 你可以使用position:absolute控制在相對定位(position:relative)的元素內部的元素的位置。該技能使得我們可以控制正常文本和陰影文本的放置,而又可以在原來的文字資料流程中使用該元素。
你可以在.shadow中控制文本及陰影的背景顏色、字型等屬性,而可以在.shadow:before中使用padding屬性控制陰影的位移值,使用color設定陰影的顏色等。

相關文章

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.