简说css

1.CSS介绍:

CSS(Cascading Style Sheet层叠样式表)用于装饰html网页的
当浏览器读到一张样式表,它就会按照这个样式表对浏览器进行渲染。

2.CSS语法

2.1 CSS实例
每个CSS样式由两部分:选择器与声明。声明包括属性与属性值,每个声明之后用分号结束
选择器 {属性:值;属性:值}
2.2CSS注释:
/注释内容/

3.0CSS的几种引入方式

3.1行内样式
行内式是在标记的style属性中设定CSS样式。不推荐大规模使用

、<p style="color :red";> hello world </p>
<div style="color:orange">lady 我是一个块</div>

3.2内部样式
将css样式写在网页的head标签内,嵌入在内,格式如下:

<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<style>
	p{
		background-color:orange
		}

3.3外部样式
通过link href来将css文件引入到html文件中:具体实现如下:

<link href="mystyle.css"  rel="stylesheet"    type="text/css"/ >

4.0CSS选择器

4.1基本选择器
元素选择器:根据标签的名字来对标签进行设置

 span {
color:pink;
}

ID选择器:通过标签的id值来对标签进行装饰或者说修饰

#id1 {
	font-size:18px
}

设置id为id1的标签的字体为18像素

类选择器:

.c1{
font-size:14px
}

span .c1{color:red;}

注类选择器有个点哟
通用选择器:

*{
color:red;
}

综合代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        span {
            font-size: 20px;
            color: pink;
        }
        #id标签{
            color: red;
        }
        .c{
            color: orange;
        }
        * {color: red}
    </style>
</head>
<body>
<span>
    我是第一个span
</span>
<div id="id标签">
    我是一个div块标签
</div>
<p class="c">
    我是通过类标签的变为橘色
</p>

</body>
</html>


<!--基本选择器:
id选择器    通过标签的id属性来对标签中的内容设置属性值
标签选择器  标签名字{属性:属性值}
类选择器 在标签中添加类属性。class = "c1"
通用选择器:所有的标签均用该属性,一般不用。

-->

4.2组合选择器:
后代选择器:

span内部的所有的a便签设置字体的颜色为绿色(原谅色)
span  a {
color :green
}

儿子选择器:

div>p{
	font-size:18px;
}

毗邻选择器:
选择所有紧接着

元素之后的

元素(意思是可以有多个)

div+p{
	margin:5px;
}

弟弟选择器:
选择div之后的所有的p标签

  div~p{
   color:red;}

属性选择器:
用于选择指定元素,也可以添加指定的标签

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>属性选择器</title>
    <style>
    input [name="user"]
    {
        color: red;
    }
</style>
</head>
<body>
<label  for="username">username</label>
<input type="text"  id ='username' name="user">
</body>
</html>

<!--
属性选择器是通过标签内的属性来设置标签的值的

-->

分组和嵌套:
当多个元素的样式相同时,我们没必要重复为每个元素设置样式,我们可以将多个元素用逗号隔开然后进行统一设置样式

div,p,a {color:red;}

上面的代码是将div ,p, a中的标签统一设置为红色

嵌套:
多种选择器通过空格分隔混合起来使用
将div中的所有的a,以及p标签的字体都设置为红色

div a,p{
color:red;
}

伪类选择器

未访问的链接
a:link{
color:red;
}
鼠标移动到链接上
a:hover{
color:orange;
}
点击在元素上,选定的链接
a:active{
color:yellow;
}
已访问的链接
a:visited{
color:green;
}

伪元素选择器

#first-letter before , after
first-letter 常用给首字母或者第一个字设置特殊样式

p:first-letter{
font-size:32px;
color:red;
}
before
在元素的开头添加内容
p: before{
content:"开头"
color:orange;
}
after
在元素的最后添加内容:
p:after{
content :  "ending";
color:blue;
}

CSS选择器的优先级:
行内样式>id选择器>类选择器>元素选择器

  1. 内联或者说是行内权重1000
  2. id选择器的权重为100
  3. 类选择器的权重为10
  4. 元素选择器的权重为1
    示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>选择器优先级</title>
<style>
    /*#d1{color: aqua*/
        /*}*/

    .cl{
        color: deeppink;
    }
      p{
        color: orange;
    }
    </style>
</head>
<body>
    <p id="d1" class="c1"  style=”“>加油没有什么能够阻挡</p>

</body>
</html>

<!--优先级
行内的style优先级>id的优先级 >类选择器>标签选择器
就是你的标签选择器放在离你修饰的标签最近的地方,
但是类选择器距离你的修饰的标签远一点,显示的属性是类所给的属性值
-->

5CSS属性相关:

宽和高:
width属性可以为元素设置宽度
height 可以为元素设置高度。
块标签才能设置宽度,内联标签的宽度由内容决定。
为块设置边框并设置

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>属性</title>
    <style>
        div {
            weight:100px;
            height: 100px;
            border: red solid 10px;
            text-align: center;
            /*text-t*/
        }
   p{
            font-size: inherit;
        }

p则继承父类的元素样式
    </style>
</head>

