css、類別選取器、id選取器、html選取器

來源:互聯網
上載者:User

一、使用CSS

1、為什麼要使用css:統一網站風格

csss使用文法:

選取器{

屬性1:屬性值;

屬性2:屬性值;

}

dome1.html 檔案:

<html>
<head>
<link rel="stylesheet" type="text/css" href="demo1.css">
</head>
<body>
<!--span元素是用於存放一小塊內容-->
<span class="css1">標題一</span><br />
<span class="css2">標題二</span><br />
<span class="css3">標題三</span><br />
<span class="css4">標題四</span><br />
<span class="css5">標題五</span><br />
<br />
</body>
</html>

css檔案:

.css1{
font-size:14px;
color:red;
}
.css2{
font-size:20px;
color:green;
font-style:italic;
}
.css3{
font-size:30px;
color:silver;
font-weight:bold;
}
.css4{
font-size:25px;
color:yellow;
text-decoration:underline;
}
.css5{
font-size:20px;
background-color:blue;
}

效果:

2、css使用濾鏡效果:

代碼如下:

<html>
<head>
<title>css濾鏡效果以及css內部連結的使用</title>
<!--css內部連結用style標籤對,只能對本頁面有效-->
<style type="text/css">
/*這裡用到偽類展示移動過程中的效果*/
a:link img{ /*a:link:設定 a 對象在未被訪問前的樣式。*/
filter:gray;
}
a:hover img{/*a:hover:設定對象在其滑鼠移至上方時的樣式。*/
filter:"";
}
</style>
</head>
<body>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
<a href="#"><img src="1.jpg"/></a>
</body>
</html>

二、常用4中選取器

1、類別選取器(class選取器)

基本使用

.類別選取器{

屬性名稱:屬性值;

.........

} 如:

.style1{

/*css檔案是通過這樣定義注釋的*/
width:400px;
height:350px;/*特別要注意分號不能少,一定要寫一個分號,少了分號後面就有問題了*/
border:1px solid red;
background-color:silver;
margin-left:100px;
margin-top:50px;
padding-top:20px;
padding-left:30px;
}

2、id選取器

基本使用

#id選取器{

屬性名稱:屬性值;

.........

}

如:

#mid{

font-size:12px;

color:red;

}

3、html元素選取器

使用方法

html元素{

屬性名稱:屬性值;

.........

}

p{

background-color:red;

font-size:14px;

}

table{

border:1 solid;

}

小技巧:對html元素樣式分類:在2個p標籤中的內容顯示方式不一樣。下面看看是怎麼實現的(注意看標有紅顏色字型)?

<html>
<head>
<title>使用html選取器對2個p元素內容不同處理方法</title>
<!--css內部連結用style標籤對,只能對本頁面有效-->
<style type="text/css">
p.num{
font-size:14px;
color:red;
text-decoration:underline;
}
p.num1{
font-size:16px;
color:green;

}
</style>
</head>
<body>
<p class="num">他說,對這個事,重慶市委態度是鮮明的,措施是堅決的,就是要依法辦事</p>
<p class="num1">他強調,對涉及不雅視頻的幹部,要深入調查,切實以事實為依據,以法律為準繩,去依法依紀處理,最後的處理結果,有關部門會適時向外界公布</p>
</body>
</html>

4、萬用字元選取器

該選取器可以用到所有的html元素,但是其優先權最低

優先順序表現:Id選取器> class選取器> html選取器> 萬用字元選則器

css 檔案可以使用在各種檔案(如html,php,…)

*{

屬性名稱:屬性值;

.........................

}

小技巧:讓body內容在網頁一直置中顯示:

 

body{
border:1px solid red;
width:500px;/*高與寬自己需求設定*/
height:500px;
margin:0px auto;
}

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.