【CSS_01】

1. 快速入门及导入方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--规范:<style> 可以编写css代码 每一个声明最好使用分号结尾
    语法:
       选择器{
       声明1;
       声明2;
       }
    -->
    <!--内部样式-->
    <style>
        h1{
            color: aqua;
        }
    </style>
    <!--外部样式-->
    <link rel="stylesheet" href="css.css">
</head>
<body>
<!--style="color: chocolate" 行内样式-->
<h1 style="color: chocolate">标题</h1>
</body>
</html>
h1{
    color: blue;
}

CSS的优势:
  1. 内容和表现分离
  2. 网页结构表现统一,可以实现复用
  3. 样式丰富
  4. 建议使用独立于html的css文件
  5. 利用SEO,容易被搜索引擎收录
  6. 优先级:就近原则(行内样式最近,其次哪个样式离h1近,优先级高于远的)

拓展:外部样式的两种写法
  • 链接式
 <!--外部样式-->
    <link rel="stylesheet" href="css.css">
  • 导入式
<style>
        @import "css.css";
    </style>

2. 三种选择器(重要)

作用:选择页面上的某一个或某一类元素

2.1 基本选择器

  1. 标签选择器:选择一类标签
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*标签选择器:会选择到页面上所有这个标签的元素*/
        h1{
            color: chocolate;
            background: burlywood;
            border-radius: 16px;
        }
        p{
            font-size: 80px;
        }
    </style>
</head>
<body>

<h1>标题1</h1>
<h1>标题1</h1>
<p>学习</p>
</body>
</html>
  1. 类 选择器 class:选择所有class属性一致的标签,跨标签
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /*类选择器的格式:.class的名称{}
        好处:可以多个标签归类,同一个class可以复用
        */
        .num1{
            color: chocolate;
        }
        .num3{
            color: aqua;
        }
    </style>
</head>
<body>
<h1 class="num1">标题1</h1>
<h1 class="num1">标题2</h1>
<h1 class="num3">标题3</h1>
<p class="num1">p标签</p>
</body>
</html>
  1. Id 选择器:全局唯一
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*id选择器:id必须保证全局唯一
        #id名称{}
        不遵循就近原则,固定的
        id选择器> class 选择器> 标签选择器
        */
        #01{
            color: aqua;
        }
        .num1{
            color: antiquewhite;
        }
    </style>
</head>
<body>

<h1 id="01" class="num1">biaoti1</h1>
<h1 id="02">biaoti1</h1>
<h1 class="num1">biaoti1</h1>
</body>
</html>

2.2 层次选择器

  1. 后代选择器:某个元素后面的所有元素
 body p{
            background: aqua;
        }
  1. 子选择器:只选中某个元素后的一代
 body>p{
            background: chocolate;
        }
  1. 相邻兄弟选择器:只选中同辈
 .active + p{
            background: azure;
        }
  1. 通用选择器:选中当前元素向下的所有兄弟选择器
.active~{
            background: cornflowerblue;
        }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*后代选择器*/
        body p{
            background: aqua;
        }
        /*子选择器*/
        body>p{
            background: chocolate;
        }
        /*相邻兄弟选择器*/
        .active + p{
            background: azure;
        }
        /*通用选择器*/
        .active~{
            background: cornflowerblue;
        }
    </style>
</head>
<body>
    <p>p0</p>
    <p>p1</p>
    <p class="active">p2</p>
    <p>p3</p>
    <ul>
       <li>
           <p>p4</p>
       </li>
       <li>
           <p>p5</p>
       </li>
       <li>
           <p>p6</p>
       </li>
    </ul>
</body>
</html>

2.3 层次选择器

伪类:条件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*选中li的第一个元素*/
        ul li:first-child{
            background: chocolate;
        }
        /*选中li的最后一个元素*/
        ul li:last-child{
            background: cornflowerblue;
        }
        /*选中p1:定位到父元素,选择当前的第一个元素
        选择当前p元素的父级元素,选中父级元素的第一个p元素才能生效
        */
        p:nth-child(1){
            background: blueviolet;
        }
        /*选中父元素下的第2个类型*/
        p:nth-of-type(2){
            background: chartreuse;
        }
    </style>
</head>
<body>
    <p>p1</p>
    <p>p2</p>
    <p>p3</p>
    <ul>
        <li>li1</li>
        <li>li2</li>
        <li>li3</li>
    </ul>
</body>
</html>

2.4 属性选择器(常用)

id+class 结合

=
*=
^=
$=

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>属性选择器</title>
    <style>
        .demo a{
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: blueviolet;
            text-align: center;/*文本左右居中*/
            color: azure;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;/*字体大小/行高*/
        }
        /*属性名
        =  绝对等于
        *= 包含这个元素判断
        ^= 以这个元素开头
        $= 以这个元素结尾
        */

        /*存在id的元素*/
        a[id]{
            background: cornflowerblue;
        }
        /*id=first的元素*/
        a[id=first]{
            background: cornflowerblue;
        }
        /*class中有link的元素*/
        a[class*=links]{
            background: darkgreen;
        }
        /*选中href中以http开头的元素*/
        a[href^=http]{
            background: aqua;
        }
        /*选中href中以docx结尾的元素*/
        a[href$=docx]{
            background: chartreuse;
        }
    </style>
</head>
<body>
<p class="demo">
  <a href="https://www.baidu.com" class="links iteam first" id="first">1</a>
  <a href="https://www.gole.com" class="links iteam ">2</a>
  <a href="resouce/1.jpg" class=" iteam">3</a>
  <a href="resouce/1.docx" class=" iteam docx">4</a>
</p>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值