前端CSS入门基础知识点总结(适合新手)

时间:2018-11-09
本文章向大家介绍前端CSS入门基础知识点总结(适合新手),需要的朋友可以参考一下

一、CSS

CSS(cascading style sheet,层叠样式表)定义如何显示HTML元素。

当浏览器读到一个样式表,它会按照这个样式表来对文档进行格式化(渲染)。

1、CSS语法

每个CSS样式由两个部分组成:选择器   +    声明(属性、属性值)    每个声明之后加;分号结束!

2、CSS注释

/* 注释内容 */

二、CSS的引入方式

1、行内样式

<!--行内式  是在标记的style 属性中设定CSS样式。尽量不要频繁使用-->


<p style="color: pink">这个一个p标签 内联样式是字体颜色粉色</p>

2、内部样式

<!--嵌入式是将CSS样式集中写在网页的<head></head>标签对的<style></style>标签对中。-->
<head>
    <meta charset="UTF-8">
    <style>
        h1 {color: pink}
    </style>
    <title>test练习</title>
</head>
<body>
<h1 id="p1">海燕</h1>
</body>
</html>

3、外部样式

<!--外部样式就是将css写在一个单独的文件中,然后在页面进行引入即可。推荐使用此方式-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="01样式.css">  link 即为引入外部样式
    <title>test练习</title>
</head>

三、CSS选择器

1、基本选择器

1、元素(标签)选择器:适用于批量的(统一,默认)的样式
p {color: sandybrown}

2、ID 选择器:给特定标签设置特定样式
#p1 {background-color: burlywood} 
(井号后跟 需渲染内容的 ID 号)

3、类 选择器:给某一些标签设置相同的样式
.c1 {color: antiquewhite;font-size: 18px}
.p1 { font-size: 29px}
(在HTML标签中有一个class属性,c1 和 p1 对应名称的class)

4、通用选择器
* {font-size: 14px}
/*适用所有元素*/

ps:

样式类名不要用数字开头(有的浏览器不认)。

标签中的class属性如果有多个,要用空格分隔。

2、组合选择器

1、后代选择器
 p内部的a标签设置字体颜色 <p>空格<a>:p标签内部嵌套的所有a标签(包括儿子,孙子等等))
p a {
     color: red;
 }

2、儿子选择器
选择所有父级是<div>元素的<p>元素:div标签内子标签是p标签的生效(只找儿子辈))
div>p {
    font-family: "Arial Black","arial-black",cursive;
 }

3、毗邻选择器
(选择所有紧挨着<div>之后的<p>标签:找的是<p>标签(条件:同级的上面有个<div>))
div+p {
    margin: 5px;
}

4、弟弟选择器
(#p1(id=p1的)后面所有的兄弟p标签)
#p1~p {
    color: gold
}

3、属性选择器

1、用于选取带有指定属性的元素。
p[title] {
  color: red;
}
2、用于选取带有指定属性和值的元素。
p[title="123"] {
  color: green;
}


对应下面2个HTML生效
<p title="xxx">咕咕咕</p>
<p title="123">略略略</p>
/*找到所有title属性以hello开头的元素*/
[title^="hello"] {
  color: red;
}

/*找到所有title属性以hello结尾的元素*/
[title$="hello"] {
  color: yellow;
}

/*找到所有title属性中包含(字符串包含)hello的元素*/
[title*="hello"] {
  color: red;
}

