css初学选择器

行内样式

<body>
    <!--
        行内样式
        css样式写在行内
    -->
<h1 style="color: red; font-size: larger">标题1</h1>
<h1 style="color: blue; font-size: medium">标题1</h1>
<h1 style="color: yellow; font-size: small">标题1</h1>

</body>

内部选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        h1{
            color: blue;
            font-size: 30px;
        }
    </style>
</head>
<body>
<!--
    内部样式
    css样式写head中的style标签中
-->
<h1>标题1</h1>
<h1>标题1</h1>
<h1>标题1</h1>

</body>
</html>

外部选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<style type="text/css">
        @import url("./css/css1.css");
    </style>-->

    <link href="css/css1.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--
    外部样式
    需要单独写一个css文件然后引入到html文件中
    css文件写法:
    h1{
    color: brown;
    font-size: 20px;
	}
    在head中
    导入式需写在style中
    <style type="text/css">
        @import url("./css/css1.css");
    </style>
    链接式不需要写在style
    <link href="css/css1.css" rel="stylesheet" type="text/css">

-->
<h1>标题1</h1>
<h1>标题1</h1>
<h1>标题1</h1>

</body>
</html>

标签选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*<!--
        标签选择器
        -->*/
        h1{
            color: green;
            font-size: 20px;
        }
    </style>
</head>
<body>

<h1>h1标签</h1>
<h1>h1标签</h1>

</body>
</html>

类选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*

                类选择器
                class=属性值
                在<style>标签中用
                .class属性值{}
                可以写多个重复的class值
        */
        .c1{
            color: blue;
            font-size: 15px;
        }
        .c2{
            color: brown;

        }
    </style>
</head>
<body>

<h1 class="c1">标签</h1>
<h1 class="c1">标签</h1>
<h1 class="c2">标签</h1>
<h1 class="c2">标签</h1>
<h1 class="c2">标签</h1>
<p class="c1">段落</p>
<p class="c1">段落</p>
<p class="c2">段落</p>
<p class="c2">段落</p>

</body>
</html>

ID选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*
            id选择器
            id=属性值
            在<style>标签中用
            #属性值{}
            只能写一个重复的id名,可以有多个id
        */
        #d1{
            color: brown;
        }
        #d2{
            color: blue;
        }
        #d3{
            color: red;
        }
        #d4{
            color: red;
            font-size: 20px;
        }
        #d4{
            color: yellow;
            font-size: 20px;
        }
    </style>
</head>
<body>
<!--
    id选择器
    id=属性值
    在<style>标签中用
    #属性值{}
    只能写一个重复的id名,可以有多个id
-->
<h1 id="d1">标签</h1>
<h1 id="d2">标签</h1>
<h1 id="d3">标签</h1>
<h1 >标签</h1>
<h1 >标签</h1>
<p id="d4">段落</p>
<p id="d5">段落</p>
<p >段落</p>
<p >段落</p>
</body>
</html>

span标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .c1{
            color: red;
            font-size: 20px;
        }
        #d{
            color: darkorchid;
            font-size: 30px;
        }
    </style>
</head>
<body>
<!--
    <span>标签 的作用
    能让某几个文字或者某个词语凸显出来
-->
<p>2000年,意气风发的刑警<span class="c1">安欣(张译饰)</span>与 倍受欺负的鱼贩子<span id="d">高启强(张颂文饰)</span>相识,
    而后随着高启强逐渐偏离正途,安欣意识到在京海市社会发展的背后正是以高家兄弟为首的
    黑恶势力暗流汹涌,两人分道扬镳并展开了长达20年的正邪较量 [19]  。
    2021年,在全国开展扫黑除恶常态化的背景下,中央督导组雷霆出击,安欣协同专案组彻查强盛集团犯罪团伙及其背后的保护伞,
    最终京海市得以拨云见日清风正气
</p>


</body>
</html>

