CSS text(文字)


CSS 文字格式

文字格式

This text is styled with some of the text formatting properties. The heading uses the text-align, text -transform, and color properties. The paragraph is indented, aligned, and the space between characters is specified. The underline is removed from the  "試試看"  link.

Text Color

##"  link.

Text Color
  • ##顏色屬性被用來設定文字的顏色。

    顏色是透過CSS最經常的指定:
  • 十六進位值- 如"#FF0000"
  • 一個RGB值- "RGB(255,0,0)"

顏色的名稱- 如"紅色"

參閱CSS 顏色值查看完整的顏色值。

一個網頁的背景顏色是指在主體內的選擇:

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
body {color:red;}
h1 {color:#00ff00;}
p.ex {color:rgb(0,0,255);}
</style>
</head>

<body>
<h1>This is heading 1</h1>
<p>This is an ordinary paragraph. Notice that this text is red. The default text-color for a page is defined in the body selector.</p>
<p class="ex">This is a paragraph with class="ex". This text is blue.</p>
</body>
</html>

##執行實例»

1.gif點擊"運行實例" 按鈕查看線上實例


對於W3C標準的CSS:如果你定義了顏色屬性,你還必須定義背景色屬性。

文字的對齊方式

文字排列屬性是用來設定文字的水平對齊方式。

文字可居中或對齊到左或右,兩端對齊.

當text-align設定為"justify",每一行被展開為寬度相等,左,右外邊距是對齊(如雜誌和報紙)。

實例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
h1 {text-align:center;}
p.date {text-align:right;}
p.main {text-align:justify;}
</style>
</head>

<body>
<h1>CSS text-align 实例</h1>
<p class="date">2015 年 3 月 14 号</p>
<p class="main">“当我年轻的时候,我梦想改变这个世界;当我成熟以后,我发现我不能够改变这个世界,我将目光缩短了些,决定只改变我的国家;当我进入暮年以后,我发现我不能够改变我们的国家,我的最后愿望仅仅是改变一下我的家庭,但是,这也不可能。当我现在躺在床上,行将就木时,我突然意识到:如果一开始我仅仅去改变我自己,然后,我可能改变我的家庭;在家人的帮助和鼓励下,我可能为国家做一些事情;然后,谁知道呢?我甚至可能改变这个世界。”</p>
<p><b>注意:</b> 重置浏览器窗口大小查看 "justify" 是如何工作的。</p>
</body>

</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

文字修飾

text-decoration 屬性用來設定或刪除文字的裝飾。

從設計的角度看text-decoration屬性主要是用來刪除連結的底線:

實例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
a {text-decoration:none;}
</style>
</head>

<body>
<p>链接到: <a href="http://www.php.cn">php中文网</a></p>
</body>

</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

也可以這樣裝飾文字:

實例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
h1 {text-decoration:overline;}
h2 {text-decoration:line-through;}
h3 {text-decoration:underline;}
</style>
</head>

<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>

</html>

執行實例»######點擊"運行實例" 按鈕查看線上實例#######

我們不建議強調指出不是連結的文本,因為這常常混淆使用者。


文字轉換

文字轉換屬性是用來指定在一個文字中的大寫和小寫字母。

可用於所有字句變成大寫或小寫字母,或每個單字的首字母大寫。

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
p.uppercase {text-transform:uppercase;}
p.lowercase {text-transform:lowercase;}
p.capitalize {text-transform:capitalize;}
</style>
</head>

<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例


文字縮排

文字縮排屬性是用來指定文字的第一行的縮排。

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
p {text-indent:50px;}
</style>
</head>
<body>

<p>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>

運行實例»

點擊"運行實例" 按鈕查看線上實例


更多實例

指定字元之間的空間
這個例子示範如何增加或減少字元之間的空間。

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
h1 {letter-spacing:2px;}
h2 {letter-spacing:-3px;}
</style>
</head>

<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
</body>
</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

指定行與行之間的空間
這個例子示範如何指定在一個段落中行之間的空間

#實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
p.small {line-height:70%;}
p.big {line-height:200%;}
</style>
</head>

<body>
<p>
This is a paragraph with a standard line-height.<br>
This is a paragraph with a standard line-height.<br>
The default line height in most browsers is about 110% to 120%.<br>
</p>

<p class="small">
This is a paragraph with a smaller line-height.<br>
This is a paragraph with a smaller line-height.<br>
This is a paragraph with a smaller line-height.<br>
This is a paragraph with a smaller line-height.<br>
</p>

<p class="big">
This is a paragraph with a bigger line-height.<br>
This is a paragraph with a bigger line-height.<br>
This is a paragraph with a bigger line-height.<br>
This is a paragraph with a bigger line-height.<br>
</p>

</body>
</html>

執行實例»

點擊"運行實例" 按鈕查看線上實例

#設定元素的文字方向
這個範例示範如何改變元素的文字方向。

實例

<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style type="text/css">
div.ex1 {direction:rtl;}
</style>
</head>
<body>

<div>一些文本。 默认书写方向</div>
<div class="ex1">一些文本。从右到左的书写方向。</div>

</body>
</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

增加單字之間的空白空間
這個例子示範如何增加一個段落中的單字之間的空白空間。

實例

<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style type="text/css">
p
{ 
	word-spacing:30px;
}
</style>
</head>
<body>

<p>
This is some text. This is some text.
</p>

</body>
</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

在元素內禁用文字換行

這個範例示範如何停用一個元素內的文字換行。

實例

<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style type="text/css">
p
{
	white-space:nowrap;
}
</style>
</head>
<body>

<p>
这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。
</p>

</body>
</html>

運行實例»

點擊"運行實例" 按鈕查看線上實例

垂直對齊圖像
這個範例示範如何設定文字的垂直對齊圖像。

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
img.top {vertical-align:text-top;}
img.bottom {vertical-align:text-bottom;}
</style>
</head>

<body>
<p>An <img src="https://img.php.cn/upload/article/000/000/015/5c67d9134b166854.png" alt="php中文网" width="80" height="50" /> image with a default alignment.</p> 
<p>An <img class="top" src="https://img.php.cn/upload/article/000/000/015/5c67d9134b166854.png" alt="php中文网" width="80" height="50" /> image with a text-top alignment.</p> 
<p>An <img class="bottom" src="https://img.php.cn/upload/article/000/000/015/5c67d9134b166854.png" alt="php中文网" width="80" height="50" /> image with a text-bottom alignment.</p>
</body>
</html>

執行實例 »

點擊 "執行實例" 按鈕查看線上實例

#新增文字陰影
這個範例示範如何設定文字陰影。

實例

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
h1 {text-shadow:2px 2px #FF0000;}
</style>
</head>
<body>

<h1>Text-shadow effect</h1>

<p><b>Note:</b> Internet Explorer 9 and earlier do not support the text-shadow property.</p>

</body>
</html>

執行實例 »

點擊 "執行實例" 按鈕查看線上實例


所有CSS文字屬性。

縮排元素中文字的首行設定文字陰影控制元素中的字母##設定或返回文字是否被重寫 vertical-align設定元素的垂直對準white-space設定元素中空白的處理方式
屬性描述
color設定文本顏色
direction設定文字方向。
letter-spacing設定字元間距
line-height設定行高
text-align對齊元素中的文字
#text-decoration在文字中加入修飾
text-indent
text-shadow
text-transform
#unicode-bidi
##########word-spacing#########設定字間距#############