ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CSS

2022-08-07 10:34:52  阅读:86  来源: 互联网

标签:color 元素 height background border 选择器 CSS


1、什么是CSS

  1. CSS是什么
  2. CSS怎么用
  3. CSS选择器(重点+难点)
  4. 美化网页
  5. 盒子模型
  6. 浮动
  7. 定位
  8. 网页动画

1.1、什么是CSS

Cascading Style Sheet 层叠级联样式表

CSS:表现(美化网页)

字体、颜色、边距、高度、宽度、背景图片、网页定位、网页浮动...

1.2、发展史

CSS1.0

CSS2.0 DIV块+CSS,HTML与CSS结构分离思想

CSS2.1 浮动、定位

CSS3.0 圆角、阴影、动画

1.3、快速入门

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--规范,<style> 可以编写CSS代码,每一个声明,最好使用分号结尾
    语法:
        选择器{
            声明1;
            声明2;
            声明3;
            声明4;
        }

    <style>
        h1 {
            color: red;
        }
    </style>

    -->
    <link rel="stylesheet" href="CSS/style.css">

</head>
<body>

<h1>我是标题</h1>

</body>
</html>
h1 {
    color: aqua;
}

CSS的优势:

  1. 内容和表现分离
  2. 网页结构表现统一,可以实现复用
  3. 样式十分丰富
  4. 建议使用独立于HTML的CSS文件
  5. 利用SEO,容易被搜索引擎收录

1.4、CSS的三种收录方式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /* 内部样式 */
        h1 {
            color: red;
        }
    </style>

    <!--外部样式-->
    <link rel="stylesheet" href="CSS/style.css">

</head>
<body>

<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<!--优先级:就近原则-->

<h1>我是标题</h1>


</body>
</html>
h1 {
    color: yellow;
}

拓展:外部样式的两种写法

  • 链接式:

html

<!--外部样式-->
<link rel="stylesheet" href="CSS/style.css">
  • 导入式:

    @import是CSS2.1特有的

    <style>
        @import url("css/style.css");
    </style>
    

2、选择器

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

2.1、基本选择器

  1. 标签选择器:选择一类标签 标签{}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
      <style>
        /*
    
        */
        h1{
          color:aqua;
          background: burlywood;
          border-radius: 10px;
        }
        p{
          font-size: 50px;
        }
      </style>
    
    
    </head>
    <body>
    
    <h1>学Java</h1>
    <h1>学Java</h1>
    <p>学Java</p>
    
    </body>
    </html>
    
  2. 类选择器:选择所有class属性一致的标签,跨标签, .类名{}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <style>
            /* 类选择器的格式 .class的名称{}
                可以多个标签归类,是同一个class,可以复用
             */
            .java {
                color: green;
            }
    
            .c + + {
                color: aqua;
            }
        </style>
    
    </head>
    <body>
    
    <h1 class="java">java</h1>
    <h1 class="c++">c++</h1>
    <h1>python</h1>
    
    <p class="java">java</p>
    
    </body>
    </html>
    
  3. ID选择器:全局唯一! #id名{}

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
      <style>
        /*
        ID选择器:id必须保证全局唯一
        #id名称{}
        不遵循就近原则,固定的
        id选择器 > class选择器 > 标签选择器
         */
        #test{
            color: red;
        }
    
        .java{
            color: green;
        }
    
      </style>
    
    </head>
    <body>
    
    <h1 id="test">标题1</h1>
    <h1 class="java">标题2</h1>
    <h1>标题3</h1>
    <h1>标题4</h1>
    <h1>标题5</h1>
    
    </body>
    </html>
    

2.2、层次选择器

  1. 后代选择器:某个元素后面的所有

    /* 后代选择器 */
    body p {
        background: aqua;
    }
    
  2. 子选择器

    /* 子选择器 */
    body > p {
        background: red;
    }
    
  3. 相邻兄弟选择器

    /* 相邻兄弟选择器:只有一个,向下相邻 */
    .active + p {
        background: #7f72ff;
    }
    
  4. 通用选择器

    /* 通用选择器:当前元素的向下的所有兄弟元素 */
    .active ~ p {
        background: #ff9a34;
    }
    

