32.前端之css

css

css

  1. css是级联样式表(Cascading Style Sheet),属于标记语言,没有逻辑。
  2. css是完成页面样式(长啥样) 与 布局(标签位置)
  3. 内容:
    • css如何控制html标签 - 建立联系 - css选择器
    • css可以控制哪些样式(样式与布局)
    • css如何导入到html文件中

css三种引入方式

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css的引入</title>
    <!--2、内联式-->
    <!--书写位置:在head标签中的style标签内-->
    <!--css语法:css选择器 { 样式1; 样式2; } -->
    <!--优缺点: 可读性强,有复用性,样式被html页面绑定了,不能提供给其它html页面使用-->
    <style>
        h2 {
            color: red;
            font-size: 100px;
            text-align: center;
        }
    </style>

    <!--3、外联式-->
    <!--书写位置:在外部css文件中,在html文件中通过link标签引入css文件-->
    <!--css语法:css选择器 { 样式1; 样式2; } -->
    <!--优缺点: 可读性强,有复用性,适合团队开发(文件级别的复用性)-->
    <link rel="stylesheet" href="css/样式引入.css">
</head>
<body>
    <!--1、行间式-->
    <!--书写位置:在标签的style属性中书写样式-->
    <!--优缺点: 可读性差,没有复用性,书写直接-->
    <h1 style="color: red; font-size: 100px; text-align: center;">css的引入</h1>
    <h1>h1标签</h1>

    <h2>h2标签</h2>
    <h2>h2标签</h2>

    <h3>h3标签</h3>
    <h3>h3标签</h3>

    <h4>h4标签</h4>
    <h4>h4标签</h4>
</body>
</html>
/* css/样式引入.css */
h3 {
    color: green;
}
h4 {
    font-size: 50px;
    text-align: center;
}

三种方式优先级比较

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css的引入</title>

    <!--优先级:
    1、内联与外联之间没有优先级区别,由于html属于解释性语言,书写在下方的会覆盖上方的样式
    2、行间式的优先级要高于一切
    -->

</head>
<body>
    <h3>h3标签</h3>
    <h3>h3标签</h3>

    <h4>h4标签</h4>
    <h4 style="font-size: 100px">h4标签</h4>
</body>

<style>
    h4 {
        color: #ff7800;
        font-size: 20px;
    }
</style>
<link rel="stylesheet" href="css/样式引入.css">
</html>

css基础选择器

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>css基础选择器</title>
    <style>
        /*优先级:可以从作用范围来判断 - 作用范围越精确,优先级越高 */
        /*1、统配选择器*/
        * {
            color: pink;
            font-size: 12px;
        }
        /*2、标签选择器*/
        h1 {
            font-size: 20px;
        }

        /*3、类选择器*/
        .h {
            font-size: 30px!important;
        }

        .h2 {
            font-size: 40px;
        }

        .h.h2 {
            font-size: 50px;
        }

        /*4、id选择器*/
        #hhh {
            font-size: 100px;
        }

        /*优先级:!important > 行间式 > id > class > 标签 > 统配 */
    </style>
</head>
<body>
    <h1 class="h">1标题</h1>
    <h2 id="hhh" class="h h2" style="font-size: 12px">2标题</h2>
</body>
</html>

高级选择器

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>高级选择器</title>
    <style>
        .h2 {
            color: red;
        }

        /*1、后代(子代)选择器*/
        /*后代:空格连接  子代:>连接*/
        /*body > .h2 控制一个 | body .h2 控制两个*/
        body > .h2 {
            font-size: 40px;
        }

        /*2、兄弟(相邻)标签:只能上兄弟修饰下兄弟*/
        /*兄弟:~连接  相邻:+连接*/
        /*.h3 + .h4 控制一个 | .h3 ~ .h4 控制两个*/
        .h3 + .h4 {
            color: pink;
        }

        /*3、群组选择器:控制多个选择器*/
        .h2, body h3, h4 {
            text-align: center;
        }

        /*4、选择器的优先级:权重 - 个数*/
        /*权值:不同级别没有可比性、同一级别比个数、选择器类型不影响优先级、优先级一致看顺序
        *:1
        标签:10
        class(伪类):100
        id:1000
        !important:10000
        */
        #h6 {
            color: black;
        }

        .d1 div h6 {
            color: pink;
        }
        .d2 h6 {
            color: brown;
        }

        body h6 {
            color: cyan;
        }
        div > h6 {
            color: orange;
        }
        h6 {
            font-size: 100px;
            text-align: center;
            color: red;
        }

    </style>

    <style>

        /*5、交叉选择器*/
        h6#h6.h.hh {
            color: chartreuse;
        }
    </style>
