CSS

什么是CSS

如何学习

  1. css是什么

  2. css怎么用

  3. css选择器(重点)

  4. 美化网页(文字美化,阴影,超链接,列表,渐变)

  5. 盒子模型

  6. 浮动

  7. 定位

  8. 网页动画(特效)w3c,菜鸟教程

  9. 仿网站

1.1什么是CSS

Cascading Style Sheet 层叠级联样式表

CSS:表现(美化网页)

字体,颜色,边距,高宽....

1.2发展史

CSS1.0

CSS2.0 DIV(自定义块)+CSS HTML与CSS结构分离思想,网页变简单

CSS2.1浮动,定位

CSS3.0圆角,阴影,动画....浏览器兼容性

2快速入门

<head>
   <meta charset="UTF-8">
   <title>第一个css</title>
<!--   规范,<style>可以编写css的代码
语法:
   选择器{
           声明1;
           声明2;
           声明3;
          }
-->
   <link rel="stylesheet" href="style.css">
</head>
<body>
<h1>我是标题</h1>
</body>


style.css
h1{
  color: aqua;
}

image-20220809134247919

css的优势:

  1. 内容和表现分离

  2. 网页结构表现统一,可以实现复用

  3. 样式十分丰富

  4. 建议使用独立的html的css

  5. 利用SEO,容易被搜索引擎收录

2.1CSS的三种导入方式

<head>
   <meta charset="UTF-8">
   <title>导入方式</title>
   <style>
       /*内部样式*/
       h1{
           color: aqua;
      }
   </style>
   <link rel="stylesheet" href="style.css">
</head>
<body>
<!--优先级:就近原则-->
<!--行内样式-->
<h1 style="color: red;">1</h1>

拓展:外部样式两种写法

  • 链接式xtml

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

 

  • 导入式CSS2.1特有的

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

3.选择器

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

3.1、基本选择器

1、标签选择器:选择一类标签

    <style>
       /*标签选择器会选择到页面上所有的这个标签的元素*/
       h1{
           color: aqua;
           background: red;
           border-radius: 30px;
      }
       p{
           font-size: 40px;
      }
   </style>
</head>
<body>
<h1>学java</h1>
<p>听狂神说</p>
</body>

 

2、类选择器 class:选择所有class属性一致的标签,跨标签

   <style>
       /*类选择器的格式 .class的名称{}
       好处:可以多个标签归类,是同一个class,可以复用
       */
      .qingjiang{
           color: aqua;
      }
      .kuanshen{
           color: red;
      }
   </style>
</head>
<body>
<h1 class="qingjiang">标题1</h1>
<h1 class="kuanshen">标题2</h1>
<h1>标题3</h1>
<p class="qingjiang">p标签</p>
</body>

 

3、id选择器:全局唯一

    <style>
       /*
       id选择器 必须保证全局唯一!
       #id名称{}
       不遵循就近原则,固定的
       id选择器>class 选择器>标签选择器
       */
       #qingjiang{
           color: aquamarine;
      }
   </style>
</head>
<body>
<h1 id="qingjiang">标题1</h1>
<h1>标题2</h1>
<h1>标题3</h1>
<h1>标题4</h1>
</body>

3.2层次选择器

1、后代选择器:在某个元素的后面 爷爷 爸爸 儿子

   <style>
       /*后代选择器*/
       body p{
           background: aliceblue;
      }
   </style>

 

2、子选择器 只有后面一代

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

 

3、相邻兄弟选择器 同辈

 /*兄弟选择器:只能有一个,向下*/
      .active+p{
          background: azure;
      }


<body>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>

 

4、通用选择器

.active~p{
          background: blueviolet;
      }
当前选中元素的向下的所有兄弟元素

3.2结构伪类选择器

伪类:条件

带冒号的一般是伪类

