顏色屬性被用來設置文字的顏色。
顏色是通過CSS最經常的指定:
一個網頁的文本顏色是指在主體內的選擇:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
body {
color: blue;
}
h1 {
color: #00ff00;
}
h2 {
color: rgb(255, 0, 0);
}
</style>
</head>
<body>
<h2>hello world</h2>
<h1>welcome to CaoZhou</h1>
</body>
</html>
注:對于W3C標準的CSS:如果你定義了顏色屬性,你還必須定義背景色屬性。
文本排列屬性是用來設置文本的水平對齊方式。
文本可居中或對齊到左或右,兩端對齊。
當text-align設置為"justify",每一行被展開為寬度相等,左,右外邊距是對齊(如雜志和報紙)。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
h1 {
text-align: center;
}
p.date {
text-align: right;
}
p.main {
text-align: justify;
}
</style>
</head>
<body>
<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 lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.none {}
.del {
text-decoration: none;
}
</style>
</head>
<body>
<p>原來的樣子</p>
<a href="#" class="none">wwwwwwwwwwwwwwwwww</a>
<p>去掉下劃線</p>
<a href="#" class="del">wwwwwwwwwwwwwwwwwwwww</a>
</body>
</html>
也可以這樣裝飾文字:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</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>
注:不建議強調指出不是鏈接的文本,因為這常常混淆用戶。
text-transform文本轉換屬性是用來指定在一個文本中的大寫和小寫字母。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</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>
text-indent文本縮進屬性是用來指定文本的第一行的縮進。
p {text-indent:50px;}
增加或減少字符之間的空間。
<style>
h1 {
letter-spacing:2px;
}
h2 {
letter-spacing:-3px;
}
</style>
指定在一個段落中行之間的空間。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</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">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</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">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
img{
width: 200px;
height: 100px;
}
img.top {
vertical-align: text-top;
}
img.bottom {
vertical-align: text-bottom;
}
</style>
</head>
<body>
<p>An <img src="img/logo.png" /> image with a default alignment.</p>
<p>An <img class="top" src="img/logo.png" /> image with a text-top alignment.</p>
<p>An <img class="bottom" src="img/logo.png" /> image with a text-bottom alignment.</p>
</body>
</html>
設置文本陰影。
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=640, user-scalable=no">
<title>項目</title>
<style>
h1{
text-shadow: 2px 2px #FF0000;
}
</style>
</head>
<body>
<h1>Text-shadow effect</h1>
</body>
</html>
本文主要介紹了CSS文本樣式實際應用中應該如何去操作,通過講解文本中對應的屬性去改變文本的表現形式。使用豐富的效果圖的展示,能夠更直觀的看到運行的效果,能夠更好的理解。使用Html語言,代碼結構更佳的清晰,能夠幫助你更好的學習。
這里是云端源想IT,幫你輕松學IT”
嗨~ 今天的你過得還好嗎?
睡眠等同于希望
每次醒來都是一個新的開始
一個新的希望
- 2024.03.22 -
在Web開發的世界中,CSS(層疊樣式表)是構建視覺吸引力和定義網頁布局的不可或缺的工具。
掌握如何恰當地引入CSS樣式以及理解它們的優先級規則,對于前端開發者來說至關重要。今天,我們就來深入探討CSS的四種引入方式,以及選擇器的優先級之謎,了解常用的CSS樣式及使用方法!
CSS(層疊樣式表)為網頁提供了豐富的樣式定義,允許開發者通過多種方式將樣式應用到HTML文檔中。以下是四種主要的CSS引入方式:
1.1 行內樣式
這是最直接也最簡單的方法,通過在HTML元素的style屬性中直接編寫CSS規則。
示例:
<p style="color: red; font-size: 20px;">這是一段紅色的文字。</p>
這種方式的優點是簡單快捷,但缺點是它使得HTML代碼與樣式混合,不夠純凈,且不利于樣式的復用和維護。
1.2 內嵌樣式
在一個HTML文檔中使用<style>標簽將CSS規則嵌入到HTML的head部分。這種方式適用于定義特定于某一頁面的樣式。
示例:
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
1.3 外部樣式
這是最常用的方法,它通過<link>標簽將外部的CSS文件鏈接到HTML文檔中。這種方法的優勢在于可以在多個頁面間共享同一個樣式文件,有助于保持代碼的整潔和一致性。
示例:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
其中,mystyle.css的內容可能如下:
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
1.4 導入樣式
使用@import語句在CSS文件中導入另一個CSS文件。盡管這種方法可以分離樣式表,但它通常不被推薦使用,因為其加載時序可能會影響頁面渲染效率。
示例:
@import url('https://fonts.googleapis.com/css?family=Roboto');
body {
font-family: 'Roboto', sans-serif;
}
1.5 樣式單優先級
作用域范圍:外部樣式表>內部樣式表>行內樣式表
優先級:
2.1 字體樣式
normal - 文字正常顯示
italic - 文本以斜體顯示
oblique - 文本為“傾斜”(傾斜與斜體非常相似,但支持較少)
font-weight 屬性指定字體的粗細
示例:
<!DOCTYPE html>
<html>
<head>
<style>
.sp1{
color: darkorange;
font-size: 20px;
font-weight: bolder; /* bolder 字體是否加粗*/
font-style: italic; /* italic 字體是否傾斜*/
font-family: "宋體"; /* 設置字體樣式*/
}
.sp2{
/* 簡寫 */
/* 順序不能能變:style-weigth-size-family */
font:italic bolder 15px 宋體 ;
color:rgb(28, 235, 97);
}
</style>
<body>
<span>
編程學習,從云端源想開始!
</span><br>
<span>
讓知識觸手可及
</span>
<p>讓知識觸手可及</p>
</body>
</html>
2.2 文本樣式
color: 字體顏色
text-align: center; - - 文本對齊方式
text-decoration:none; - - 文本的線
text-shadow: pink 5px 5px 2px; - - 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】
line-height: - - 行高 (文本在標簽內所占的高度)
width:
height:
border: 1px #ffffff solid; - - 盒子邊框【邊框粗細-顏色-邊框線樣式】
示例:
<style>
.p{
color: rgb(0, 255, 21); /* 字體顏色 */
text-align: center; /* 文本對齊方式 */
text-decoration:none; /* 文本的線 */
text-shadow: pink 5px 5px 2px; /* 文本的陰影 【陰影顏色-水平方向的偏移量-垂直方向的偏移量-模糊距離】*/
line-height: 400px; /* 行高 (文本在標簽內所占的高度)*/
width: 400px;
height: 300px;
border: 1px rgb(76, 14, 223) solid; /* 盒子邊框【邊框粗細-顏色-邊框線樣式】 */
}
</style>
</head>
<body>
<p>歡迎來到云端源想!</p>
<a href="https://www.baidu.com"></a>
</body>
2.3 背景樣式
width: 500px;
height: 1200px;
background-color: pink; - - 背景顏色
background-image: url(…/img/background.jpg); - - 背景圖片
background-repeat: no-repeat; - - 背景圖片是否平鋪
background-position: left top; - - 指定背景圖片的位置
background-attachment: fixed; - - 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】
示例:
<style>
.d{
width: 500px;
height: 1200px;
background-color: pink; /* 背景顏色 */
background-image: url(../img/background.jpg); /* 背景圖片 */
background-repeat: no-repeat; /* 背景圖片是否平鋪 */
background-position: left top; /* 指定背景圖片的位置 */
background-attachment: fixed; /* 背景圖片是否隨著標簽滾動 【fixed-固定 scroll-滾動】 */
}
</style>
</head>
<body>
<div>
</div>
2.4 列表樣式
<!DOCTYPE html>
<html>
<head>
<style>
li{
background-color: lemonchiffon;
/*列表樣式:常用取值:none-無樣式 square-正方形 circle-空心圓 decimal-數字*/
list-style-type: circle;
/*列表樣式為自定義圖片*/
list-style-image: url(../img/2.jpg);
/*列表樣式的放置位置*/
list-style-position: inside;
/*列表樣式縮寫*/
list-style: square url(../img/2.jpg) inside;
/*常用的列表樣式*/
list-style: none;
}
</style>
</head>
<body>
<ul>
<li>列表項1</li>
<li>列表項2</li>
<li>列表項3</li>
</ul>
</body>
</html>
2.5 邊框樣式
<!DOCTYPE html>
<html>
<head>
<style>
.border{
/*邊框寬度*/
border-width: 2px;
/*邊框顏色*/
border-color: red;
/*邊框樣式:solid 實線 dotted 點線 dashed 虛線*/
border-style: dashed;
/*邊框樣式縮寫:樣式 顏色 寬度*/
border:solid green 5px;
/*邊框可以為4個方向分別設置*/
border-top: dashed black 4px;
border-right: dashed #FF00FF 4px;
border-bottom: dotted darkblue 4px;
border-left: solid fuchsia 5px;
/*沒有邊框*/
border: none;
/*常用的細邊框樣式*/
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div class="border">這是一個帶有邊框的元素</div>
</body>
</html>
2.6 盒子模型
所有的html元素可以看做是盒子,在css中,"box model"是用來設計和布局時使用。
CSS盒子模型本質是一個盒子,封裝周圍的html元素,它包括:邊框、邊距、填充、實際內容。
盒子模型允許我們在其他元素和周圍元素邊框之間的空間放置元素。
想要快速入門前端開發嗎?推薦一個前端開發基礎課程,這個老師講的特別好,零基礎學習無壓力,知識點結合代碼,邊學邊練,可以免費試看試學,還有各種輔助工具和資料,非常適合新手!點這里前往學習哦!云端源想
示例:
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* border:邊框,分4個方向,同理margin、padding也分為四個方向
* margin:元素與元素之間對的距離
* padding:內容與邊框之間的距離
* 設置的時候順序:上 右 下 左
*/
.div{
border: solid red 10px;
/*四個方向上的元素與元素之間的距離都是50px*/
margin: 50px;
/*兩個值的時候:第一個參數表示上下距離都是50px,第二個參數表示左右距離都是100px*/
margin: 50px 100px;
padding: 50px;
/*
一個元素真正的寬度=width+左右padding值+左右的border值
一個元素的真正高度=height+上下的padding值+上下的border值
* */
}
</style>
</head>
<body>
<div>111111111112222222222223333333333333333</div>
</body>
1)盒子的寬高
元素的實際寬度和高度:
2)設置寬度=元素實際寬度,box-sizing屬性。
<head>
<meta charset="UTF-8">
<title></title>
<style>
/* box-sizing:確認元素的大小
content-box: 實際寬度=width+左右的psdding值+上下的border值
實際高度=height+上下的padding值+上下的border值
border-box:實際寬度=width;實際高度=height
padding和border不會影響元素的實際寬高
* */
.box{
width: 100px;
height: 200px;
border: 5px solid;
padding: 5px;
box-sizing: content-box;
}
</style>
</head>
<body>
<div>你好中國</div>
</body>
CSS的世界博大精深,以上只是冰山一角,希望通過這些基礎的常用樣式可以幫助你快速進入CSS世界的大門。
掌握CSS的引入方式和選擇器優先級是構建高效、可維護網站的關鍵。通過這些知識,你可以更好地管理和優化你的樣式代碼,創造出既美觀又專業的網頁設計。現在,準備好邁入CSS的世界,開啟你的創意之旅吧!
我們下期再見!
END
文案編輯|云端學長
文案配圖|云端學長
內容由:云端源想分享
擇器
格式: body div div p{樣式代碼} 匹配body里面的div里面的div里面的所有p標簽(包括后代)
格式: body>div>div>p{樣式代碼} 匹配body里面的div里面的div里面的所有p子元素(不包含后代)
格式: a:link/visited/hover/active:{樣式代碼}
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--樣式-->
<style>
#l2{color: green}
.c1{color: yellow}
input[type="text"],body>p{background-color: red}
/*子元素選擇器*/
body>div>div>p{color: pink}
/*子孫后代選擇器*/
body div div p{background-color: yellow}
/*偽類選擇器*/
a:link{color: red;}/*未訪問*/
a:visited{color: pink}/*訪問過*/
a:hover{color: green}/*懸停*/
a:active{color: yellow}/*點擊*/
</style>
</head>
<body>
<ul>
<li>劉備</li><li id="l2">關羽</li><li class="c1">張飛</li>
</ul>
<p>香蕉</p><p class="c1">蘋果</p>
<input type="text">
<input type="password">
<div>
<p>p1</p>
<div><p>p2</p></div>
<div><div><p>p3</p></div></div>
</div>
<a href="http://www.celinfcom">注釋助手</a>
顏色賦值
三原色: 紅綠藍 ,red green blue rgb ,每個顏色的取值范圍0-255 顏色賦值的幾種方式:
<style>
h1{
/*color: #ff0000;*/
/* color: #f00;*/
/*color: rgb(255,0,0);*/
color: rgba(255,0,0,0.3);
}
#d2{
width: 200px;
height: 200px;
background-color: pink;
/*設置背景圖片*/
background-image: url("../b.jpg");
/*設置背景圖片尺寸*/
background-size: 100px 100px;
/*禁止重復*/
background-repeat: no-repeat;
/*控制位置:橫向 縱向*/
/*background-position: 50px 100px;*/
background-position: 50% 50%;
}
</style>
<body>
<div id="d2"></div>
<h1>顏色測試</h1>
</body>
背景圖片
<style>
#d1{
width: 611px;
height: 376px;
background-color: #e8e8e8;
background-image: url("http://celinf.org/itemCat/study_computer.png");
background-repeat: no-repeat;
background-position: 90% 90%;
background-size: 318px 319px;
}
</style>
<body>
<div id="d1"></div>
</body>
文本和字體相關樣式
<style>
div{
width: 200px;
height: 50px;
border: 1px solid red;
/*水平對齊方式*/
text-align: center;
/*行高*/
line-height: 50px;
/*文本修飾 overline上劃線underline下劃線 line-through刪除線
none去掉文本修飾*/
text-decoration: line-through;
/*文本陰影:顏色 x偏移值 y偏移值 濃度*/
text-shadow: red -15px -15px 5px;
/*字體大小*/
font-size: 20px;
/*字體加粗 bold加粗 normal去掉加粗*/
font-weight: bold;
}
a{
text-decoration: none;/*去掉自帶下劃線*/
}
h3{
font-weight: normal;/*去掉自帶加粗*/
/*設置斜體*/
font-style: italic;
/*設置字體*/
/*font-family: cursive;*/
font: 30px cursive;
}
</style>
<body>
<h3>這是個h3</h3>
<a href="">超鏈接</a>
<div>文本和字體測試</div>
</body>
元素的顯示方式display
<style>
div{
width: 100px;
height: 100px;
border: 1px solid red;
}
span{
border: 1px solid blue;
/*行內元素不能修改寬高*/
width: 100px;
height: 100px;
/*把行內元素改成了塊級元素或行內塊元素都可以修改寬高 */
display: inline-block;
}
img{
width: 100px;
height: 100px;
display: none;/*隱藏元素*/
}
a{
width: 132px;
height: 40px;
background-color: #0aa1ed;
/*行內元素不能修改寬高*/
display: block;
text-align: center;
line-height: 40px;
color: white;
text-decoration: none;
font-size: 20px;
/*圓角 值越大越圓*/
border-radius: 3px;
}
</style>
<body>
<a href="">查看詳情</a>
<img src="../b.jpg" alt="">
<img src="../b.jpg" alt="">
<img src="../b.jpg" alt="">
<div>div1</div>
<div>div2</div>
<div>div3</div>
<span>span1</span>
<span>span2</span>
<span>span3</span>
</body>
盒子模型
盒子模型用來控制元素的顯示效果包括: 元素內容content+外邊距margin+邊框border+內邊距padding
border邊框效果
盒子模型之內容content
盒子模型之外邊距margin
盒子模型之邊框border
賦值方式:
<style>
#d1{
width: 100px;
height: 100px;
border:1px solid red;
/*margin-left: 100px;
margin-top: 100px;*/
/*margin-bottom: 50px;*/
margin: 10px 20px 30px 40px;
}
#d2{
width: 100px;height: 100px;border:1px solid red;
/*上下相鄰彼此添加外邊距 取最大值*/
margin-top: 100px;
}
#s1{
/*行內元素上下外邊距無效*/
margin-right: 100px;
}
#s2{
/*左右相鄰彼此添加外邊距 兩者相加*/
margin-left: 50px;
}
#big{
width: 200px;height: 200px;background-color: green;
overflow: hidden;/*解決粘連問題*/
}
#big>div{
width: 50px;height: 50px;background-color: red;
margin-left: 50px;
/*當元素的上邊緣和上級元素的上邊緣重疊時,給元素添加上外邊距會出現粘連問題*/
margin-top: 50px;
}
#border_div{
width: 400px;
height: 200px;
border: 10px solid blue;
/*設置圓角*/
border-radius: 200px;
}
</style>
<body>
<div id="border_div"></div>
<div id="big">
<div></div>
</div>
<span id="s1">span1</span><span id="s2">span2</span>
<div id="d1">外邊距測試</div>
<div id="d2">div2</div>
</body>
盒子模型之內邊距padding
<style>
div{
width: 150px;
height: 150px;
border:1px solid red;
/*內邊距會影響元素的寬高*/
padding-top: 50px;
padding-left: 50px;
}
</style>
<body>
<div>內邊距</div>
</body>
CSS的三大特性
<style>
#d1{
color: red;
}
div{
/*!important作用是提升優先級*/
color: blue !important;
}
</style>
<body>
<div id="d1">
<p>這是個p標簽</p>
<span>這是div里面的span</span>
<a href="">超鏈接</a>
</div>
<span>這是div外面的span</span>
</body>
綜合性練習【Demo】
<style>
body{
font: 12px "simhei", Arial, Helvetica, sans-serif;
color: #666;width: 1000px;
}
#d1{
width: 611px;height: 376px;
background-color: #e8e8e8;
background-image: url("http://celinf.org/study_computer1.png");
background-size: 318px 319px;
background-repeat: no-repeat;
background-position: 90% 90%;
overflow: hidden; display: inline-block;
}
#d2{margin: 68px 0 0 36px; width: 245px;height: 232px; }
#d3{
width: 375px;height: 376px;
background-color: #e8e8e8; overflow: hidden;
background-image: url("http://celinf.org/study_computer.png");
background-repeat: no-repeat;
background-size: 292px 232px;
background-position: 85% 85%; display: inline-block;
}
div>div{width: 253px; height: 232px;margin: 39px 0 0 25px; }
.title_p{
color: #333333;font-size: 32px;
margin-bottom: 12px;
font-weight: lighter;
}
.intro_p{font-size: 12px;font-weight: lighter;margin-bottom: 24px;}
.price_p{
font-size: 24px;color: #0aa1ed;
font-weight: bold;margin-bottom: 12px;
}
a{
display: block; background-color: #0aa1ed;
color: white;
width: 132px;height: 40px;
text-align: center; line-height: 40px;
font-size: 20px; text-decoration: none;
border-radius: 2px;
}
</style>
<body>
<div id="d1">
<div id="d2">
<p class="title_p">靈越 燃7000系列</p>
<p class="intro_p">
酷睿雙核i5處理器|256GB SSD| 8GB內存<br>
英特爾HD顯卡620含共享顯卡內存
</p>
<p class="price_p">¥4999.00</p>
<a href="#">查看詳情</a>
</div>
</div>
</div>
<div id="d3">
<div>
<p class="title_p">顏值 框不住</p>
<p class="intro_p">
酷睿雙核i5處理器|256GB SSD| 8GB內存
<br>
英特爾HD顯卡620含共享顯卡內存
</p>
<p class="price_p">¥6888.00</p>
<a href="#">查看詳情</a>
</div>
</div>
</body>
學習記錄,如有侵權請聯系刪除
*請認真填寫需求信息,我們會在24小時內與您取得聯系。