CSS 学习笔记

什么是CSS

  • Cascading Style Sheet 层叠级联样式表

  • CSS:表现(美化网页)

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

CSS的导入方式:

  • 链接式:

    html

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

    @import是CSS2.1特有的!

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

CSS的基础语法:

  • CSS的语法非常简单
选择器{
声明1;
声明2;
声明3;
}	注意每个声明都要以分号结尾

选择器设定方式:

  • 标签选择器:选择一类标签
<标签名>{
声明1;
声明2;
声明3;
}
<h1>..
<h2>..
<h1>..
  • 类选择器:选择一类名
.class(名称){
    声明1;
    声明2;
    声明3;
}
<h1 class = "...">..
<h2 class = "...">..
<h3 class = "...">..
  • id选择器,选择唯一的标签

    注意id的值全局唯一,且不能以数字开头

    #idname{
    声明1;
    声明2;
    声明3;
    }
    <h1 id = "...">..
    <h1 id ="。。。">..
    

优先级:id>class>标签

层次选择器:

1.后代选择器

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

2.子选择器

/*子选择器,此处指body 标签下的第一级p标签才会置为红色*/
body>p{
background:red;
}

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

.classname + p{		/*特指某些类的标签*/
background:red;
}

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

.classname ~ p{
background:red;
}

结构伪类选择器:

所有带冒号的都是伪类

  • 一个选择器可以同时选择多个元素
 <style>
        ul li:last-child{
            background:chartreuse;
        }
        ul li:first-child{
            background:red;
        }
    </style>
  <link rel="stylesheet" href="Demo01.css">
    <style>
        /*ul 的第一个子元素*/
        ul li:last-child{
            background:chartreuse;
        }
        /*ul 的最后一个子元素*/
        ul li:first-child{
            background:red;
        }

        /*选中p标签父元素的第一个子元素(迂回)
        选择当前p元素的父级元素的第一个,并且是当前元素才生效!

        */
        p:nth-child(3){
            background: #1260d4;
        }
        p:nth-of-type(1){
            background: #12d494;
        }
    </style>

在这里插入图片描述

属性选择器:

  • 属性选择器是前端部分的重中之重

  • 改进:将id 和 class 结合,使选择器更加强大

  • 可以结合正则表达式来使用属性选择器

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

  id = first的元素
  a[id = first]{
    background:yellow;
  }

   class 中有links的元素:
   a[class *= "links"]{
      background: yellow;
    }

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

美化网页元素

为什么要美化网页

  • 有效传递页面元素
  • 美化页面元素
  • 凸显主题
  • 提高用户体验

span标签: 一些重点的字,用span标签容纳

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>页面元素美化</title>
    <style>
        #title1{
            font-size: 50px;
        }
    </style>
</head>
<body>

欢迎学习
<span id ="title1">java</span>

</body>
</html>

字体样式:

<style>
  body{
    font-family: 楷体;
  }
  h1{
    font-size: 50px;
  }
  p{
    font-weight: bold;
    color: blue;
  }
</style>

文本样式

  • 颜色 color rgb rgba
  • 文本对其方式 text-align:center
  • 首行缩进 text-indent:2em(两个字符长度)
  • 行高 (line-height):单行文字上下居中:line-height = height :行高等于height !
  • 装饰(text-decoration)
  • 文本图片水平对齐: certical-align:middle (注意选择器中要有两个选项,位置是相对的,应该是相对对齐)

超链接伪类

    <style>
        /*默认的颜色*/
        a{
            text-decoration: none;
            color: aqua;
        }
        /*鼠标悬停后的颜色*/
        a:hover{
            color: red;
            font-size: 50px;
        }
        a:active{
            color: #12d494;
        }
        a:visited{
            color: antiquewhite;
        }
        /*阴影颜色 水平偏移 垂直偏移 阴影半径*/
        #name{
            text-shadow:peru 10px 10px 2px;
        }
    </style>