<style>
       /*ul的第一个子元素*/
       ul li:first-child{/*ul li 选中其中的元素*/
           background: blue;
      }
       /*ul的最后一个子元素*/
       ul li:last-child{
           background: blueviolet;
      }
    /*选中p1:定位到父元素,选择当前的第一个元素
      选择当前p元素的父级元素,选中父级元素的第一个,按顺序
      */
       p:nth-child(2){
           background: aqua;
      }
       /*按类型 按类型*/
       p:nth-of-type(2){
           background: burlywood;
      }
   </style>

image-20220809151733986

3.4属性选择器(常用)

id + class结合

= 绝对等于
*= 包含等于
^= (开头为什么什么的)等于
$= (结尾为什么什么的)等于
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       .demo a{
           float: left;
           display: block;
           height: 50px;
           width: 50px;
           border-radius: 10px;/*让边框变圆*/
           background: aqua;
           text-align: center;/*文本左右居中*/
           color: blue;
           text-decoration: none;
           margin-right: 5px;
           font: bold 20px/50px Arial;/*文本上下居中*/
      }
       /*存在id属性的元素   a[]{}* 属性名,属性名=属性值(正则)*/
       a[id]{
           background: red;
      }
       /*id存在first的元素*/
       a[id=first]{
           background: pink;
      }
       /*class中有links item 1的元素*/
       a[class="links item 1"]{
           background: bisque;
      }
       /*class中有links的元素*/
       a[class*="links"]{
           background: blueviolet;
      }
       /*选中href中以http开头的元素*/
       a[href^="http"]{
           background: coral;
      }
       /*选中href中以png结尾的元素*/
       a[href$="png"] {
           background: greenyellow;
      }
   </style>
</head>
<body>
<p class="demo">
   <a href="http://www.baidu.com" class=" item first" id="first">1</a>
   <a href="" class=" item active" target="_blank" title="test">2</a>
   <a href="image/123.html" class="links item" id="">3</a>
   <a href="image/123.png" class="links item">4</a>
   <a href="" class="links item 1">5</a>
   <a href="" class="links item 1">6</a>



</p>
</body>

image-20220809163317098

4 美化网页元素

4.1为什么要美化网页

1、有效的传递页面信息

2、美化网页,页面漂亮,才能吸引用户

3、凸显页面的主题

4、提高用户体验

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

<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <style>
       #title1{
           font-size: 50px;
      }
   </style>
</head>
<body>
欢迎学习<span id="title1">JAVA</span>
</body>

4.2字体样式

<!--
   font-family:字体
   font-size:字体大小
   font-weight: 字体粗细
   color;字体颜色
-->
   <style>
       body{
           font-family: "Agency FB",楷体 ;
           color: greenyellow;
      }
       h1{
           font-size: 50px;
      }
       .p1{
           font-weight: lighter;
      }
   </style>

4.3文本样式

1、颜色 color rgb rgba

2、文本对齐的方式 text-align =

3、首行缩进 text-indent:2em

4、行高line-height:

5、装饰 text-decoration

6、文本图片水平对齐 vertical-align:middle

<!--    颜色:单词
           RGB 0~F
           RGBA A:0~1
           text-align:排版居中
           text-indent:段落首行缩进
           行高,和 块的高度一致,就可以上下居中
水平对齐 参照物,a,b:
body中所选文字要用span标签选中
<style>
img,span{
vertical-align:middle中对齐
}
</style>
-->
   <style>
       h1{
           color: rgba(0,225,225,0.4);
           text-align: center;
      }
       .p1{
           text-indent: 2ex;
      }
       .p2{
           background: greenyellow;
           height: 300px;
           line-height: 100px;
      }
       /*下划线*/
       .a{
           text-decoration: underline;
      }
       /*中划线*/
       .b{
           text-decoration: line-through;
      }
       /*上划线*/
       .c{
           text-decoration: overline;
      }
       /*超链接去下划线*/
       text-decoration:none;
   </style>

4.4阴影

 /*text-shadow: 阴影的颜色,水平偏移,垂直偏移,阴影平移*/
      #price{
          text-shadow: blueviolet 10px 10px 2px;
      }

4.5超链接伪类

正常情况下,a:hover{}

 a:hover{
           color: green;
           font-size: 40px;
      }

4.6列表样式

