首頁  >  文章  >  web前端  >  CSS語法總結

CSS語法總結

高洛峰
高洛峰原創
2016-11-22 09:41:321392瀏覽

在學習CSS的過程中,了解並掌握CSS的語法是至關重要的,在這裡總結相關的語法。

一、CSS寫法:

css 程式碼

选择器{
    属性名:属性值 
}

二、寫在什麼地方:

有三種方式,分別為:行內樣式,內部樣式,外部樣式。

行內樣式

直接寫在HTML標籤內,寫於style屬性當中

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div style="background:red;width:15px;height:15px;"></div>
</body>
</html>

內部樣式

寫在頭部標籤內,置於style標籤內部

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .exp{
            height: 150px;
            width: 200px;
            background-color: #123456;
            box-shadow: 0 0 8px #132478;
        }
    </style>
</head>
<body>
    <div></div>
</body>
</html>

引入,置於CSS檔案中

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div></div>
</body>
</html>

css 程式碼

.exp{
    height: 30px;
    width: 20px;
    background-color: #123456;
    box-shadow: 0 0 1px #132478;
}

三、註解:

css 程式碼

.exp{
    background: #123444;    /*背景色,注释用此符号*/
    height: 90px;
    width: 80px;    //CSS中的注释不能用此符号,和HTML有所不同
}

註:

在寫法上,瀏覽器的私有屬性放在前面,標準屬性放在後面。私有屬性需要加前綴:加-webkit-,加-moz-,加-ms-,加-o-。

四、屬性值語法:CSS語法總結

🎜五、組合符號:🎜

1、空格 数量与顺序必须保持一致

 <&#39;font-size&#39;> <&#39;font-family&#39;>
 合法值:
 12px arial
 不合法值:
 12px   或者 arial 12px

2、&& 数量必须一致,顺序随意

 <length>&&<color>
 合法值:
 green 2px  或者  2em blue
 不合法值:
 blue  或者  5em

3、|| 必须出现一个,顺序无关

 underline||overline||linethrough||blink
 合法值:
 underline  或者  overline underline

4、| 只能出现一个

 <color>|transparent
 合法值:
 #123456  或者  transparent
 不合法值:
 #aabb33 transparent

5、[] 分组作用,作为整体

 bold [thin||<length>]     合法值:
 bold thin  或者  bold 3px

七、数量符号:

1、无

 <length>    只能出现一次
 合法值:
 1px  或者  10em
 不合法值:
 1px 3em 5px

2、+ 可以出现一次或多次

 <color-stop>[,<color-stop>]+
 合法值:
 #fff,red,yellow  或者  blue,red 50%,black
 不合法值:
 #123456

3、? 可出现,也可不出现

 inset?&&<color>
 合法值:
 inset #123445  或  #abcd33

4、{} 基本元素可以出现几次(最少出现几次,最多出现几次)

 <length>{2,4}    最少出现两次,最多出现四次
 合法值:
 1px 3em  或者  1px 3px 5em
 不合法值:
 3px

5、* 可以出现0次,1次或者多次

 <time>[,<time>]*
 合法值:
 1s     出现0次
 1s,5ms    出现1次

6、# 出现1次或多次,中间用”,"隔开

 <time>#    相当于    <time>[,<time>]*
 合法值:
 2s,4s,8s
 不合法值:
 2s 4s

八、@规则语法:

1、@标识符 xxx;

2、@标识符 xxx{}

常用的:

@media 响应式布局
@keyframe 描述动画的中间步骤
@font-face 引入外部字体


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn