css语法

基础语法

 body {
  background: blue;
}

body是标签名,background是属性,blue是属性值

 多个标签组合

h1,h2,h3 {
  color: red;
}

把标题<h1><h2><h3>的字体颜色都变为红色

 属性综合写法

 div {
  font:italic normal bold 11pt arial;
}

div的字体风格(font-style)属性值是italic,字体变量(font-variant)属性值是normal,字体浓淡(font-weight)属性值是bold,字体大小(font-size)属性值是11pt,字体名称(font-family)属性值是Arial

 标签嵌套

div h1 {
  color: blue;
}

div里面的<h1>字体颜色为蓝色。

定义id

#main {

  background: #ff0000;
  width: 100%;
}
<div id="main">hello</div>

这里是用#+id名是方式,也可以用标签名加+#+id名,如div#main,一般不这么用。

 定义class

.red {
  color: red;
}
<div class="red" >hello</div>

同id的写法差不多,把“#”改为“.” ,如果一个标签同时定义了id跟class,id具有优先级。

*表示该标签下的所有元素样式

 body * {
    padding: 0px;
}

 用得比较少

样式优先级

(1) tag中的style > id class 

(2) tag中的style > 页面中的style > 导入的样式@import 

(3) 用link,如<link rel='stylesheet' type='text/css' href='' />,更写在页面上同等级的。

  如果同样的样式定义了2次,后面出现的具有优先级。

原文地址:https://www.cnblogs.com/ljmin/p/2582014.html