CSS2

发布时间 2023-04-02 21:43:55作者: CYF0913

一、CSS基础

1.CSS的编写位置

编写位置有3种

1.1行内元素

<h1 style="color:red;font-size:60px;">欢迎来到尚硅谷学习</h1>

1.2内部样式

<style>
h1 {
    color: red;
    font-size: 40px;
}
</style>

1.3外部样式

建立一个单独的css文件夹,新建一个.css的样式文件

如何在HTML文件引入.css文件

<link rel="stylesheet" href="./xxx.css">

二、CSS选择器

1、CSS基本选择器

1.1、通配选择器

可以选中所有的HTMl元素

    <style>
        语法格式:
        * {
            属性名:属性值;
        }
        * {
            font-size: 10px;
        }
    </style>

1.2、元素选择器

<style>
     h1 {
         font-size: 20px;
         color: blanchedalmond;
     }
</style>
<body>
    <h1>
        为页面中某种元素统一格式
    </h1>
</body>

1.3、 类选择器

.类名 {
属性名: 属性值;
}

/* 选中所有class值为speak的元素 */
.speak {
    color: red;
}
/* 选中所有class值为answer的元素 */
.answer {
    color: blue;
}

class可以写多个值 要用空格分开

<h1 class="h1class ssss sffa">我的二次玩行外样式</h1>

 1.4、Id选择器

<p id="idp">id选择器</p>
#idp {
            color: antiquewhite;
        }

 一个元素只能拥有一个 id 属性,多个元素的 id 属性值不能相同

 小总结:

2、CSS复合选择器

 2.1.交集选择器

语法:选择器1选择器2选择器3...选择器n {}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h1.qweclass {
            color: aquamarine;
        }
        .qweclass {
            font-size: 100px;
        }
    </style>
</head>
<body>
    <h1 id="qwe" class="qweclass">123</h1>
    <h1 id="qweid" class="qweclass1 qweclass">234</h1>
    <span id="qaz" class="qweclass">123</span>
    <h1 id="qazz" class="qweclass">123</h1>
</body>
</html>

 2.2.并集选择器

语法:选择器1, 选择器2, 选择器3, ... 选择器n {}

        .qweclass,
        #qwe,
        #qweid {
            font-size: 100px;
        }

 2.3.HTML元素间的关系

  1.  父元素
  2. 子元素
  3. 祖先元素
  4. 后代元素
  5. 兄弟元素

2.4、后代选择器

语法:选择器1 选择器2 选择器3 ...... 选择器n {} (先写祖先,再写后代)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div ul li {
            color: red;
        }
        .asd ul li {
            font-size: 10px;
        }
        .qwe li {
            font-size: 200px;
        }
    </style>
</head>
<body>
    <div class="asd">
        <ul class="qwe">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
    </div>
</body>
</html>

 2.5、子代选择器

选中指定元素中,符合要求的元素(儿子元素)。(先写父,再写子)

    <style>
        div>ul>li {
            color: aqua;
        }
        ul>li {
            font-size: 100px;
        }
    </style>

 2.6、兄弟选择器

选中指定元素后,符合条件的相邻兄弟元素。

+ 或 ~

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul+ul+div {
            color: brown;
        }
        ul~div {
            font-size: 200px;
        }
        ul+ul {
            font-size: 200px;
        }
    </style>
</head>
<body>
    <div class="asd">
        <ul class="qwe">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <ul class="qwe">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <div>我的div</div>
    </div>
</body>
</html>

两种兄弟选择器,选择的是下面的兄弟。

 2.7、属性选择器

选中属性值符合一定要求的元素。

  1. [属性名] 选中具有某个属性的元素。
  2. [属性名="值"] 选中包含某个属性,且属性值等于指定值的元素。
  3. [属性名^="值"] 选中包含某个属性,且属性值以指定的值开头的元素。
  4. [属性名$="值"] 选中包含某个属性,且属性值以指定的值结尾的元素。
  5. [属性名*=“值”] 选择包含某个属性,属性值包含指定值的元素。
/* 选中具有title属性的元素 */
div[title]{color:red;}
/* 选中title属性值为atguigu的元素 */
div[title="atguigu"]{color:red;}
/* 选中title属性值以a开头的元素 */
div[title^="a"]{color:red;}
/* 选中title属性值以u结尾的元素 */
div[title$="u"]{color:red;}
/* 选中title属性值包含g的元素 */
div[title*="g"]{color:red;}

2.8、伪类选择器

选中特殊状态的元素。

  1. :link 超链接未被访问的状态。
  2. :visited 超链接访问过的状态。
  3. :hover 鼠标悬停在元素上的状态。
  4. :active 元素激活的状态。
  5. :focus 获取焦点的元素。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        a:link{
            font-size: 100px;
            color: red;
        }
        a:visited{
            color: aqua;
        }
        a:hover{
            color: bisque;
        }
        a:active{
            color: brown;
        }
        input:focus {
            color: blueviolet;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <h1>动态伪类</h1>
    <a href="https://www.baidu.com">去百度</a>
    <a href="https://www.jd.com">去百度</a>
    <input type="text"/>
</body>
</html>

2.9、结构伪类

常用的:

  1. :first-child 所有兄弟元素中的第一个
  2. :last-child 所有兄弟元素中的最后一个
  3. :nth-child(n) 所有兄弟元素中的n
  4. :first-of-type 所有同类型兄弟元素中的第一个
  5. :last-of-type 所有同类型兄弟元素中的最后一个
  6. :nth-of-type(n) 所有同类型兄弟元素中的 n个 。