2.3、结构伪类选择器

伪类:加上条件

/* ul的第一个元素 */
ul li:first-child {
    background: #a0ff1b;
}

/* ul的最后一个元素 */
ul li:last-child {
    background: #ffa928;
}

/* 选中p1:定位到父元素,选择当前的第一个元素
 选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效
 */
p:nth-child(2) {
    background: #ff5f31;
}

/* 选中父元素,下的p元素的第二个,类型 */
p:nth-of-type(1){
    background: #ff3c8f;
}

2.3、属性选择器(常用)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>

        .demo a {
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: #2700ff;
            text-align: center;
            color: gainsboro;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;
        }

        /* 属性名,属性名 = 属性值(正则)
        = 绝对等于
        *= 包含这个元素
        ^= 以这个开头
        $= 以这个结尾
        */
        /*id=first的元素*/
        a[id=first] {
            background: #ff3c8f;
        }

        /*class中有links的元素*/
        a[class*=links] {
            background: #ff3c8f;
        }

        /*选中href中以http开头的元素*/
        a[href^=http] {
            background: red;
        }

        /*选中以pdf结尾的*/
        a[href$=pdf] {
            background: red;
        }

    </style>

</head>
<body>

<p class="demo">

    <a href="https://www.baidu.com" class="links item first" id="first">1</a>
    <a href="http://" class="links item active" target="_blank" title="test">2</a>
    <a href="image/123.html" class="links item">3</a>
    <a href="image/123.png" class="links item">4</a>
    <a href="image/123.jpg" class="links item">5</a>
    <a href="abc" class="links item">6</a>
    <a href="/a.pdf" class="links item">7</a>
    <a href="/abc.pdf" class="links item">8</a>
    <a href="abc.doc" class="links item">9</a>
    <a href="abcd.doc" class="links item">10</a>

</p>

</body>
</html>

3、美化网页元素

3.1、为什么美化

  1. 有效传递网页信息
  2. 美化网页,吸引用户
  3. 凸显主题
  4. 提高用户体验

span标签:重点突出的字,用span套起来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        #title {
            font-size: 50px;
        }
    </style>

</head>
<body>

欢迎学习 <span id="title">Java</span>

</body>
</html>

3.2、字体样式

<!--    字体
    font-family:字体
    font-size:字体大小
    font-weight:字体粗细
    color:字体颜色
    
    颜色:
    单词
        RGB(red green blue) 0~F
        RGBA A:0~1
    -->
    <style>

        body {
            font-family: 楷体, "Arial Black";
            color: #7f72ff;
        }

        h1 {
            font-size: 50px;
        }

        p {
            font-weight: bold;
        }

        p1 {
            font: oblique bolder 12px "宋体";
        }

    </style>

3.3、文本样式

  1. 颜色:color RGB RGBA
  2. 文本对齐的方式:text-align
  3. 首行缩进:text-indent
  4. 行高:line-height
  5. 装饰:text-decoration
  6. 文本图片水平对齐:vertical-align
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--
    text-align:排版 居中
    text-indent:段落首行缩进
    height + line-height:行高和块高一致为上下居中

    -->
    <style>

        h1 {
            color: rgba(0, 255, 255, 0.5);
            text-align: center;
        }

        .p1 {
            text-indent: 2em;
        }

        .p3 {
            background: #2700ff;
            height: 30px;
            line-height: 30px;
        }

        /* 下划线 */
        .l1 {
            text-decoration: underline;
        }

        /* 中划线 */
        .l2 {
            text-decoration: line-through
        }

        /* 上划线 */
        .l3 {
            text-decoration: overline
        }

        /* 水平对齐 ~ 参照物 a, b */
        img, span {
            vertical-align: middle;
        }

    </style>