/*找到所有title属性(有多个值或值以空格分割)中有一个值为hello的元素:*/
[title~="hello"] {
  color: green;
不怎么常用的属性选择器

4、分组和嵌套

/*1、分组(当多个元素的样式相同的时候,我们没有必要重复地为每个元素都设置样式,
       可以通过在多个选择器之间使用逗号分隔的分组选择器来统一设置元素样式)*/
div, (一定要加逗号(不然就变成后代了))
p{
    color: sandybrown;
}

/*2、嵌套(多种选择器可以混合起来使用(空格,>,+,~),比如:.c1类内部所有p标签设置字体颜色为红色。)*/
.c1 p {
    color: red;
 }

5、伪类选择器

/* 未访问的链接 */
a:link {
  color: #FF0000
}

/* 已访问的链接 */
a:visited {
  color: #00FF00
} 

/* 鼠标移动到链接上 */
a:hover {
  color: #FF00FF
} 

/* 选定的链接 */ 
a:active {
  color: #0000FF
}

/*input输入框获取焦点时样式*/
input:focus {
  outline: none;
  background-color: #eee;
}

6、伪元素选择器

1、first-letter: 常用的给首字母设置特殊样式:

p:first-letter{
    font-size: 48px;
    color: red;
}

2、before:在每个p元素之前插入内容

 p:before{
    content: "*";
    color: red;
 }

3、 after:在每个p元素之后插入内容
 
 p:after{
   content: "[?]";
    color: blue;
 }

before和after多用于清除浮动

四、选择器的优先级

1、CSS继承

  继承是CSS的一个主要特征,它是依赖于祖先-后代的关系的。继承是一种机制,它允许样式不仅可以应用于某个特定的元素,还可以应用                   于它的后代。例如一个body定义了的字体颜色值也会应用到段落的文本中。

body {
    color: red;
}
此时页面上所有标签都会继承body的字体颜色。然而CSS继承性的权重是非常低的,是比普通元素的权重还要低的0。
我们只要给对应的标签设置字体颜色就可覆盖掉它继承的样式。

此外,继承是CSS重要的一部分,我们甚至不用去考虑它为什么能够这样,但CSS继承也是有限制的。有一些属性不能被继承,如:border, margin, padding, background等。


具体的选择器权重计算方式如下图:

除此之外还可以通过添加 !important方式来强制让样式生效,但并不推荐使用。因为如果过多的使用!important会使样式文件混乱不易维护。

万不得已可以使用!important

2、样式文件优先级(重要)

  1. 越靠近标签的优先级越高(就近原则)

  2. 权重的计算

    1. 内联样式 1000

    2. ID选择器 100

    3. 类选择器 10

    4.元素选择器 1

五、CSS字体和文字属性相关

1、通用的属性:宽和高(width(宽度) 和 height(高度))

width 属性 可以为元素设置宽度。

height 属性可以为元素设置高度。

块级标签才能设置宽度,内联标签的宽度由内容决定。

2、文字字体

font-family可以把多个字体名称作为一个“回退”系统来保存。如果浏览器不支持第一个字体,则会尝试下一个。浏览器会使用它可识别的第一个值。

* {
  font-family: "Microsoft Yahei", "微软雅黑", "Arial", sans-serif
}

3、字体大小和粗细

@字体大小设置
p {
  font-size: 14px;    如果设置成 inherit 表示继承父元素的字体大小值。
}
@字体粗细设置
p {
    font-weight: normal;  默认值,标准粗细
    font-weight:  bold;   粗体
    font-weight:  bolder; 更粗
    font-weight:  lighter;更细
    font-weight:  100-900;设置具体粗细,400等同于normal,700等同于bold
    font-weight:  inherit;继承父元素字体的粗细值
}

4、文本颜色

.c1 {
    color: aqua;
}

颜色属性被用来设置文字的颜色。

颜色是通过CSS最经常的指定:

  • 十六进制值 - 如: FF0000
  • 一个RGB值 - 如: RGB(255,0,0)
  • 颜色的名称 - 如:  red

还有rgba(255,0,0,0.3),第四个值为alpha, 指定了色彩的透明度/不透明度,它的范围为0.0到1.0之间。

5、文字对齐

text-align 属性规定元素中的文本的水平对齐方式。

p {text-align: center;}

6、文字装饰

text-decoration 属性用来给文字添加特殊效果。

 

常用的为去掉a标签默认的下划线:

a { text-decoration: none; }

首行缩进:

p { text-indent: 28px; }
首行缩进28像素

六、CSS背景和边框属性

1、背景属性

1.背景颜色
background-color: red;
  背景图片
background-image: url("头像.jpg") ;

2. 背景重复
 repeat(默认):背景图片平铺排满整个网页
 repeat-x:  背景图片只在水平方向上平铺
 repeat-y:  背景图片只在垂直方向上平铺
 no-repeat: 背景图片不平铺

background-repeat: no-repeat; 

3.背景位置
background-position: right top;
background-position: 200px 200px;

可简写为:

background:  url("头像.jpg") no-repeat center top ;

使用背景图片的一个常见案例就是很多网站会把很多小图标放在一张图片上,然后根据位置去显示图片。减少频繁的图片请求。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>滚动背景图示例</title>
    <style>
        * {
            margin: 0;
        }
        .box {
            width: 100%;
            height: 500px;
            background: url("头像.jpg") no-repeat center center;
            background-attachment: fixed;
        }
        .c1 {
            height: 500px;
            background-color: tomato;
        }
        .c2 {
            height: 500px;
            background-color: steelblue;
        }

    </style>
</head>
<body>
    <div class="c1"></div>
    <div class="box"></div>
    <div class="c2"></div>
</body>
</html>
鼠标滚动,背景不变

2、边框属性

1、边框属性有(border - width、border-style、bordercolor)

 通常简写成:

.c2 {
    border: aqua solid 3px;
}

2、边框的样式:

 

3、单独为某一个边框设置样式:

#i1 {
  border-top-style:dotted;
  border-top-color: red;
  border-right-style:solid;
  border-bottom-style:dotted;
  border-left-style:none;
}