</head>
<body>
    <div class="d1">
        <div class="d2">
            <h6 id="h6" class="h hh">css高级选择器优先级</h6>
        </div>
    </div>

    <h3 class="h3">第1个h3</h3>
    <h4 class="h4">第1个h4</h4>
    <h4 class="h4">第2个h4</h4>
    <h3 class="h3">第1个h3</h3>
    <div>
        <h4 class="h4">第1个h4</h4>
        <h4 class="h4">第2个h4</h4>
    </div>
    
    
    <h2 class="h2">h2标签</h2>
    <div>
        <h2 class="h2">div下的h2</h2>
    </div>
    <p>p标签的内容不水平居中</p>

</body>
</html>

伪类与属性选择器

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>伪类选择器</title>
    <style>
        .p {
            background-color: orange;
        }

        /*先确定位置,再筛选选择器*/
        p:nth-child(3) {
            background-color: red;
        }

        /*先确定选择器,在匹配位置*/
        p:nth-of-type(3) {
            background-color: brown;
        }

        p.p3 {
            background-color: cyan;
        }
        /*总结:
        1、伪类选择器优先级与类相同
        2、nth-child在同一结构下都是相同选择器时使用
        3、nth-of-type在同一结构下不全是相同选择器时使用
        */
    </style>

    <style>
        .h4 {
            color: orange;
        }
        [class='h4'] {
            color: brown;
        }
        [owen*='owen'] {
            color: pink;
        }
        [owen^='o'] {
            color: blueviolet;
        }
        /*总结:
        1、属性选择器优先级同类
        2、[属性名]查找所有有该属性的标签
        3、[属性名=属性值]精确查找
        4、[属性名^=值]以某某值开头
        4、[属性名*=值]包含某某值(模糊查询)
        */
    </style>
</head>
<body>
    <h4 class="h4" owen="oooowennnnn">owen</h4>
    <h4 class="h4" owen="zero">zero</h4>

    <div>
        <p class="p">第1个p</p>
        <p class="p">第2个p</p>
        <p class="p p3">第3个p</p>
        <p class="p">第4个p</p>
        <p class="p">第5个p</p>
    </div>
    <div>
        <div>
            <h3>h3标签</h3>
            <p class="p">第1个p</p>
            <p class="p">第2个p</p>
            <p class="p">第3个p</p>
            <p class="p">第4个p</p>
            <p class="p">第5个p</p>
        </div>
    </div>
</body>
</html>

a标签的四大伪类

!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>a标签的四大伪类</title>
    <style>
        /*一、a的四大伪类*/
        a {
            font-size: 30px;
        }
        /*1、标签没有被访问过*/
        a:link {
            color: orange;
        }
        /*2、标签被悬浮*/
        a:hover {
            /*鼠标样式*/
            /*wait row-resize none text pointer default*/
            cursor: pointer;
        }
        /*3、标签被激活*/
        a:active {
            color: pink;
            cursor: wait;
        }

        /*4、标签已被访问过*/
        a:visited {
            color: brown;
        }
    </style>

    <style>
        /*二、reset操作*/

        /*在开发中往往用不到四种伪类,且要清除掉系统的默认样式*/
        /*就可以如下对a标签进行样式设置:清除系统默认样式 - reset操作*/
        a {
            color: black;
            text-decoration: none;
        }
    </style>
    <style>
        /*三、普通标签的伪类运用*/
        .btn {
             80px;
            height: 45px;
            background-color: orange;
        }
        /*字体*/
        .btn {
            font: bold 20px/45px 'STSong';
            text-align: center;
        }
        /*边界圆角*/
        .btn {
            border-radius: 5px;
        }
        /*不允许文本操作*/
        body {
            user-select: none;
        }

        /*伪类*/
        .btn:hover {
            cursor: pointer;
            background-color: orangered;
        }
        .btn:active {
            background-color: brown;
        }
    </style>
</head>
<body>
    <div class="btn">按钮</div>

    <!--
    标签没有被访问过
    标签被悬浮
    标签被激活
    标签已被访问过
    -->
    <a href="https://www.baidu.com">前往百度</a>

</body>
</html>

字体样式

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>文本属性</title>
    <style>
        .box {
             300px;
            height: 300px;
            background-color: orangered;
        }
        .box {
            /*大小*/
            font-size: 30px;
            /*颜色*/
            color: blue;
            /*字族*/
            font-family: "STSong", "新宋体", "Arial";
            /*字重: lighter | normal | bold | 100 ~ 900*/
            font-weight: 500;
            /*字体样式*/
            font-style: oblique;
        }
        .box {
            /*文本划线: underline  line-through  overline*/
            text-decoration: none;

            /*文本水平位置: left center right*/
            text-align: center;

            /*行高*/
            line-height: 300px;
        }
        .box {
            /*整体设置*/
            font: bold 20px/300px '黑体', 'Arial';
            color: darkcyan;
            text-align: left;
            
            /*首行缩进*/
            text-indent: 20px;
            /*字间距*/
            letter-spacing: 10px;
        }
    </style>
