一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

CSS更好利用text-decoration属性显示文字特效

时间:2015-07-07 编辑:简简单单 来源:一聚教程网

text-decoration属性可以做很多事情,我们来学习这个属性实现更多细致的样式。

文字可以有更多装饰

例如:

a {
  text-decoration: underline overline;
}

text-decoration

可以看到在Almanac 中text-decoration修饰的内容,更具体点,它给子属性text-decoration-line添加多个属性值。


改变装饰的颜色

下划线的颜色默认为文本设置color的属性值,但你可以改变:

a {
  text-decoration-color: #f06d06;
}

text-decoration

观察在Almanac中 text-decoration-color修饰的内容。

注意在Gecko里下划线是呈现在字体的下面,然而在WebKit/Blink里下划线呈现在字体的上面:

text-decoration

如今处理彩色下划线普遍使用border来代替text-decoration。边框可以比text-decoration更好的独立控制下划线的颜色,宽度以及位置。

但有一些事情border是做不了的,就像......


改变装饰的样式

border无法实现下面的样式!

a {
  text-decoration-style: wavy;
}

text-decoration

观察Almanac 中text-decoration-style修饰的内容。


它会变得更好

现如今已经有很多方法可以更好的实现带下划线的文本。例如,使用skip可以更好的增强阅读性,就像下面一样:

text-decoration

上面例子是用 text-decoration-skip实现的,可是并不是所有浏览器都支持。同时,使用较宽松的下划线以及减少contrast-y的值可能会更好,这里使用了rgba():

text-decoration

隐藏下划线只是它的(text-decoration)的一个功能,此外,你也可以用skip修饰一些行内元素,空格,甚至控制edges。

需要注意的是Safari/iOS浏览器似乎会使用默认的skip值。

text-decoration

汇总

html


  line:
  
  
  
  color:         
  skip:                  
text-decoration-style: solid;

text-decoration-style: double;

text-decoration-style: dotted;

text-decoration-style: dashed;

text-decoration-style: wavy;


CSS

css;toolbar:false">.solid { 
  text-decoration-style: solid; 
}
.double { 
  text-decoration-style: double;
}
.dotted { 
  text-decoration-style: dotted; 
}
.dashed { 
  text-decoration-style: dashed; 
}
.wavy { 
  text-decoration-style: wavy; 
}
/* styling for Pen, unrelated to text-decoration-style */
body { 
  font-family: sans-serif;
}
p {
  text-decoration: underline;
  font-size: 2em;
}
div {
  line-height: 1.5;
}
JS
$(".style button").on("click", function() {
  $("p").css("text-decoration-line", $(this).text());
});
$(".color button").on("click", function() {
  $("p").css("text-decoration-color", $(this).text());
});
$(".skip button").on("click", function() {
  $("p").css("text-decoration-skip", $(this).text());
});





text-decoration属性变成了属性简写

跟着最新的CSS规范,text-decoration现在的写法是这样的:

a {
  text-decoration: overline aqua wavy;
}

text-decoration属性现在需要用三种属性值来表示了:text-decoration-line, text-decoration-color, and text-decoration-style.

但不幸的是,目前只有火狐浏览器实现了对这些新属性的支持。

你可以用火狐浏览器试一试下面的演示:
HTML代码

IT'S LIKE WATER, PEOPLE

(You should see a wavy line on top. Currently works only in Firefox)

CSS代码


body {
  padding: 30px;
  text-align: center;
  font-family: Arial, sans-serif;
}

a, a:visited {
  color: blue;
  background: aqua;
  -moz-text-decoration-color: aqua;
  -moz-text-decoration-line: overline;
  -moz-text-decoration-style: wavy;
  text-decoration-color: aqua;
  text-decoration-line: overline;
  text-decoration-style: wavy;
}

演示

aaa.jpg
在这演示中,我们没有使用简写形式,而是分开描述每个属性。这是可以更好的保证浏览器的向后兼容,使那些目前不支持这种写法的浏览器也不受影响。

相关文章

热门栏目