</head>
<body>

<p class="l1">123456</p>
<p class="l2">123456</p>
<p class="l3">123456</p>

<h1>起风了</h1>

<p class="p1">《起风了》原曲改编自日本歌手高桥优谱曲、作词并演唱的歌曲《ヤキモチ》</p>

<p class="p2">由买辣椒也用券演绎的《起风了》从器乐、编曲、到录音,百分百还原了温暖的日系旋律。</p>

<p class="p3">What else is the whole life of mortals but a sort of comedy, in which the various actors, disguised by
    various
    costumes and masks, walk on and play each one his part, until the manager waves them off the stage.</p>

<p>
    <img src="images/a.png" alt="">
    <span>这是月球图片</span>
</p>

</body>
</html>

3.4、超链接伪类

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        a {
            /* 默认的颜色 */
            text-decoration: none;
            color: rgba(6, 3, 6, 0.93);
        }

        /* 鼠标悬浮的颜色 */
        a:hover {
            color: orange;
            font-size: 40px;
        }

        /* 鼠标按住不释放的状态 */
        a:active {
            color: green;
        }

        /*
        text-shadow:阴影颜色 水平偏移 垂直偏移 阴影半径
        */
        #price {
            text-shadow: wheat 10px 10px 10px;
        }
        
    </style>

</head>
<body>

<a href="#">
    <img src="images/a.png" alt="">
</a>

<p>
    <a href="#">码出高效:Java开发手册</a>
</p>

<p>
    <a href="">作者:XXX</a>
</p>

<p id="price">
    ¥:25.00
</p>
</body>
</html>

3.5、列表

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="nav">
  <h2 class="title">全部商品分类</h2>
  <ul>
    <li>
      <a href="#">图书</a>
      <a href="#">音像</a>
      <a href="#">数字商品</a>
    </li>
    <li>
      <a href="#">家用电器</a>
      <a href="#">手机</a>
      <a href="#">数码</a>
    </li>
    <li>
      <a href="#">电脑</a>
      <a href="#">办公</a>
    </li>
    <li>
      <a href="#">家居</a>
      <a href="#">家装</a>
      <a href="#">厨具</a>
    </li>
    <li>
      <a href="#">服饰鞋帽</a>
      <a href="#">个性化妆</a>
    </li>
    <li>
      <a href="#">礼品箱包</a>
      <a href="#">钟表</a>
      <a href="#">珠宝</a>
    </li>
    <li>
      <a href="#">食品饮料</a>
      <a href="#">保健食品</a>
    </li>
    <li>
      <a href="#">彩票</a>
      <a href="#">旅行</a>
      <a href="#">充值</a>
      <a href="#">票务</a>
    </li>
  </ul>
</div>
</body>
</html>
#nav{
    width: 300px;
    background: antiquewhite;
}
.title{
    font-size: 18px;
    font-weight: bold;
    text-indent: 1em;/*缩进*/
    line-height: 35px;
    background: red;
}
/*ul li*/
/*
list-style:
    non 去掉实心圆
    circle 空心圆
    square 正方形
*/
/*ul{!*nav替换效果*!
    background: antiquewhite;
}*/
ul li{
    height: 30px;
    list-style: none;
    text-indent: 1em;
}
a{
    text-decoration: none;
    font-size: 14px;
    color: black;
}
a:hover{
    color: burlywood;
    text-decoration: underline;
}

3.6、背景

背景颜色

背景图片

<style>
    div {
        width: 1000px;
        height: 700px;
        border: 1px solid red;
        background-image: url("images/a.jpg"); /*默认是全部平铺的*/
    }

    .div1 {
        background-repeat: repeat-x;/*水平平铺*/
    }

    .div2 {
        background-repeat: repeat-y;/*垂直平铺*/
    }

    .div3 {
        background-repeat: no-repeat;/*不平铺*/
    }