</head>
<body>
    <div class="box">
        文本ABC ab
    </div>
</body>
</html>

边界圆角

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>边界圆角</title>
    <style>
        .box {
             300px;
            height: 300px;
            background-color: orangered;
        }
        .box {
            /*固定值 | 百分比*/
            /*border-radius: 10%;*/

            /*左上为第一个角,顺时针编号*/
            /*border-radius: 10px 20px 30px 40px;*/

            /*不足找对角*/
            /*border-radius: 10px 50px 100px;*/
            /*border-radius: 10px 50px;*/

            /*横纵分离,先横后纵*/
            /*border-radius: 50px / 50%;*/
            /*border-radius: 50px 100px / 50%;*/

            border-radius: 60%;
        }
    </style>
    <style>
        .b {
             300px;
            height: 150px;
            background-color: orangered;
            border-radius: 50% 50% 0 0 / 100% 100% 0 0;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <div class="b"></div>
</body>
</html>

背景图片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>背景图片</title>
    <style>
        .box {
             300px;
            height: 300px;
            background-color: orangered;
        }

        .box {
            /*显示比屏幕大的图片:缩放尺寸*/
            /*background-image: url("img/kj.gif");*/
            /*尽量只设置宽,高等比缩放,不失真*/
            /*background-size: 300px 300px;*/
        }

        .box {
            /*显示比屏幕小的图片:处理平铺与位置*/
            background-image: url("img/lss.jpg");
            /*平铺: repeat-x repeat-y repeat no-repeat*/
            background-repeat: no-repeat;
            /*位置*/
            /*1.只设置x轴,y轴默认center*/
            /*2.x轴:left center right 具体像素 百分百*/
            /*2.y轴:top center bottom 具体像素 百分百*/
            background-position: center center;
        }
    </style>
</head>
<body>
    <div class="box"></div>
</body>
</html>

精灵图

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>精灵图案例</title>
    <style>
        /*精灵图就是操作大图在显示区域的位置*/
        .box {
             500px;
            height: 100px;
            background-color: orangered;
            border: solid;
        }
        .box {
            background-image: url("img/bg.png");
            background-position-y: -150px;
        }
        .box:hover {
            cursor: pointer;
            background-position-y: -250px;
        }
    </style>
    <style>
        .b1 {
             155px;
            height: 48px;
            border: solid;
            background-image: url("img/bg.png");
        }
        .b1:hover {
            cursor: pointer;
            background-position-y: -48px;
        }
    </style>
    <style>
        .b2 {
             157px;
            height: 48px;
            border: solid;
            background-image: url("img/bg.png");
            background-position: -155px 0;
        }
        .b2:hover {
            cursor: pointer;
            background-position: -155px -48px;
        }
    </style>
</head>
<body>
    <div class="box"></div>

    <div class="b1"></div>

    <div class="b2"></div>
</body>
</html>

显示方式

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>显示方式</title>
    <style>
        .box {
             500px;
            height: 100px;
            background-color: orangered;
        }
    </style>
    <style>
        /*显示方式:display
        block:
            1.支持设置宽高
            2.自带换行
            重点:所有有宽高、参与位置布局的都是block

        inline:
            1.不支持设置宽高,宽高只能有文本撑开
            2.不带换行,一行显示不下会自动换行(保留数据的整体性)
            重点:存放文本的

        inline-block:
            1.支持设置宽高
            2.不带换行,一行显示不下会自动换行(以标签整体换行,标签左右有间距)
            重点:不去主动设置该显示方式,系统的两个img、input都设置为了单标签(不会嵌套任何东西)
            注:如果要用inline-block参与布局,为了标签布局不受内容影响,设置vertical-align: top
        */
        a {
             500px;
            height: 100px;
            display: block;
            background-color: pink;
        }
        owen {
             100px;
            height: 100px;
            display: inline-block;
            background-color: cyan;
            font-size: 30px;

            /*文本垂直对齐方式: baseline top bottom*/
            vertical-align: top;
        }
        owen:nth-of-type(2) {
            font-size: 20px;
        }

        img {
             100px;
            height: 100px;
            background-color: tan;
        }
        input {
             100px;
            height: 100px;
            background-color: tan;
            vertical-align: top;
        }
    </style>
</head>
<body>
    <a href="5.边界圆角.html" class="box"></a>

    <div>div1</div>
    <div>div2</div>

    <a href="">a1</a>
    <a href="">a2</a>

    <owen>html/css</owen>
    <owen>javascript</owen>
    <owen></owen>
    <owen></owen>
    <owen></owen>
    <img src="img/lss.jpg" alt="">
    <input type="text">
    
    <div></div>
</body>
</html>

盒模型布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>盒模型布局</title>
    <style>
        body {
            margin: 0;
            padding: 100px 0 0 200px;
        }

        /*盒模型组成部分: */
        /*margin + border + padding + content
        1.每部分都有自己的独立区域
        2.content是宽x高,作为内容或子标签的显示区域
        3.padding是内边距,没有自身颜色,完成内容的内移(保证显示区域大小不变,可以响应减小content)
        4.border是边框,有宽度、样式、颜色,自身透明(transparent)时,是可以透出背景颜色的
        5.margin是外边距,控制盒子的显示位置,left、top控制自身,right、bottom影响兄弟
        注:margin的偏移依据当前所在位置
        */
        div {
             100px;
            height: 100px;
            background-color: red;
        }
        owen {
            /*margin: 10px;*/
            margin-top: 50px;
            margin-left: 10px;
            /*margin-right: 10px;*/
            /*margin-bottom: 100px;*/
        }

        owen {
            /*border-color: black;*/
            /*border- 3px;*/

            /*none solid dashed dotted*/
            /*border-style: solid;*/

            border: red dashed 10px;
        }

        owen {
            /*padding: 上右下左,不足找对边*/
            /*padding: 10px 20px 30px;*/
            padding: 10px;
        }
        owen {
            display: block;
            /* 100px;*/
             80px;
            /*height: 100px;*/
            height: 80px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <!--
    盒模型:
    概念:广义上页面中所有的标签都称之为盒子,狭义上布局的盒子指的是display:block
    -->
    <owen>123</owen>
    <div></div>
</body>
</html>

浮动布局

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>浮动布局</title>
    <style>
        .wrap {
            border: 10px solid yellow;
             300px;
            /*父级在宽度不固定时高度不能设置死*/
            /*height: 300px;*/
        }
        /*清浮动:父级清浮动,就是在自己宽度是否确定下,都能保证父级的高度刚刚好包裹子集*/
        .wrap:after {
            content: '';
            display: block;
            clear: both;
        }

        .box {
             100px;
            height: 100px;
            background-color: orange;
            border-radius: 50%;
            font: 20px/100px 'Arial';
            color: blue;
            text-align: center;
        }
        .box {
            float: left;
        }
        .b {
             500px;
            height: 100px;
            background-color: red;
        }
        /*浮动布局:
        1.子集浮动参照父级宽度
        2.子集浮动不再撑开父级高度
        3.父级高度需要自己处理,否则会影响兄弟布局,采用清浮动处理
        */
    </style>
    <style>

        h1:before {
            content: '123';
        }
        /*当控制的控制自身完成所有布局(包含所有子集布局),再执行该时间点*/
        h1:after {
            content: '000';
        }
    </style>
</head>
<body>
    <h1>内容</h1>

    <!--.wrap>.box{$}*9-->
    <div class="wrap">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
        <div class="box">5</div>
        <div class="box">6</div>
        <div class="box">7</div>
        <div class="box">8</div>
        <div class="box">9</div>
    </div>
    <div class="b"></div>
</body>
</html>

两种布局案例

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>两种布局总结</title>

    <style>
        body, h1 {
            margin: 0;
        }
    </style>
    <style>
        .header {
             1210px;
            height: 100px;
            background-color: orange;
            /*自动获取留白区域*/
            /*margin-left: auto;*/
            /*margin-right: auto;*/
            margin: 0 auto;
        }
        .header:after {
            content: '';
            display: block;
            clear: both;
        }
        .header a {
            display: block;
             500px;
            height: 100px;
            background-color: red;
            float: left;
        }
        .header form {
            /*background-color: pink;*/
            float: right;
            /*父子级顶端产生距离,建议使用padding*/
            padding-top: 30px;
        }
        .header input {
            /*margin-right: 20px;*/
             220px;
            height: 30px;
            border: none;
            font-size: 17px;
            vertical-align: top;
        }
        .header button {
             32px;
            height: 32px;
            border: none;
            background-color: red;
            outline: none;
            color: white;
            margin-right: 30px;
            vertical-align: top;
        }

    </style>
</head>
<body>
    <!--盒模型:margin、padding协调操作,能用padding尽量用padding,再考虑用margin-->
    <!--浮动:需要左右排列的block采用浮动布局,且父级一定要清浮动-->
    <div class="header">
        <h1>
            <a href=""></a>
        </h1>
        <form method="get" action="https://www.baidu.com/s">
            <input type="text" name="wd">
            <button type="submit">Go</button>
        </form>
    </div>


</body>
</html>
原文地址:https://www.cnblogs.com/yellowcloud/p/11305342.html