层次选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*!*
            后代选择器 div E  p F
            div里边所有的p标签都会生效
        *!
        div p{
            background-color: #FF0000;
        }
        !*
            子选择器  div>p E>F
            div里边直接关联的p子标签生效,没有直接关联的p标签不生效
         *!
        div>p{
            background-color: blue;
        }*/
        /*
            兄弟选择器 E+F
            只能选择E后边的相邻的选择器,不能选择前面的
            先给第一个标签赋一个class属性值
            用.class属性值+相邻的标签
        */
        .class1+p{
            background-color: #FF0000;
        }
        /*
            兄弟通用选择器 E~f
            同一位置上的标签都会被选择
            先给第一个标签赋一个class属性值
            用.class属性值~相邻的标签
        */
        .class1~p{
            background-color: green;
        }
    </style>
</head>
<body>
<div>
    <p class="class1">1</p>
    <p>2</p>
    <p>3</p>
    <ul>
        <li>
            <p>4</p>
        </li>
        <li>
            <p>5</p>
        </li>
        <li>
            <p>6</p>
        </li>
    </ul>
</div>

</body>
</html>

结构伪类选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        /*
            E:first-child 作为父元素的第一个子元素的元素E
        */
        p:first-child{
            background-color: #0D7114;
        }
        /*
            E F:nth-child(n)
            选择父级元素E的第n个子元素F,(n可以是1、2、3),关键字为even(奇数)、odd(偶数)
        */
        /*p:nth-child(2){
            background-color: #1F87CC;
        }*/
        /*
            E:last-child 作为父元素的最后一个子元素的元素E
        */
        /*li:last-child{
            background-color: darkorchid;
        }*/
        /*
            E:first-of-type 当父元素中的E元素不是第一个时
            选择父元素内具有指定类型的第一个E元素
        */
        /*p:first-of-type{
            background-color: #FF0000;
        }*/
        /*
            E:last-of-type
            选择父元素内具有指定类型的最后一个E元素
        */
        p:nth-of-type(2){
            background-color: #FAA53B;
        }
        /*
            E F:nth-of-type(n)
            选择父元素内具有指定类型的第n个F元素
        */
        p:last-of-type{
            background-color: yellow;
        }

    </style>
</head>
<body>
<div>
    <h1>标题</h1>
    <p>段落</p>
    <p>段落</p>
    <p>段落</p>
    <ul>
        <li>无需列表</li>
        <li>无需列表</li>
        <li>无序列表</li>
    </ul>
</div>

</body>
</html>

属性选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        a {
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            text-align: center;
            background: #aac;
            color: blue;
            font: bold 20px/50px Arial;
            margin-right: 5px;
            text-decoration: none;
            margin: 5px;
        }
        /*!*
            根据属性
        *!
        [href]{
            background-color: #FAA53B;
        }
        !*
            属性=属性值
        *!
        [id=first]{
            background-color: #1F87CC;
        }*/
        /*
            属性值已什么开头的
        */
        /*[title^=test]{
            background-color: yellow;
        }*/
        /*
            属性值已什么结尾的
        */
        [class$=item]{
            background-color: darkorchid;
        }
        /*
            属性值中包含什么
        */
        [href*=file]{
            background-color: aqua;
        }
    </style>
</head>
<body>
<p class="demo">
    <a href="http://www.baidu.com" class="links item first" id="first" >1</a>
    <a href="" class="links active item" title="test website" target="_blank" >2</a>
    <a href="sites/file/test.html" class="links item"  >3</a>
    <a href="sites/file/test.png" class="links item" > 4</a>
    <a href="sites/file/image.jpg" class="links item" >5</a>
    <a href="efc" class="links item" title="website link" >6</a>
    <a href="/a.pdf" class="links item" >7</a>
    <a href="/abc.pdf" class="links item" >8</a>
    <a href="abcdef.doc" class="links item" >9</a>
    <a href="abd.doc" class="linksitem last" id="last">10</a>
</p>


</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值