CSS学习笔记

CSS学习笔记

1、导入方式

<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--内部样式-->
    <style>
        h1{
            color: red;
        }
    </style>

<!--    外部样式-->
    <link rel="stylesheet" href="css/style.ss">
</head>
<body>
<!--优先级:就近原则-->
<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<h1>我是标题</h1>
</body>

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

链接式

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

导入式

<style>
        @import "css/style.ss";
</style>

@import是依赖css的,存在一定的兼容问题,并且根据浏览器渲染机制来说,他在dom树渲染完成后才会渲染,并且不能被js动态修改。

相比之下link兼容性较好,且dom元素的样式可以被js动态修改,又因为link的权重高于@import,所以 @import适用于引入公共基础样式或第三方样式,link适用于自己写的且需要动态修改的样式

2、选择器(重要)

2.1、基本选择器

标签选择器

  <style>
    h1{
      /*标签选择器,会选择到页面上所有的这个标签的元素*/
      color: yellow;
      background: aliceblue;
      border-radius: 80px;
    }
  </style>

类选择器

<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
     /*类选择器的格式   .class的名称{}*/
    .wang{
      color: yellow;
    }
  </style>
</head>
<body>
<h1 class="wang">我是标题</h1>
</body>

id选择器

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        /*id选择器的格式   #id的名称{}*/
        #wang{
            color: red;
        }
    </style>
</head>
<body>
<h1 id="wang">我是标题</h1>
</body>

优先级:id>class>标签

2.2层次选择器

后代选择器 :在某个元素的后面

   /*后代选择器*/
       body p{
           background: green;
       }

子选择器 : 只能在同一层

  /*子选择器*/
       body>p{
           background: red;
       }

相邻兄弟选择器 :只有一个,相邻(向下)

  /*相邻兄弟选择器*/
       .active+p{
           background: yellow;
       }

通用选择器 : 当前选中元素的向下的所有兄弟元素

 /*通用兄弟选择器*/
       .active~p{
           background: aqua;
       }

2.3、结构伪类选择器

<!--    避免使用class,id选择器-->
    <style>
      /*ul的第一个子元素*/
      ul li:first-child{
          color: green;
      }
      /*ul的最后一个子元素*/
      ul li:last-child{
          color: yellow;
      }
    </style>

2.4、属性选择器(重要)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .demos a{
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: yellow;
            text-align: center;
            color: grey;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;
        }

        /*
        = 绝对等于
        *= 包含这个元素
        ^= 以这个开头
        $= 以这个结尾
        */

        /*a[id]{*/
        /*    background: green;*/
        /*}*/

        /*a[class*="links"]{*/
        /*    background: aqua;*/
        /*}*/

        /*a[href^=http]{*/
        /*    background: fuchsia;*/
        /*}*/

        a[href$=jpg]{
            background: aquamarine;
        }
    </style>
</head>
<body>
<p class="demos">
    <a href="http://www.baidu.com" class="links item first" id="first">1</a>
    <a href="" class="links item active" target="_blank" title="test">2</a>
    <a href="images/123.html">3</a>
    <a href="images/123.jpg">4</a>
    <a href="images/123.jpg">5</a>
    <a href="abc" class="links item">6</a>
    <a href="/abc" class="links item">7</a>
    <a href="koi" class="links item">8</a>
    <a href="gss" class="links item last">9</a>
</p>
</body>
</html>

3、美化网页元素

3.1为什么要美化网页

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

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

font-family:字体
font-size:字体大小
font-weight:字体粗细
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #study{
            font-size: 100px;
        }
    </style>
</head>
<body>
学习<span id="study">CSS</span>
</body>
</html>

3.2字体样式

font-weight:bolder;/*也可以填px,但数值不能超过900,900相当于bloder*/
/*常用写法:*/
font:oblique bloder 12px "楷体"

3.3文本样式

  1. 颜色–>color
  2. 文本对齐方式–>text-align:center
  3. 首行缩进–>text-indent:2em
  4. 行高–>line-height:300px;
  5. 下划线–>text-decoration
color:red;/*红色*/
text-align: center;/*居中*/
text-decoration:underline/*下划线*/
text-decoration:line-through/*中划线*/
text-decoration:overline/*上划线*/
text-decoration:none/*超链接去下划线*/

图片、文字水平对齐

img,span{vetical-align:middle}

3.4文本,阴影和超链接伪类

<style>
	a{/*超链接有默认的颜色*/
		text-decoration:none;/*下划线设置为空*/
		color:#000000;
	}
	a:hover{/*鼠标悬浮的状态*/
		color:orange;
	}
	a:active{/*鼠标按住未释放的状态*/
		color:green
	}
	a:visited{/*点击之后的状态*/
		color:red
	}
</style>

阴影:

/*	第一个参数:表示水平偏移
	第二个参数:表示垂直偏移
	第三个参数:表示模糊半径
	第四个参数:表示颜色
*/
text-shadow:5px 5px 5px 颜色

