前端-CSS总结

1 阅读2分钟

CSS四种样式导入方式

行内式
<h1 style="color:red;">我是一个主标题</h1>

内嵌式
<style>
    h1{
        color:red;
    }
</style>

<h1>我是一个主标题</h1>
外链式
<link ref="stylesheet" href="css/css.css">

导入式
<style>
    @import url(css/css.css);
</style>

选择器

id选择器

    <style>
        .para1{
        color:red;
        }
    </style>
  <p id="para1">我是段落</p>

类选择器

<style>
        #warning{
        color:red;
        }
    </style>
   <p class="warning">我是段落</p>

标签选择器

span {
        color: red;
     }

复合选择器


后代选择器

.box p {
    color:red;
}
div.box p.spec em {
      color: red;
}

后代选择器不仅仅是儿子,还可以是孙子或者更深层次

交集选择器
h3.spec {
      font-style: italic;
 }
h3标签并且类名是spec

并集选择器
ul, ol {
    list-style: none;
}
同时选择 ul和ol

伪类

伪类主要用于定义元素的特殊状态,他们不是类,他们不是类,而是表示了元素处于特定状态时的样式变化 通俗来说,伪类依附于已存在的元素,使用冒号(:)作为表示,描述元素某特性或状态下的样式

a:link  未访问的链接
a:visited  已访问的链接
a:hover  鼠标悬停状态
a:active  鼠标点击(按下不放)时激活

元素关系选择器

子选择器

div>p  div的子标签p

相邻兄弟选择器

img+p img后面紧跟着的p

通用兄弟选择器

p~span  p后面所有同层级的span

序号选择器

:first-child 第一个元素
.box1 p:first-child {
            color: red;
        }
 类box1 中的第一个p元素,如果第一个元素不是p则不生效
 
 p:last-child 最后一个元素
 
 p:nth-child(3) 第三个元素
 p:nth-of-type(3) 第三个p类型的元素
 p:nth-last-child(3) 倒数第三个p元素,如果第三个不是p则不生效
 
 p:nth-last-of-type(3) 倒数第三个p类型的元素
 

属性选择器

img[alt] 选择有alt属性的img标签

img[alt="故宫"] 选择alt属性是故宫的img标签

img[alt^="北京"] 选择alt属性以北京开头的img标签

img[alt$="夜景"] 选择alt属性以夜间结尾的img标签

img[alt*="美"] 选择有alt属性中含有美字的img标签

img[alt~="手机拍摄"] 选择有alt属性中有空格隔开的手机拍摄字样的img标签

img[alt|="参赛作品"] 选择有ait属性以“参赛作品-”开头的img标签

CSS3 新增伪类

:empty 选择空标签

:focus 选择当前获得焦点的表单元素

:enabled 选择当前有效的表单元素

:disabled 选择当前无效的表单元素

:checked 选择当前已经勾选的单选按钮或者复选框

:root 选择根元素,即<html>标签