小工具知识网

当前位置: 首页 > 行业知识 > CSS详细教程

行业知识

CSS详细教程

2026-12-01 18:44:00 来源:互联网转载

文章目录

  • 前言
  • 一、CSS应用方式
    • 1.在标签上
    • 2.在head标签的style上
    • 3.写在文件中
    • 二、选择器
      • 1.ID选择器
      • 2.类选择器
      • 3.标签选择器
      • 4.属性选择器
      • 5.后代选择器
      • 6.样式覆盖
      • 三、CSS样式
        • 1.高度和宽度
        • 2.块级标签和行内标签转换
        • 3.字体颜色/大小/粗细/样式/对齐/边框
        • 4.浮动
        • 5.背景色
        • 6.内边距
        • 7.边界
        • 8.外边距
        • 9.鼠标悬停效果
        • 10.内容增加
        • 11.位���

          前言

          层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。CSS 能够对网页中元素位置的排版进行像素级精确控制,支持几乎所有的字体字号样式,拥有对网页对象和模型样式编辑的能力。该博客将详细介绍常见的CSS样式以及相关内容。

          一、CSS应用方式

          1.在标签上

           

          2.在head标签的style上

          • 相当于把样式取了一个名字
          • 后续使用直接在标签中添加:class=‘名字’
            https://blog.csdn.net/2203_75327677/article/details/...        Document            .c1 {            color: red;        }        

            用户注册

            https://blog.csdn.net/2203_75327677/article/details/...

            3.写在文件中

            在common.css文件中(一般放在static文件夹中)

            .c1 {color: red;}

            在.html文件中调用

            https://blog.csdn.net/2203_75327677/article/details/...        Document        

            用户注册

            https://blog.csdn.net/2203_75327677/article/details/...

            二、选择器

            1.ID选择器

            • 用id
              #c1 {color: red;}

              2.类选择器

              • 用class
                .c1 {color: red;}

                3.标签选择器

                • 直接对标签进行处理
                • 后续所有该标签全部使用该样式
                  p{color: red;}xxx

                  4.属性选择器

                  • 直接对标签的某种属性处理
                  • 后续所有该标签的该属性全部使用该样式

                    样例1

                        Document                input[type="text"]{        border: 1px solid red;    }    

                    样例2

                        .v1[xx="456"]{        color: gold;    }https://blog.csdn.net/2203_75327677/article/details/...https://blog.csdn.net/2203_75327677/article/details/...    a    b    chttps://blog.csdn.net/2203_75327677/article/details/...

                    5.后代选择器

                    • 让子孙都可以使用生效
                          .zz h2{        color:chartreuse;    }                        

                      我是p里面的h2

                      我是p外面的h2

                      https://blog.csdn.net/2203_75327677/article/details/...
                      • 只让子使用生效(只生效一层)
                            .zz > h2{        color:chartreuse;    }

                        6.样式覆盖

                        • 当一个标签引用多个样式,会出现属性重复的问题
                        • 第一种方式:在head中将优先级最高的写在后面(c3生效)
                              .c2 {        color: darkgoldenrod;    }    .c3 {        color:hotpink;    }    我是天才
                          • 第二种方式:在head中将优先级高的样式后面添加! important(c2生效)
                                .c2 {        color: darkgoldenrod !important;    }    .c3 {        color:hotpink;    }

                            三、CSS样式

                            1.高度和宽度

                            • 宽度支持百分比,但是高度不支持,因为高度具有不确定性
                            • 行内标签默认无效,块级标签默认有效
                              .c4 {        height: 300px;        width: 500px;    }

                              2.块级标签和行内标签转换

                              • display: inline-block;让行内标签对height和width生效
                              • 而且只占有需要大小,还是有行内标签的属性
                                .c4 {        display: inline-block;        height: 300px;        width: 500px;        border: 1px solid red;    }https://blog.csdn.net/2203_75327677/article/details/...联通
                                • 块级和行内标签相互转换
                                  移动    联通

                                  3.字体颜色/大小/粗细/样式/对齐/边框

                                          Document            .c1 {            color: #00FF7F;                   /* 字体颜色 */            font-size: 20px;                  /* 字体大小 */            font-weight: 600;                 /* 字体粗细 */            font-family: Microsoft Yahei;     /* 字体样式 */            text-align: center;               /* 水平方向居中 */            line-height: 50px;                /* 垂直方向居中 */            border: 1px solid red;            /* 边框 */        }    

                                  4.浮动

                                  • 如果在块级标签中,加入了float属性,那么这个块级标签奖不会再占用一整行,而是自己有多大就占用多大
                                  • 如果你让标签浮动起来之后,就会脱离文档流
                                  • 解决办法: 在同级子标签的最下面添加
                                            Document            .item {            float: left;            width: 280px;            height: 170px;            border: 1px solid red;        }                                            

                                    5.背景色

                                    • 颜色都有三种表达方式:HTML颜色代码、rgb(x,x,x)、颜色英文
                                      background-color: 颜色;

                                      6.内边距

                                      • padding-top | padding-left | padding-right | padding-botton
                                      • 其实上面的四个上下左右的padding可以简写为padding: 20px 20px 20px 20px,顺序为上右下左(顺时针方向)
                                      • 给自己增加一定距离,会使得该块所占空间增大
                                                Document            .outer {            border: 1px solid red;            height: 200px;            width: 200px;            padding-top: 20px;            padding-left: 20px;            padding-right: 20px;            padding-bottom: 20px;        }                hello        world            

                                        7.边界

                                        • border功能:设置边界的像素/样式/颜色
                                                  Document            .left {            float: left;        }        .c1 {            height: 200px;            width: 300px;            border: 3px dotted red;            margin: 50px;        }        .c2 {            height: 200px;            width: 300px;            border: 3px solid red;            border-left: 3px solid green;            margin: 50px;        }        .c3 {            height: 200px;            width: 300px;            margin: 50px;            background-color: bisque;            border-left: 2px solid transparent;  /* 透明色 */        }        .c3:hover {            border-left: 2px solid rgb(35, 211, 19);        }        我是虚线~    我是实线~左边框是绿色,上下右边框是红色    我是透明色,鼠标碰到我边框会变色哦~    

                                          8.外边距

                                          • 给自己和别人之间增加距离,视觉上本块不会增大
                                          • margin为外边距,及边框外到外部容器的距离,有四种参数设置方式:

                                            margin:5px;/四边外边距都为5px/

                                            margin:5px 10px;/上下外边距5px,左右外边距10px/

                                            margin:5px 10px 15px;/上外边距5px,左右外边距10px,下外边距15px/

                                            margin:5px 10px 15px 20px;/顺时针方向:上5px,右10px,下15px,左20px/

                                            margin:0 auto;/上下外边距为0,左右自动,实际效果为左右居中/

                                                    
                                            • margin/border/padding/content的关系

                                              9.鼠标悬停效果

                                              • hover功能:鼠标移动到此会改变某些样式,实现视觉效果
                                                        Document            .c1 {            color:brown;        }        .c1:hover {            color: green;            font-size: 20px;        }        .c2 {            width: 300px;            height: 300px;            border: 3px solid red;        }        .c2:hover {            border: 3px solid green;        }        .download {            display: none;        }        .app:hover .download {            display: block;        }        字体碰到鼠标变成绿色    边框碰到鼠标变成绿色            鼠标放我这里触发显示二维码                        

                                                10.内容增加

                                                • after功能:实现文本内容的增加
                                                          Document            .c1:after {            content: "大帅哥"        }        张三
                                                  • after一般像下面这样用,不用每次都写stype="clear: both;"
                                                            Document            .clearfix:after {            content: "";            display: block;            clear: both;        }        .item {            float: left;        }                1        2        3    

                                                    11.位置

                                                    • position
                                                    • fixed功能:固定在窗口的某个位置

                                                      样例1:返回顶部框

                                                      .back {     position: fixed;     width: 60px;     height: 60px;     border: 1px solid red;     right: 50px;     bottom: 50px; }

                                                      样例2:中间对话框

                                                      • opacity可以设置透明度
                                                                Document                    body {            margin: 0;        }        .dialog {            position: fixed;            height: 300px;            width: 500px;            background-color: white;/*left: 0; 和 right: 0; 分别将元素的左边界和右边界的定位都设为 0,这会使得元素水平居中对齐*//*而 margin: 0 auto; 则会使元素在水平方向上的外边距自动分配,从而实现水平居中*/            left: 0;            right: 0;            margin: 0 auto;                        /*竖直居中*/            top: 200px;            z-index: 1000;  /* 防止对话框被mask遮住 */        }        .mask {            background-color: black;            position: fixed;            left: 0;            right: 0;            top: 0;            bottom: 0;            opacity: 0.7;            z-index: 999;   /* 防止对话框被mask遮住 */        }                            
                                                        • relative和absolute功能:实现相对位置的摆放
                                                        • 父类标签写relative,子类写absolute,实现相对性
                                                          https://blog.csdn.net/2203_75327677/article/details/...        .app{            position: relative;        }        .app .download {            position: absolute;            display: none;            height: 100px;            width: 100px;        }        .app:hover .download {            display: block;        }                                            小米商城                MIUI                云平台                有品                小爱开放平台                app下载                                                                                                    登录                注册                消息通知            '                        https://blog.csdn.net/2203_75327677/article/details/...

上一篇:2017版财付通怎么收款?

下一篇:织梦CMS快速Logo更换教程,轻松定制你的站点标识!