/*ul li{} 选中ul下的所有li对象*/
ul li{
   list-style:none 去掉原点或者数字
list-style:circle 空心圆
list-style:decimal 数字
}
a{
   
}
a:hover{ 碰到文字变色
   
}

image-20220810095850196

<div>空标签)

image-20220810095932820

设置背景大小

4.7背景

背景颜色

背景图片

    <style>
       div{
           width: 1000px;
           height: 700px;
           border: 2px solid red;
           /*默认是平铺的*/
           background-image: url("1.JPG");
      }
       
  </style>
</head>
<body>
<div class="div1"></div>
</body>

image-20220810102333175

image-20220810102535029

4.8渐变

开源网站:www.grabient.com

image-20220810103248877

4.9盒子模型

image-20220810105437871

4.9.1什么是盒子模型

margin:外边距

padding:内边距

border:边框

4.9.2边框

1、边框的粗细

2、边框的样式

3、边框的颜色

 <style>
       /*body总有一个默认的外边距 margin:0 常见操作*/
       h1,ul,li,a,body{
            margin: 0;
           padding: 0;
           text-decoration: none;
      }

       #box{
           /*border:粗细,样式,颜色*/
           width: 300px;
           border:1px chartreuse;
      }

       /*标签选择器*/
       form{
           background: green;
      }


       /*默认情况下,border-width is 0和border-style is none
      因此,需要将它们设置为border-width:1px和border-style:solid。可以将所有边框属性组合为一个,如下所示:
#box {
  border:1px solid red
}
*/
        div:nth-of-type(1)>input{
           border: 3px solid black;
      }
        div:nth-of-type(2)>input{
           border: 3px solid blue;
      }
       h2{
           font-size: 16px;
           background: green;
           line-height: 30px;
           margin: 0;
           color: white;
           text-align: center;
      }
       .order{
           text-indent:1em ;
      }
  </style>
</head>
<body>
<div id="box">
  <h2>会员登录</h2>
  <form action="#">
      <div>
          <span>用户名:</span>
          <input type="text">
      </div>
      <div class="order">
          <span>密码:</span>
          <input type="text">
      </div>
      <div class="order">
          <span>邮箱:</span>
          <input type="text">
      </div>
  </form>
</div>
</body>

4.9.3内外边距

<!--外边距的妙用,居中元素   margin: 0 auto-->
#box{
           /*border:粗细,样式,颜色*/
           width: 300px;
           border:1px chartreuse;
           margin: 0 auto;
      }

       h2{
           font-size: 16px;
           background: green;
           line-height: 30px;
           margin: 0;
           color: white;
           margin: 2px 1px;
      }
       div:nth-of-type(1){
           padding-right: 2px;
      }

盒子的计算方式:你这个元素到底多大?

image-20220810134551091

4.9.4圆角边框

 

 

资源网站:源码之家下载image-20220810142433597

image-20220810142610966

5浮动

5.1标准文档流

image-20220810143110935

块级元素:独占一行

h1~h6  p  div  列表

 

行内元素:不独占一行

span a img strong

行内元素可以被包含在块级元素中

5.2display

<!--block 块元素
   inline 行内元素
   inline-block 是块元素,但是可以内联,在一行
-->
   <style>
       div{
           width: 100px;
           height: 100px;
           border:1px solid rebeccapurple;
           display: inline;
      }
       span{
           width: 100px;
           height: 100px;
           border:1px solid rebeccapurple;
           display: inline-block;
      }
   </style>
</head>
<body>
<div>div块元素</div>
<span>span行内元素</span>
</body>

1、也是一种实现行内元素排列的方式,但是很多情况都是用float

5.3float

<head>
   <meta charset="UTF-8">
   <title>浮动</title>
  重要***
   <link rel="stylesheet" href="style.css">
  ***链接css
</head>
<body>
<div id="father">
   <div class="layer01"><img src="../image/2.JPG" alt=""></div>
   <div class="layer02"><img src="../image/3.jpg" alt=""></div>
   <div class="layer03"><img src="../image/4.jpg" alt=""></div>
   <div class="layer04">
      这里有子mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
   </div>