</head>
<body>
<a href="123132321">
    <img src="../Source/img/a.png" alt="">
</a>
<p>
    <a href="#"id="name">麦克斯奥特曼</a>
</p>
<p>
    <a href="#">M78星云</a>
</p>

</body>

列表

#nav{
    width: 500px;
}
li[data-cid = "0"]{
    font-size: 18px;
    font-weight: bold;
    text-indent: 1em;
    line-height: 30px;
    background: #12d494;
}
/*
list-style:none:去掉列表前面的点
*/
ul li{
    background: #12d494;

    height: 50px;
    list-style: square;
    text-indent: 1em;
}
li:hover{
    color: orange;
}

背景图片

背景颜色
backgroundcolor
背景图片
#nav{
    width: 500px;
}
li[data-cid = "0"]{
    font-size: 18px;
    font-weight: bold;
    text-indent: 1em;
    line-height: 30px;
    background: #12d494;
}
/*
list-style:none:去掉列表前面的点
*/
ul li{
    background: #12d494;
    background-image:url("../Source/img/a.png") ;
    background-repeat: no-repeat;
    background-postion:50px 2px ;
    height: 50px;
    list-style: square;
    text-indent: 1em;
}
li:hover{
    color: orange;
}
渐变

url :Gradient Hunt - Beautiful Color Gradients (一个渐变材料的开源网站)

  • 我们可以直接从这个网站挑选自己喜欢的渐变颜色复制代码到我们的css中
    background-image: radial-gradient( circle farthest-corner at 10% 20%,  rgba(14,174,87,1) 0%, rgba(12,116,117,1) 90% );

效果演示:

在这里插入图片描述

盒子模型

在这里插入图片描述

什么是盒子模型

Margin: 外边距

Padding:内边距

Border:边框

边框

  • 边框的粗细
  • 边框的样式
  • 边框的颜色
  <style>
    /*border: 粗细,样式,颜色*/
    body{
      margin: 0;
    }
    #box{
      width: 300px;
      border:1px solid red ;
    }
    form{
      background:olivedrab;
    }
    div:nth-of-type(1) input{
      border:3px solid blue;

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

    }
  </style>

盒子模型Html:(原则就是尽量的使用div标签来分块)

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

  </form>

</div>

外边距

    /*margin的顺序是顺时针顺序: 上右下左*/    
h2{
      background: aqua;
      color: antiquewhite;
      line-height: 30px;
      margin: 1px 1px 2px 3px;
    }

圆角边框

边框有4个脚

  <style>
    /*左上、右上、右下、左下 角的顺序是顺时针的四个角
    圆圈: 圆角= 半径
    */
div{
  width: 100px;
  height: 100px;
  border: 10px solid blue;
  border-radius: 100px 100px 100px 100px;
}
    img{
      border-radius: 100px 100px 100px 100px;
    }
  </style>

盒子阴影:

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

浮动

标准文档流

在这里插入图片描述

块级元素:独占一行

h1~h5 div p

行内元素:不独占一行

  • 行内元素可以被包含在块级元素中,但是不能包含块级元素

DisPlay

<!--
        block 块元素
        inline 行内元素
        inline-block 是块元素但是保持着行内元素的特性
        

-->
    div{
      width: 100px;
      height: 100px;
      border: 1px solid black;
      display: inline-block;
    }
    /*inline-block -->即是行内元素也是块元素*/

    span{
      width: 100px;
      height: 100px;
      border:1px solid black;
      display:none;/*行内元素和块元素的转换 是块元素,但是可以内联在一行*/
        /*转为块元素*/
    }

浮动 Float

