標籤:
1首先在 webroot檔案夾下建立一個css檔案
可以命名為style.css。如下:
2 jsp引用css檔案
<head>
<title>顯示</title>
<link rel="stylesheet" type="text/css" href=”style.css" />
</head>
3 css檔案的格式
Css規則主要由選取器和聲明組成。聲明由屬性和值組成。一條規則可以有多個聲明。當然選取器也可以有多個中間用逗號隔開
在這個例子中,h1 是選取器,color 和 font-size 是屬性,red 和 14px 是值。
h1{color:red; font-size:14px;}
h1,h2,h3,h4,h5,h6{
color: green;
}
為了看起來簡潔明了,一個聲明佔一行
屬性和值之間用“冒號:”隔開
記得一條聲明完了後得有一個分號“;”
選取器和聲明之間用“空格”隔開
聲明我們用花括弧“{}”包住
如果值不知道取多少合適,可以直接網上搜尋
4 css選取器介紹
4.1CSS 元素選取器
又被稱為類型選取器,是html的某個元素,比如p,a,h1.甚至可以是html本身。
h1{font-family: sans-serif;}
4.2css選取器分組
將多個元素組合在一起
h1,h2, h3, h4, h5, h6 {color:blue;}
4.3類別選取器
在html檔案中
<h1class="important">
Thisheading is very important.
</h1>
<pclass="important">
Thisparagraph is very important.
</p>
在css檔案中
如果只希望對h1元素做改變
h1.important{color:blue;}
如果希望所有相同類都做改變
*.important {color:red;}
當然,也可以省略星號“*”
但是一定要注意類前面的那個點“.”
4.4 ID選取器
在html檔案中
<pid="intro">This is a paragraph of introduction.</p>
在css檔案中
#intro{font-weight:bold;}
首先在html檔案中制定一個id名,然後在css檔案中,在id名加一個井號“#”
4.5屬性選取器
適用於某個元素的屬性,比如a元素的屬性 href
a[href]{color:red;}
這樣就會使得連結的字型變紅
4.6後代選取器(包含選取器)
後代選取器可以選中作為某元素後代的元素
在html檔案中
<h1>Thisis a <em>important</em> heading</h1>
<p>Thisis a <em>important</em> paragraph.</p>
在css檔案中
h1em {color:red;}
這樣的效果是,只有上面的important變成紅色
4.7偽類
例如在html中
<body>
<p>sometext</p>
<p>sometext</p>
</body>
在css中
p:first-child{
color: red;
}
效果是只有第一個 “some text”變為紅色
4.8虛擬元素
在html檔案中
<body>
<p>
Youcan use the :first-letter pseudo-element to add a special effect to the firstletter of a text!
</p>
</body>
在css檔案中
p:first-letter
{
color:#ff0000;
font-size:xx-large
}
這樣首字母“Y”會變為紅色,變大。
最後給大家提建議,w3shcool對於網站開發初學者有比較大的入門培訓
css入門基礎詳細教程