</style>

3.7、渐变

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--径向渐变,圆形渐变-->
    <style>
        body {
            /*background-color: #21D4FD;*/
            background-image: linear-gradient(19deg, #21D4FD 0%, #B721FF 100%);
        }

    </style>

</head>
<body>
123456
</body>
</html>

4、盒子

4.1、什么是盒子模型

image

margin:外边距

padding:内边框

border:边框

4.2、边框

border:粗细 样式 颜色

  1. 边框的粗细
  2. 边框的样式
  3. 边框的颜色
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /*body总有一个默认的外边距margin:0*/
        body {
            margin: 0;
        }

        #box {
            width: 300px;
            border: 1px solid red;
        }

        h2 {
            font-size: 16px;
            background-color: beige;
            line-height: 30px;
            color: black;
        }

        form {
            background: beige;
        }

        div:nth-of-type(1) input {
            border: 3px solid black;
        }

        div:nth-of-type(2) input {
            border: 3px dashed #B721FF;
        }

        div:nth-of-type(3) input {
            border: 3px dashed #ff5f31;
        }
        
    </style>

</head>
<body>

<div id="box">
    <h2>会员登录</h2>
    <form action="#">
        <div>
            <span>用户名:</span>
            <input type="text">
        </div>
        <div>
            <span>密码:</span>
            <input type="text">
        </div>
        <div>
            <span>邮箱:</span>
            <input type="text">
        </div>
    </form>

</div>

</body>
</html>

4.3、内外边距

<style>
    div {
        margin: 0 0 0 0; /*分别表示上、右、下、左;从上开始顺时针*/
        /*例1:居中*/
        margin: 0 auto; /*auto表示左右自动*/
        /*例2:*/
        margin: 4px; /*表示上、右、下、左都为4px*/
        /*例3*/
        margin: 10px 20px 30px; /*表示上为10px,左右为20px,下为30px*/
    }
</style>

盒子的计算方式:
margin+border+padding+内容的大小

总结:
body总有一个默认的外边距 margin:0
常见操作:初始化

4.4、圆角边框

4个角

<style>
    div {
        width: 100px;
        height: 50px;
        margin: 30px;
        background: red;
        border-radius: 50px 50px 0px 0px;
    }
</style>

4.5、阴影

<style>
    div {
        width: 100px;
        height: 100px;
        border: 10px solid red;
        box-shadow: 10px 10px 100px yellow;
    }
</style>

5、浮动

5.1、标准文档流

块级元素:独占一行:独占一行 h1~h6 、p、div、 列表…

行内元素:不独占一行:不独占一行 span、a、img、strong

注: 行内元素可以包含在块级元素中,反之则不可以

5.2、display(重要)

  1. block:块元素
  2. inline:行内元素
  3. inline-block:是块元素,但是可以内联,在一行
  4. none:消失
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <!--block 块元素
      inline 行内元素
      inline-block 是块元素,但是可以内联 ,在一行
  -->
  <style>
    div{
      width: 100px;
      height: 100px;
      border: 1px solid red;
      display: block;
    }
    span{
      width: 100px;
      height: 100px;
      border: 1px solid red;
      display: inline-block;
    }
  </style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>
</html>

5.3、float

  1. 左右浮动:left/right

  2. 清除浮动元素:clear:left/right/both

5.4、父级边框塌陷问题

解决塌陷问题方案:
方案一:增加父级元素的高度;
方案二:增加一个空的div标签,清除浮动

<div class = "clear"></div>
<style>
	.clear{
		clear:both;
		margin:0;
		padding:0;
}
</style>

方案三:在父级元素中增加一个overflow属性

<style>

    #content {
        width: 200px;
        height: 150px;
        overflow: scroll;//hidden
    }
</style>

方案四:父类添加一个伪类:after

#father:after{
	content:'';
	display:block;
	clear:both;
}