<body>
<div>hello</div>
</body>
</html>

字体属性
文字的大小:

p{
	font-size:18px
}

文本颜色:

  1. 16进制
  2. RGB三原色 RGB(0,0,255)
    3.颜色名称 red

文字对齐:
text-align:规定元素中文本的水平方式:

  1. left 左对齐
  2. right :右对齐
  3. center居中
  4. justify 两端对齐
    文字装饰:
    text-decoration属性给文字添加特效:
    none 默认,定义标准的文本
    underline 定义文本下的一条线
    overline 定义文本上的一条线
    line-through 定义穿过文本的一条线
    inherit 继承父类的text-decoration
    去掉a标签的默认下划线:
a{
	text-decoration:none
}

首行缩进:

p{
	font-size:16px;
	text-indent :32px;
}

背景属性:

/*背景颜色*/
background-color :red;
background-image"url("1.jpg")
背景重复
repeat(默认)背景图片平铺满整个网页
repeat-x:背景只是水平方向向上平铺
repeat-y:背景只是垂直方向平铺
background-repeat

示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片背景实例</title>
    <style>
        .c1{
            height: 400px;
            background-color: tomato;
        }
        .box{
            height: 400px;
            background: url("http://k.zol-img.com.cn/sjbbs/7692/a7691515_s.jpg");
            background-attachment: fixed;
            /*让图片保持在一个位置*/
        }
        .c2{
            height: 400px;
            background-color: red;
        }
        .c3{
            height: 400px;
            background-color: fuchsia;
        }
    </style>
</head>
<body>
<div class="c1"></div>
<div class="box"></div>
<div class="c2"></div>
<div class="c3"></div>
</body>
</html>
 
<!--背景滚动模式 fixed
设置背景色。
background-attachment: fixed;设置图片不动
-->

边框:
边框属性:
border-width 边框高度
border-style 边框类型 solid dashed(方形虚线) dotted (点虚线)
border-color:red

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>边框</title>
    <style>
        div {
            border: 5px dashed green;
            /*border:;*/
            text-align: center;
            /*内容居中*/
        }
    </style>
</head>
<body>
<div>div</div>
</body>
</html>
<!--边框是border
border 10px 宽度  solid 实线 dashed虚线
border 有宽度,可以通过 text-align来将块中的内容居中

-->

border-radius:

用这个属性实现圆角边框的效果
将border-radius设置为长或者高的一半即可获得一个圆形。
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>画圆</title>
    <style>
        div{

            width: 400px;
            height: 400px;
            background-attachment: fixed;
            background-color: red;
            border: 4px solid black;
            border-radius: 50%;
        }

    </style>
</head>
<body>
<div></div>

</body>
</html>

display属性:
用于控制html元素的显示效果:

  1. display:none:
  2. display:block 默认占满整个屏幕宽度,如果设置了宽度,则会用margin填充剩下的部分
  3. display:inline 按行内元素显示,此时设置元素的宽度高度等属性均不影响
  4. display:inline-block 使得元素同时具有行内元素和块级元素的特点
    display:none 不占据任何空间
    display:hidden占据显示的空间
    示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>display属性</title>
    <style>
        div {display: none;
        }
        div {
            display: inline;
        }

        /*div {*/
            /*display: block;*/
        /*}*/
        /*span{*/
            /*display: block;*/
        /*}*/
    </style>
</head>
<body>
<div>div</div>
<div>div</div>
<span>span</span>
<span>span</span>
</body>
</html>

盒子模型:

  1. margin:用于控制元素与元素之间的距离;margin控制元素周围空间的间隔,从视觉上看是分隔开了。
  2. padding 用于控制内容与边框之间的距离
  3. border(边框):围绕在内边距和外边距的外框
  4. content(内容):盒子的内容,显示文本和图像

margin示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .t1{
            border: 3px red solid;
            width:200px;
            height: 300px;
    }
        .t2{
            border: 3px green solid;
            width: 100px;
            height: 150px;
            margin: 75px auto;
        }
    </style>

</head>
<body>
<div class="t1">
    <div class="t2"></div>
</div>

</body>
</html>

padding代码块如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .t1{
            border: 3px red solid;
            width:200px;
            height: 300px;
    }
        .t2{
            border: 3px green solid;
            width: 100px;
            height: 150px;

            padding: 50px 10px 10px 20px;
             margin: 75px auto;
        }
    </style>

</head>
<body>
<div class="t1">
    <div class="t2">哎呦不错哦</div>
</div>
</body>
</html>		                                                    

浮动 float

任何元素都可以浮动,
浮动元素生成一个块级框,而不论其本身何种元素
关于浮动的两个特点:
浮动的框可以从左向右移动,直到它的外边缘碰到包含另一个浮动框边框为止。
left:向左浮动
right:向右浮动
none:默认是不浮动

clear

clear 属性规定元素的哪一侧不允许其他浮动元素。
left:在左侧不允许浮动元素
right:在右侧不允许浮动元素
both:在左右两侧

注意:clear属性只会对自身起作用,而不会影响其他元素。
清除浮动
清除浮动的副作用(父标签塌陷问题)