4、圆角边框效果

将border-radius设置为长或高的一半即可得到一个圆形

border-radius: 50%;

 3、display 属性

用于块级元素行内元素转换(控制元素显示效果)

display:"none"与visibility:hidden的区别:不常用!

visibility:hidden: 可以隐藏某个元素,但隐藏的元素仍需占用与未隐藏之前一样的空间。也就是说,该元素虽然被隐藏了,但仍然会影响布局。

display:none: 可以隐藏某个元素,且隐藏的元素不会占用任何空间。也就是说,该元素不但被隐藏了,而且该元素原本占用的空间也会从页面布局中消失

七、CSS盒子模型

  • margin:            用于控制元素与元素之间的距离;margin的最基本用途就是控制元素周围空间的间隔,从视觉角度上达到相互隔开的目的。
  • padding:           用于控制内容与边框之间的距离;   
  • Border(边框):     围绕在内边距和内容外的边框。
  • Content(内容):   盒子的内容,显示文本和图像。

 

1、margin 外边距

.c1 {
  margin-top:5px;   顶部
  margin-right:10px;右边
  margin-bottom:15px;底部
  margin-left:20px; 左边
}

可简写为:

.c1 {
  margin: 5px 10px 15px 20px;
}
顺序:上>右>下>左

最常居中使用!!!

.c1 { margin: 0 auto;}

2、padding 内填充

.c1 {
  padding-top: 5px;
  padding-right: 10px;
  padding-bottom: 15px;
  padding-left: 20px;
}
简写和顺序 与 margin 一致!!!

补充padding的常用简写方式:

  • 提供一个,用于四边;
  • 提供两个,第一个用于上-下,第二个用于左-右;
  • 如果提供三个,第一个用于上,第二个用于左-右,第三个用于下;
  • 提供四个参数值,将按上-右-下-左的顺序作用于四边;

八、浮动与清除浮动、处理溢出

1、浮动(float)

在 CSS 中,任何元素都可以浮动。

浮动元素会生成一个块级框,而不论它本身是何种元素。