</div>
</body>



.css:
div{
  margin: 10px;
  padding: 5px;
}
#father{
  border: 1px rebeccapurple solid;
}
.layer01{
  border: 1px green dashed;
  display: inline-block;
  float: left;
}
.layer02{
  border: 1px red dashed;
  display: inline-block;
  float: right;
}
.layer03{
  border: 1px gainsboro dashed;
  display: inline-block;
  float: right;
}
.layer04{
  border: 1px palegreen dashed;
  font-size: 12px;
  line-height: 23px;
  display: inline-block;
  float: right;
}

display: inline-block 把图片放在一行内
float: right 进行浮动操作

5.4父级边框塌陷的问题

clear

/*
clear:right; 右侧不允许有浮动元素
clear:left; 右侧不允许有浮动元素
clear:both; 两侧不允许有浮动元素
*/

解决问题:

1、增加父级元素的高度

#father{
   border: 1px rebeccapurple solid;
   height: 800px;
}

 

2、在html的body里增加一个空的div清除浮动,css中再加入图二所示

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

3、overflow

在父级元素中增加一个    overflow: hidden;

4、父类添加一个伪类 :after

#father:after{
   content: '';添加一个空内容
   display: block;让这个内容变成一个块,有高宽
   clear: both;
}

小结:

1、浮动元素后面增加空的div

简单,代码中尽量避免空div

2、设置父元素的高度

简单,元素假设有了固定的高度,会被限制

3、overflow

简单,下拉的一些场景避免使用

4、父类添加一个伪类 :after 推荐

6定位

6.1、相对定位

<!--    相对定位
       相对于自己原来的位置进行偏移
-->
   <style>
       body{
           padding: 20px;
      }
       div{
           margin: 10px;
           padding: 5px;
           font-size: 12px;
           line-height: 25px;
      }
       #father{
           background: #00DBDE;
           border: 1px solid rebeccapurple;
      }
       #first{
           background: #FC00FF;
           border: 1px dashed blue;
           position: relative;/*相对定位:上下左右*/
           top: -20px;
      }
       #second{
           background: rebeccapurple;
           border: 1px dashed greenyellow;
      }
       #third{
           background: burlywood;
           border: 1px dashed cyan;
      }
   </style>
</head>
<body>
<div id="father">
   <div id="first">第一个盒子</div>
   <div id="second">第二个盒子</div>
   <div id="third">第三个盒子</div>
</div>
</body>

相对定位:position:relative;

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

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

 

6.2、绝对定位

定位:基于xxx定位,上下左右

position:absoulte

1、没有父级元素定位的前提下,相对于浏览器定位

2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移

3、在父级元素范围内移动

相对于父级或浏览器进行指定的偏移,绝对定位的位置,它不在标准文档流中,原来的位置不会被保留

 /*绝对定位,相对于浏览器*/
      div:nth-of-type(1){
          width: 100px;
          height: 100px;
          background: #00DBDE;
          position: absolute;
          right: 0;
          bottom: 0;
      }

 

6.3、z-index

#content{
   width: 300px;
   padding: 0px;
   margin: 0px;
   overflow: hidden;
   font-size: 12px;
   line-height: 25px;
   border: 1px solid royalblue;
}
ul,li{
   padding: 0px;
   margin: 0px;
   list-style: none;
}
#content ul{
   position: relative;
}
.tipText,.tipBg{
   position: absolute;
   width: 300px;
   height: 25px;
   top: 80px;
}
.tipText{
   z-index: 999;   在第几层
}
.tipBg{
   background: palegreen;
   opacity: 0.5;/*背景的透明度*/
}

 

6.4、固定定位

 /*固定定位,fixed*/
      div:nth-of-type(2){
          width: 50px;
          height: 50px;
          background: cyan;
          position: fixed;
          right: 0;
          bottom: 0;
      }

动画

canvas动画网站



posted @ 2022-08-11 13:10  小新新Blog  阅读(38)  评论(0)    收藏  举报