伪元素清除法(使用较多):

.clearfix:after {
content: “”;
display: block;
clear: both;
}
overflow溢出属性:
值 描述
visible 默认值。内容不会被修剪,会呈现在元素框之外。
hidden 内容会被修剪,并且其余内容是不可见的。
scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。
auto 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
inherit 规定应该从父元素继承 overflow 属性的值。
overflow(水平和垂直均设置)
overflow-x(设置水平方向)
overflow-y(设置垂直方向)

定位:

static
static 默认值,无定位,不能当作绝对定位的参照物,并且设置标签对象的left、top等值是不起作用的的。

relative(相对定位)
相对定位是相对于该元素在文档流中的原始位置,即以自己原始位置为参照物。有趣的是,即使设定了元素的相对定位以及偏移值,元素还占有着原来的位置,即占据文档流空间。对象遵循正常文档流,但将依据top,right,bottom,left等属性在正常文档流中偏移位置。而其层叠通过z-index属性定义。

注意:position:relative的一个主要用法:方便绝对定位元素找到参照物。

absolute(绝对定位)
定义:设置为绝对定位的元素框从文档流完全删除,并相对于最近的已定位祖先元素定位,如果元素没有已定位的祖先元素,那么它的位置相对于最初的包含块(即body元素)。元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样。元素定位后生成一个块级框,而不论原来它在正常流中生成何种类型的框。

重点:如果父级设置了position属性,例如position:relative;,那么子元素就会以父级的左上角为原始点进行定位。这样能很好的解决自适应网站的标签偏离问题,即父级为自适应的,那我子元素就设置position:absolute;父元素设置position:relative;,然后Top、Right、Bottom、Left用百分比宽度表示。

另外,对象脱离正常文档流,使用top,right,bottom,left等属性进行绝对定位。而其层叠通过z-index属性定义。

   .language-right{
      position: absolute;
      left: 100px;
        margin: auto;
    }
      #language{
            width: 100px;
            height: 100px;
            position: relative;
            top: 350px;
        }

fixed
fixed:对象脱离正常文档流,使用top,right,bottom,left等属性以窗口为参考点进行定位,当出现滚动条时,对象不会随着滚动。
示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>返回顶部示例</title>
  <style>
    * {
      margin: 0;
    }

    .d1 {
      height: 1000px;
      background-color: #eeee;
    }

    .scrollTop {
      background-color: darkgrey;
      padding: 10px;
      text-align: center;
      position: fixed;
      right: 10px;
      bottom: 20px;
    }
  </style>
</head>
<body>
<div class="d1">111</div>
<div class="scrollTop">返回顶部</div>
</body>
</html>

是否脱离文档流:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        .c1 {
             height: 50px;
            width: 100px;
            background-color: dodgerblue;
        }
        .c2 {
             height: 100px;
            width: 50px;
            background-color: orange;
            position: relative;
            left: 100px;
        }
    </style>
</head>
<body>
<div class="c1"></div>
<div class="c2"></div>
<div style="height: 100px;width: 200px;background-color: black"></div>
</body>
</html>

绝对定位:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        .c1 {
            height: 50px;
            width: 100px;
            background-color: red;
            position: relative;
        }
        .c2 {
            height: 50px;
            width: 200px;
            background-color: green;
            position: absolute;
            left: 50px;
        }
    </style>
</head>
<body>
<div class="c1">购物车
    <div class="c2">空空如也~</div>
    <div style="height: 50px;width: 100px;background-color: deeppink"></div>
</div>

</body>
</html>

固定定位:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="c1" style="height: 50px;width: 500px;background-color: black"></div>
<div class="c2" style="height: 50px;width: 100px;background-color: deeppink;position: fixed;right: 10px;bottom: 20px"></div>
<div class="c3" style="height: 10px;width: 100px;background-color: green"></div>

</body>
</html>

z-index
设置对象的层叠顺序。

1.z-index 值表示谁压着谁,数值大的压盖住数值小的,
2.只有定位了的元素,才能有z-index,也就是说,不管相对定位,绝对定位,固定定位,都可以使用z-index,而浮动元素不能使用z-index
3.z-index值没有单位,就是一个正整数,默认的z-index值为0如果大家都没有z-index值,或者z-index值一样,那么谁写在HTML后面,谁在上面压着别人,定位了元素,永远压住没有定位的元素。
4.从父现象:父亲怂了,儿子再牛逼也没用

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="x-ua-compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>自定义模态框</title>
  <style>
    .cover {
      background-color: rgba(0,0,0,0.65);
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 998;
    }

    .modal {
      background-color: white;
      position: fixed;
      width: 600px;
      height: 400px;
      left: 50%;
      top: 50%;
      margin: -200px 0 0 -300px;
      z-index: 1000;
    }
  </style>
</head>
<body>

<div class="cover"></div>
<div class="modal"></div>
</body>
</html>

index越大越在前面显示,显示在第一层的z-index值最大
opacity
用来定义透明效果。取值范围是0~1,0是完全透明,1是完全不透明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值