3.5列表ul li

/*list-style{
	none:去掉原点
	circle:空心圆
	decimal:数字
	square:正方形
}*/
ul li{
	height:30px;
	list-style:none;
	text-indent:1em;
}
a{
	text-decoration:none;
	font-size:14px;
	color:#000;
}
a:hover{
	color:orange;
	text-decoration:underline
}
/*放在div中,作为导航栏*/
<div id="nav"></div>
#nav{
	width:300px;
}

3.6列表ul li

/*list-style{
	none:去掉原点
	circle:空心圆
	decimal:数字
	square:正方形
}*/
ul li{
	height:30px;
	list-style:none;
	text-indent:1em;
}
a{
	text-decoration:none;
	font-size:14px;
	color:#000;
}
a:hover{
	color:orange;
	text-decoration:underline
}
/*放在div中,作为导航栏*/
<div id="nav"></div>
#nav{
	width:300px;
}

3.7背景

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            width: 1000px;
            height: 700px;
            border: 1px solid red;
            background-image: url("images/2.jpg");
        }
        .d1{
            background-repeat: repeat-x;
        }
        .d2{
            background-repeat: repeat-y;
        }
        .d3{
            background-repeat: no-repeat;
        }
    </style>
</head>
<body>
<div class="d1"></div>
<div class="d2"></div>
<div class="d3"></div>
</body>
</html>

4、盒子模型

4.1什么是盒子模型

  1. margin:外边距
  2. padding:内边距
  3. border:边框

4.2边框

border:粗细 样式 颜色

  1. 边框的粗细

  2. 边框的样式

  3. 边框的颜色

    测试代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    body{
      margin: 0;
    }
    h1{
      font-size: 20px;
      color: red;
        margin-top: 10px;

    }
    /* border: 粗细,样式,颜色*/
    #box{
        width: 300px;
        border: 1px solid red;
        margin: 0 auto;
    }
    form{
      background: green;
    }
    div:nth-of-type(1) input{
      border: 3px solid yellow;
        padding: 10px;
    }
    div:nth-of-type(2) input{
      border: 3px dashed  red;
    }
  </style>
</head>
<body>
<div id="box">
  <h1>京东会员</h1>
  <form action="">
       <div>
         <span>用户名:</span>
         <input type="text">
       </div>
       <div>
         <span>密&nbsp; &nbsp;码:</span>
         <input type="text">
       </div>
       <div>
         <span>邮&nbsp; &nbsp;箱:</span>
         <input type="text">
       </div>

  </form>
</div>
</body>
</html>

4.3外边距----妙用:居中

margin-left/right/top/bottom–>表示四边,可分别设置,也可以同时设置如下

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

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

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

margin:0;
padding:0;
text-decoration:none;
123

4.4圆角边框----border-radius,阴影

border-radius有四个参数(顺时针),左上->右上->右下->左下
圆圈:圆角=半径

box-shadow: 10px 10px 10px yellow;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            width: 500px;
            height: 500px;
            border: 10px solid red;
            border-radius: 0 0 100px 100px;
            box-shadow: 10px 10px 10px yellow;
        }
    </style>
</head>
<body>
<div></div>
</body>
</html>

5、浮动

5.1标准文档流

块级元素:独占一行 h1~h6 、p、div、 列表…
行内元素:不独占一行 span、a、img、strong

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

5.2、display(重要)

  • block:块元素
  • inline:行内元素
  • inline-block:是块元素,但是可以内联,在一行

这也是一种实现行内元素排列的方式,但是我们很多情况用float

  • 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: inline-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.3float:left/right左右浮动

clear:both

5.4overflow及父级边框塌陷问题

clear:
right:右侧不允许有浮动元素
left:左侧不允许有浮动元素
both:两侧不允许有浮动元素
none:

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

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

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

overflow:hidden/*隐藏*/
overflow:scoll/*滚动*/

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

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

小结:

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

5.5display与float对比

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

6、定位

6.1相对定位

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

top:-20px;
left:20px;
bottom:-10px;
right:20px;

<!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 red;
    }
    #first{
      background: green;
      border: 1px solid yellow;
        position: relative;
        top:20px;
    }
    #second{
      background: yellow;
      border: 1px solid green;
    }
    #third{
      background: aquamarine;
      border: 1px solid orange;
    }
  </style>
</head>
<body>
<div id="father">
  <div id="first">第一个盒子</div>
  <div id="second">第二个盒子</div>
  <div id="third">第三个盒子</div>
</div>
</body>
</html>

6.2绝对定位-absolute

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

 #second{
            background-color: green;
            border: 1px dashed #0ece4f;
            position: absolute;
            right:30px;
            top:30px
        }
        #third{

6.3固定定位-fixed

<!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>
posted @ 2021-05-23 10:49  努力敲码的旺仔  阅读(97)  评论(0编辑  收藏  举报