关于浮动的两个特点:

  • 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
  • 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>布局</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .c1 {
            height: 1000px;
            width: 20%;
            background-color: red;
            float: left;
        }

        .c2 {
            height: 1000px;
            width: 80%;
            background-color: green;
            float: left;
        }
    </style>
</head>
<body>

<div class="c1"></div>
<div class="c2"></div>
</body>
</html>
浮动页面布局示例

left:向左浮动

right:向右浮动

none:默认值,不浮动

2、清除浮动(clear)

clear属性规定元素的哪一侧不允许其他浮动元素

ps:clear属性只会对自身起作用,而不会影响其他元素。

 浮动有一个副作用:父标签塌陷

清除主要有三种方式:

  • 固定高度
  • 伪元素清除法
  • overflow:hidden
/*伪元素清除浮动(最常用)*/
#clearfix:after {
    content: "";
    clear: left;
    display: block;
    }

3、溢出(overflow)

  • overflow(水平和垂直均设置)
  • overflow-x(设置水平方向)
  • overflow-y(设置垂直方向)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>头像示例</title>
    <style>
        .header-img {
            border: 1px solid pink;
            border-radius: 50%;
            height: 128px;
            width: 128px;
            overflow: hidden;
            margin: 0 auto;
        }
        img {
            max-width: 100%;
        }
    </style>
<body>
<div class="header-img">
    <!--头像img需在div之内-->
    <img src=" http://tx.haiqq.com/uploads/allimg/150325/1222515561-5.jpg" alt="">
</div>
</body>
</html>
头像示例

九、定位(position)

  1. static     -----> 默认
  2. relative  -----> 相对定位(相对于原来的位置)
  3. absolute-----> 绝对定位(相对于最近的一个被定位过的的祖宗标签)
  4. fixed      -----> 固定在某个位置(返回顶部按钮)

left     right    top     bottom

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定位练习</title>
    <style>
        * { margin: 0;padding: 0 }
        .c1,
        .c2,
        .c3,
        .c4{
            height: 150px;
            width: 150px;
        }
        .c1 {
            background-color: red;
        }
        .c2 {
            background-color: #ff6700;
            position: relative;
            left: 150px; ;
        }
        .c3 {
            background-color: lightgreen;
        }
        .c4 {
            background-color: coral;
            position: absolute;
            left: 300px;
            top: 150px;
        }
        #d1 {
            background-color: pink;
            bottom: 20px;right: 20px;
            position: fixed;
            font-size: 14px;
        }
    </style>
</head>
<body>
<div class="c1">c1</div>
<div class="c2">c2</div>
<div class="c3">c3</div>
<div class="c4">c4</div>
<div id="d1">返回顶部</div>
</body>
</html>
定位示例

ps:脱离文档流的3种方式:

  1、 float  浮动 

  2、absolute   绝对定位

  3、fixed  固定      

十、元素透明度与层叠顺序

1、opacity (元素透明度)

取 0---1 之间的值 ;

和rgba()的区别:
  1:opacity 改变元素以及子元素的透明度

  2:rgba()只改变背景颜色的透明度

2、z-index(层叠顺序设置)

1、数值越大,越靠近你

2、只能作用于定位过的元素

<!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>
模态框示例

3、顶部导航栏练习

<!DOCTYPE HTML>
<html>
<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;
      padding: 0;
    }
    a {
      text-decoration: none; /*去除a标签默认的下划线*/
    }

    .nav {
      background-color: black;
      height: 40px;
      width: 100%;
      position: fixed;
      top: 0;
    }

    ul {
      list-style-type: none; /*删除列表默认的圆点样式*/
      margin: 0; /*删除列表默认的外边距*/
      padding: 0; /*删除列表默认的内填充*/
      display: inline-block;
    }
    /*li元素向左浮动*/
    li {
      float: left;
    }

    li > a {
      display: block; /*让链接显示为块级标签*/
      padding: 0 1