`
RandyWei
  • 浏览: 65439 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

CSS渐变文本效果

 
阅读更多

Do you want to create fancy headings without rendering each heading with Photoshop? Here is a simple CSS trick to show you how to create gradient text effect with a PNG image (pure CSS, no Javascript or Flash). All you need is an empty<span> tag in the heading and apply the background image overlay using the CSSposition:absolute property. This trick has been tested on most browsers: Firefox, Safari, Opera, and even Internet Explorer 6. Continue to read this article to find out how.


View        Download

 

 

 

Benefits

  • This is pure CSS trick, no Javascript or Flash. It works on most browsers including IE6 (PNG hack required).
  • It is perfect for designing headings. You don't have to render each heading with Photoshop. This will save you time and bandwidth.
  • You can use on any web fonts and the font size remains scalable.

How does this work?

The trick is very simple. Basically we are just adding a 1px gradient PNG (with alpha transparency) over the text.

screenshot

The HTML markups

<h1><span></span>CSS Gradient Text</h1>

The CSS

The key point here is: h1 { position: relative } and h1 span { position: absolute }

h1 {
  font: bold 330%/100% "Lucida Grande";
  position: relative;
  color: #464646;
}
h1 span {
  background: url(gradient.png) repeat-x;
  position: absolute;
  display: block;
  width: 100%;
  height: 31px;
}

That's it! You are done. Click here to view my demo page.

Make it work on IE6

Since IE6 doesn't render PNG-24 properly, the following hack is required in order to display the transparent PNG (add anywhere in between the <head> tag):

<!--[if lt IE 7]>

<style>
h1 span {
  background: none;
  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='gradient.png', sizingMethod='scale');
}
</style>

<![endif]-->

This is why we hate IE 6!

jQuery prepend version (for semantic lovers)

If you don't want to have the empty <span> tag in the heading, you can use Javascript to prepend the <span> tag. Here is a sample using jQuery prepend method:

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
$(document).ready(function(){

  //prepend span tag to H1
  $("h1").prepend("<span></span>");
	
});
</script>

More samples

Want to make Web 2.0 glossy text?

screenshot

Literally, you can apply this trick on any solid background color (as long your gradient color is the same as your background color).

screenshot

screenshot

screenshot

screenshot

screenshot

screenshot

Pattern / Texture

You can also apply this trick with a tile background image. Look, here is an example of zebra pattern. So, be creative!

screenshot

分享到:
评论

相关推荐

    CSS渐变文本效果的两种方法比较

    是否想不用photoshop来创建一个带渐变的标题文字吗? 这里用一个简单的css技巧来向你展示如何仅仅使用css和png图片来制造这种效果.

    css实现动态阴影、蚀刻文本、渐变文本效果

    主要介绍了css实现动态阴影、蚀刻文本、渐变文本效果,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

    CSS Tips:段落每行渐变色文本效果的实现方法

    对于单行文本或单个词实现渐变填充效果并不是什么奇怪的事情,不过对于一个段落的每一行实现一个文本渐变填充的效果。估计还是会令很多人感到好奇。如果你是其中好奇的一员,请继续往下阅读,一探其中之究竟! 目标...

    jquery+css3渐变显示“更多”隐藏内容

    点击“more”显示更多隐藏文字内容,渐渐显示,在IE 等不支持CSS3浏览器下效果较差,建议在火狐浏览器下浏览,懒人之家推荐下载

    react-textgradient:具有SVG后备功能CSS文本渐变。 [未维护]

    尽可能使用CSS渐变(Chrome,Safari,iOS,Android)。 在Firefox上使用SVG作为后备。 文本保持为流畅文本(绝不替换为svg)。 字体大小,字体,粗细等……通常由CSS控制。 安装。 npm install react - ...

    css渐变色彩 省略标记 嵌入字体 文本阴影全面了解

    下面小编就为大家带来一篇css渐变色彩 省略标记 嵌入字体 文本阴影全面了解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    HTML5 SVG渐变填充文本特效.zip

    HTML5 SVG渐变填充文本特效.zip

    CSS教程:网页文本渐变

    这里用一个简单的css技巧来向你展示如何仅仅使用css和png图片来制造这种效果. 经测试这种方法适合大多数主流浏览器.当然, IE6需要一个支持透明PNG的Hack(值得庆幸的是微软正在极力的将用户的IE6自动升级到IE7, 延伸...

    CSS设计指南

    随后4 章介绍了页面布局、界面组件,css3 圆角、阴影、渐变、多背景等视觉设计技巧,最后还对如何实现最前沿的响应式设计进行了通俗易懂的演示。  《css设计指南(第3版)》适合css 初中级读者阅读。

    CSS3,线性渐变(linear-gradient)的使用总结

    《CSS3 经典教程系列》的前一篇文章向大家详细介绍了 text-shadow 文本阴影特性的用法,今天这篇文章我们在一起来看看 CSS3 中实现渐变效果的 Gradient 属性的具体用法。在以前,渐变效果和阴影、圆角效果一样都是做...

    图解CSS3核心技术与案例实战

    本书理论知识系统全面,详细讲解了选择器、边框、背景、文本、颜色、盒模型、伸缩布局盒模型、多列布局、渐变、过渡、动画、媒体、响应Web设计、Web字体等主题下涵盖的所有CSS3新特性,所有这些都巧妙地融入到案例中...

    CSS设计指南-]Charles Wyke-Smith.pdf

    简介: 《图灵程序设计丛书:CSS设计指南(第3版)》是一本面向初中级读者...随后4章介绍了页面布局、界面组件,CSS3圆角、阴影、渐变、多背景等视觉设计技巧,最后还对如何实现最前沿的响应式设计进行了通俗易懂的演示。

    一种表格背景色渐变效果的实现

    实现表格背景色渐变效果的技术细节 ...我们可以使用 CSS 的 `filter` 属性来实现背景色渐变效果,并使用 JavaScript 来实现交互式行为。在实际应用中,我们可以根据需要选择不同的技术方案,以实现所需的效果。

Global site tag (gtag.js) - Google Analytics