CSS属性之文本(Text)属性 常用14个

color属性


color属性设置文本字体颜色。

颜色值有三种写法:
  • 直接写颜色名,如:red
  • 十六进制,如:#d2d2d2
  • rgb颜色,如:rgb(255,0,255),注:数值最大为255,最小为0,即rgb(0,0,0)到rgb(255,255,255)。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  
	 div#div1 {
	   color: red;
	 }
	 div#div2 {
	   color: #d2d2d2;
	 }
	 div#div3 {
	   color: rgb(255,0,255);
	 }
	
	</style>
  </head>
  <body>
    
	<div id="div1">
	  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
	</div>
	
	<div id="div2">
	  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
	</div>
	
	<div id="div3">
	  Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
	</div>
	 
  </body>
</html>

效果如下:



direction属性

direction属性指定文本方向/书写方向。

属性值:
  • "ltr"--文本方向从左到右显示。
  • "rtl"--文本方向从右到左显示。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p2 {
	    direction: ltr;
	  }
	  #p3 {
	    direction: rtl;
	  }
	 
	
	</style>
  </head>
  <body>
    
	<p id="p1">文本默认方向。</p>
	<p id="p2">文本从左到右显示。这是设置的lrt值。!</p>
	<p id="p3">文本从右到左显示。这是设置的rtl值。!</p>
	 
  </body>
</html>

效果如下:



letter-spacing属性


letter-spacing属性增加或减少字符间的空白(字符间距)letter(字母,文字),spacing(间距,距离)。
属性值:
  • 可为正值,意为增加字符间距。
  • 可为负值,意为减少字符间距。
  • 当值为0时,表示字符间距不变,为默认。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 {
	    letter-spacing: 0px;
	  }
	  #p2 {
	    letter-spacing: 3px;
	  }
	  #p3 {
	    letter-spacing: -5px;
	  }
	 
	
	</style>
  </head>
  <body>
    
	<p id="p1">文本默认方向。</p>
	<p id="p2">文本从左到右显示。</p>
	<p id="p3">文本从右到左显示。</p>
	 
  </body>
</html>

效果如下:



word-spacing属性

word-spacing属性增加或减少单词间的间距

属性值同letter-spacing一样。




line-height属性


line-height属性设置以百分比计的行高。大多数浏览器的默认行高间距为110%~120%。

属性值:
  • number,设置数字,此数字与当前字体尺寸相乘来设置行高。
  • 百分比,设置为当前字体尺寸的百分比行高间距。
  • length,设置为固定的行高间距。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  
	  #p2 {
	    line-height: 60%;
	  }
	  #p3 {
	    line-height: 2;
	  }
	 
	
	</style>
  </head>
  <body>
    
	<p id="p1">
      这是默认的行高。<br>
      这是默认的行高。<br>
	  大多数浏览器的默认行高是110%~120%。<br>
	</p>
	<p id="p2">
      这是减少两行间的的行高。<br>
      这是减少两行间的的行高。<br>
      这是减少两行间的的行高。<br>  
	</p>
	<p id="p3">
      这是增加两行间的行高。<br>
      这是增加两行间的行高。<br>
      这是增加两行间的行高。<br>     
	</p>
	
	 
  </body>
</html>


效果如下:





text-align属性

text-align属性设置文本水平方向上的对齐方式
属性值:
  • left,文本水平方向上左对齐,注:浏览器默认的方向。
  • right,文本水平方向上右对齐
  • justify,文本水平方向上两端对齐

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 {
	    text-align: left /* 左对齐 */
	  }
	  #p2 {
	    text-align: right; /* 右对齐 */
	  }
	  #p3 {
	    text-align: justify; /* 两端对齐 */
	  }
	 
	
	</style>
  </head>
  <body>
    
	<p id="p1">
      这是默认的行高。<br>    
	</p>
	<p id="p2">
      这是减少两行间的的行高。<br>     
	</p>
	<p id="p3">
      这是增加两行间的行高。<br>  
	</p>
	
	 
  </body>
</html>


效果如下:



vertical-align属性


vertical-align属性设置文本在垂直方向上的对齐方式。
常用属性值有:
  • top,元素的顶端与行中最高元素的顶端对齐。
  • middle,把元素放在父元素的中部
  • bottom,元素的顶端与内中最低元素的顶端对齐。
  • text-top,把元素的顶端父元素字体的顶端对齐。
  • text-tottom,把元素的低端父元素字体的低端对齐。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 img {
	    vertical-align: text-top ;
	  }
	  #p2 img {
	    vertical-align: text-bottom; 
	  }
	  #p3 img {
	    vertical-align: top; 
	  }
	 
	
	</style>
  </head>
  <body>
    
	<p id="p1">
	  这是
	  <img src="1.gif" alt="文本替代">
      默认的行高。   
	</p>
	<p id="p2">
	  这是
	  <img src="1.gif" alt="文本替代">
      减少两行间的的行高。     
	</p>
	<p id="p3">
	  这是
	  <img src="1.gif" alt="文本替代">
      增加两行间的行高。  
	</p>
	
	 
  </body>
</html>


效果如下:





text-decoration属性


text-decoration属性添加到文本的修饰。(一般用于添加下划线,或者上划线,常用于a元素,一般取消a元素的下划线。)