div{
    margin: 10px;
    padding: 5px;
}
#father{
    border: 2px #000 solid;
}
.layer01{
    border: 2px #F00 dashed;
    display: inline-block;
    float: right;

}
.layer02{
    border: 2px #00F dashed;
    display: inline-block;
    float: right;

}
.layer03{
    border: 2px #060 dashed;
    display: inline-block;
    float: right;

}
/*
    clear:right 右侧不允许有浮动
    clear:left 左侧不允许有浮动
    clear:both 两侧不允许有浮动

*/
.layer04{
    border: 2px #666 dashed;
    font-size: 12px;
    line-height: 23px;
    display: inline-block;
    float: right;
    clear: right;

}
  • 对应的Html:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>浮动</title>
    <link rel="stylesheet" href="float.css" type="text/css">
</head>
<body>
<div id="father">
  <div class="layer01"><img src="../Source/img/1.png" alt=""></div>
  <div class="layer01"><img src="../Source/img/2.png" alt=""></div>
  <div class="layer01"><img src="../Source/img/3.png" alt=""></div>
    <div class="layer04">
    浮动的盒子可以向右浮动也可以向左浮动,直到它的外边缘触碰到包含边框在内的块元素
    </div>
</div>
</body>
</html>

父级边框塌陷

clear:

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

解决方案:

1.增加父级元素的高度

2.增加一个空的div

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

3.Overflow

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

4.父类添加一个伪类

#father:after{
    content: "";
    display: block;
    clear: both;
}
.layer

可以避免空div的情况

小结

  • 浮动元素后加空的div

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

  • 设置父级标签的的高度

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

  • Overflow

    简单,下拉了一些场景(尽量避免使用)

  • 父类下面添加一个伪类

    简单、有效(非常推荐、没有副作用)

对比

  • display: 方向不可控制
  • float : 要解决父级边框塌陷的问题

定位

相对定位

相对于原来的位置进行指定的偏移:

top、left、bottom、right

由于是相对位置进行移动所以移动的方向是和显示 的方向是相反的

  <style>
    div{
      margin: 10px;
      padding: 5px;
      font-size: 12px;
      line-height: 25px;

    }
    #father{
      border:2px solid #12d494;
    }
    #first{
      border:2px dashed #12d494;
      background: yellow;
      position: relative;
      top:-20px;
      left: 20px;
    }
    #second{
      border:2px dashed #12d494;
      background: yellow;

    }
    #third{
      border:2px dashed #12d494;
      background: yellow;
      bottom: -20px;

    }
  </style>

练习题:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    .father{
      width: 300px;
      height: 300px;
      border: 2px solid black;
      padding: 10px;
    }
    a{
      width: 100px;
      height: 100px;
      text-decoration: none;
      background: #12d494;
      line-height: 100px;
      text-align: center;
      display: block;
    }
    a:hover{
      background: antiquewhite;
    }
    #a2 {
      position: relative;
      left: 200px;
      top: -100px;

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

    }
    #a5{
      position: relative;
      left: 100px;
      top: -300px;
    }

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

</div>

</body>
</html>

在这里插入图片描述

绝对定位

基于XXX定位 上下左右…

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

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

    #first{
      border:2px dashed #12d494;
      background: yellow;
      position: absolute;
      top:-20px;
      left: 20px;
    }
z-index

在这里插入图片描述

  • 透明度可以直接用opacity设置

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    #content{
      margin: 0px;
      padding: 0px;
      overflow: hidden;
      font-size: 12px;
      line-height: 25px;
      border: 2px #0000FF solid;
    }
    ul,li{
      padding: 0px;
      margin: 0px;
      list-style: none;
    }
    /*父级元素相对定位*/
    #content ul{
      position: relative;
    }
    .tiptext,tipbg{
      position: absolute;
      width: 300px;
      border: 2px black solid;
    }
  </style>
</head>
<body>
<div id="content">
  <ul>
    <li><img src="../Source/img/1.png" alt=""></li>
    <li class="tiptext">黑丝真好看</li>
    <li class="tipbg">时间: 2099 年 老色皮</li>
    <li>地点: 海南大学</li>

  </ul>
</div>
</body>
</html>
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值