小结:

  1. 浮动元素增加空div----> 简单、代码尽量避免空div
  2. 设置父元素的高度-----> 简单,但是元素假设有了固定的高度,可能就会超出范围
  3. overflow----> 简单,下拉的一些场景避免使用
  4. 父类添加一个伪类:after(推荐)----> 写法稍微复杂,但是没有副作用,推荐使用

5.5、display与float对比

  1. display:方向不可以控制
  2. float:浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题。

6、定位

6.1、相对定位

相对定位:positon:relstive;
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中!原来的位置会被保留

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--    相对定位
    相对于自己原来的位置进行偏移
    -->
    <style>
        div {
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }

        #father {
            border: 1px solid #666;
        }

        #first {
            background-color: #a0ff1b;
            border: 1px solid #ff5f31;
            position: relative; /* 相对定位:上下左右 */
            top: -20px;
            left: -20px;
        }

        #second {
            background-color: #B721FF;
            border: 1px solid #ff3c8f;
        }

        #third {
            background-color: #21D4FD;
            border: 1px solid #ffa928;
            position: relative;
            bottom: -10px;
            right: -10px;
        }
    </style>

</head>
<body>

<div id="father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>
</div>

</body>
</html>

相对定位练习:

img

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        #box {
            width: 300px;
            height: 300px;
            padding: 10px;
            border: 2px solid red;
        }

        a {
            width: 100px;
            height: 100px;
            text-decoration: none;
            background: #ff3c8f;
            line-height: 100px;
            text-align: center;
            color: white;
            display: block;
        }

        a:hover {
            background: rgba(1, 14, 99, 0.59);
        }

        .a2 {
            position: relative;
            left: 200px;
            top: -100px;
        }

        .a4 {
            position: relative;
            left: 200px;
            top: -100px;
        }

        .a5 {
            position: relative;
            right: -100px;
            top: -300px;
        }

    </style>

</head>
<body>
<div id="box">
    <a href="#" class="a1">链接1</a>
    <a href="#" class="a2">链接2</a>
    <a href="#" class="a3">链接3</a>
    <a href="#" class="a4">链接4</a>
    <a href="#" class="a5">链接5</a>
</div>
</body>
</html>

6.2、绝对定位

定位:基于xxx定位,上下左右~
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3、在父级元素范围内移动
总结:相对于父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留

#second {
    background-color: #B721FF;
    border: 1px solid #ff3c8f;
    position: absolute;
    left: 0;
    right: 0;
}

6.3、固定定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>

        body {
            height: 1000px;
        }

        div:nth-of-type(1) {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            right: 0;
            bottom: 0;
        }

        div:nth-of-type(2){
            width: 50px;
            height: 50px;
            background: yellow;
            position: fixed;
            right: 0;
            bottom: 0;
        }

    </style>

</head>
<body>

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

</body>
</html>

6.4、z-index

图层-z-index:默认是0,最高无限~999

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="CSS/style.css">
</head>
<body>

<div id="content">
    <ul>
        <li>
            <img src="images/a.jpg" alt="">
        </li>
        <li class="tipText">我在B站学Java</li>
        <li class="tipBg"></li>
        <li>时间:2022-8-6</li>
        <li>地点:重庆</li>
    </ul>

</div>

</body>
</html>
#content{
    width: 380px;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px solid yellow;
}
ul,li{
    padding: 0px;
    margin: 0px;
    list-style: none;
}
/*父级元素相对定位*/
#content ul{
    position: relative;
}
.tipText,.tipBg{
    position: absolute;
    width: 380px;
    height: 25px;
    top:216px
}
.tipText{
    color: white;
    z-index: 999;/*0~999*/
}
.tipBg{
    background: orange;
    opacity: 0.5;/*背景透明度*/
    filter: alpha(opacity=50);
}

标签:color,元素,height,background,border,选择器,CSS
来源: https://www.cnblogs.com/xiaojian1995/p/16558542.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有