属性值 :
  • none,默认值,没有修饰的,注:此值一般用于取消a元素的下划线。
  • overline,为文本头部添加一条线。
  • underline,为文本低部添加一条线。(下划线)
  • line-through,定义穿过文本的一条线。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 {
	    text-decoration: line-through;
	  }
	  #p2 {
	    text-decoration: overline; 
	  }
	  #p3 {
	    text-decoration: underline; 
	  }
	  a {
	    text-decoration: none; /* 取消a元素的下划线 */
	  } 
	
	</style>
  </head>
  <body>
    
	<p id="p1">
	  定义穿过文本的一条线。   
	</p>
	<p id="p2">
	  为文本顶部添加一条线。   
	</p>
	<p id="p3">
	  为文本低部添加下划线。
	</p>
	<a href="http://www.baidu.com" target="_blank">百度</a>
	
	
	 
  </body>
</html>


效果如下:




text-indent属性

text-indent属性设置首行文本的缩进。

属性值:
  • length,固定缩进,默认值:0。
  • ?%,按父元素宽度的百分比缩进。
<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  p {
	    text-indent: 50px;
	  }
	
	</style>
  </head>
  <body>
    
	<p id="p1">
	In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just remember that all the people in this world haven't had the advantages that you've had.'   
	</p>
	
	
	
	 
  </body>
</html>


效果如下:




text-transform属性


text-transform属性改变文本的大小写(字母)。
属性值:
  • none,默认。
  • capitalize,每个单词的首字母为大写。
  • uppercase,文本全为大写。
  • lowercase,文本全为小写。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 {
	    text-transform: capitalize;
	  }
	  #p2 {
	    text-transform: uppercase;
	  }
	  #p3 {
	    text-transform: lowercase;
	  }
	
	</style>
  </head>
  <body>
    
	<p id="p1">
	This is some text.
	</p>
	<p id="p2">
	This is some text.
	</p>
	<p id="p3">
	This is some text.
	</p>
	
	
	
	 
  </body>
</html>


效果如下:





white-space属性


white-space属性规定文本中的空格符如何处理,以及是否换行。
属性值:
  • pre,与<pre>标签一样,保留空格符。
  • nowrap,文本不会换行,继续在同一行上显示,直到<br>为止。
  • pre-wrap,保留空格符序列,正常换行。
  • pre-line,合并空格符序列,正常换行。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  #p1 {
	    white-space: nowrap;
	  }
	  #p2 {
	    white-space: pre-wrap;
	  }
	  #p3 {
	    white-space: pre-line;
	  }
	
	</style>
  </head>
  <body>
    
	<p id="p1">
	This is some text.
	This is            some text.
	This is some text.
	</p>
	<p id="p2">
	This is some text.
	This is            some text.
	This is some text.
	</p>
	<p id="p3">
	This is some text.
	This is             some text.
	This is some text.
	</p>
	
	
	
	 
  </body>
</html>


效果如下:





text-overflow属性


text-overflow规定当文本溢出包含它的元素时,该如何处理。
属性值:
  • clip,将溢出的部分裁剪掉。
  • ellipsis,将溢出的部分用省略号代替。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  p {
	    width: 80px;
		border: 1px solid #000;
		white-space: nowrap; /* 设置不能换行 */
		overflow: hidden; /* 当溢出时,不显示溢出的部分 */
	  }
	  #p2 {
	    text-overflow: clip;
	  }
	  #p3 {
	    text-overflow: ellipsis;
	  }
	
	</style>
  </head>
  <body>
    
	<p id="p1"> <!-- 当没有设置该属性时 -->
	This is some text.This is some text.This is some text.
	</p>
	<p id="p2"> <!-- 当属性值为clip时,会裁剪掉溢出的部分 -->
	This is some text.This is some text.This is some text.
	</p>
	<p id="p3"> <!-- 当属性值为ellipsis时,用省略号代替溢出的部分 -->
	This is some text.This is some text.This is some text.
	</p>
	
	
	
	 
  </body>
</html>


注:要设置属性"white-space: onwrap;" 和"overflow: hidden;"

效果如下:




text-shadow属性


text-shadow属性为文本添加阴影效果。

写法:text-shadow: x y c color; 属性值分别是:水平阴影位置、垂直方向阴影位置、模糊距离(模糊程度)、阴影的颜色。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  p {
	    text-shadow: 5px 5px 1px #ff0000;
	  }
	  
	
	</style>
  </head>
  <body>
    
	<p id="p1"> 
	This is some text.This is some text.This is some text.
	</p>
	<p id="p2"> 
	This is some text.This is some text.This is some text.
	</p>
	<p id="p3"> 
	This is some text.This is some text.This is some text.
	</p>
	
	
	
	 
  </body>
</html>


效果如下:





word-wrap属性


word-wrap属性允许长的内容自动换行。

属性值:
  • normal,只在允许的断点处换行。
  • break-word,允许在长单词或URL内部换行。

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="utf-8">
	<title>文本属性</title>
	<style>
	  p {
	    width: 110px;
		border: 1px solid #000;		
	  }
	  #p3 {
	    word-wrap: break-word; /* 该属性允许在长单词和URL内部换行 */
	  }
	  
	  
	
	</style>
  </head>
  <body>
    
	<p id="p1"> 
	Thisrrqrqwrtyfgf is some text.This is some text.This is some text.<a href="http://www.baidu.com">http://www.baidu.com</a>
	</p>
	<p id="p3"> 
	Thisrrqrqwrtyfgf is some text.This is some text.This is some text.<a href="http://www.baidu.com">http://www.baidu.com</a>
	</p>
	
	
	
	 
  </body>
</html>


效果如下:






  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值