CSS学习

152 阅读8分钟

什么是CSS:

  1. CSS 指层叠样式表 (Cascading Style Sheets)
  2. 样式定义如何显示 HTML 元素
  3. 样式通常存储在样式表中
  4. 把样式添加到 HTML 4.0 中,是为了解决内容与表现分离的问题
  5. 外部样式表可以极大提高工作效率
  6. 外部样式表通常存储在 CSS 文件中
  7. 多个样式定义可层叠为一个

CSS的快速入门:

1. 最简单的方式:运用style标签  每一个声明后,最好使用分号结尾
   语法:
        选择器{
              声明一;
              声明二;
              声明三;
        }
2. html和css分开写(建议使用),然后在html用link标签关联css,href存放的是css文件的位置

两种方式简单示例:

html文件:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>

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

<style>
   h1{
       color: blue;
   }
</style>

<body>

<h1>标题</h1>

</body>
</html>

CSS文件:

h1{
    color: crimson;
}

CSS的导入方式:

- 行内样式:在标签元素中,编写一个style属性
- 内部样式:在html内部通过style标签书写CSS代码
- 外部样式:html和CSS独立分开书写代码

==> 优先级:就近原则(谁离得近就执行谁的)

导入方式举例:

HTML文件

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

<!--导入式-->
<style>
    @import "CSS/style.css";
</style>

<!--优先级:就近原则-->
<body>
<!--第一种:行内样式:在标签元素中,编写一个style属性-->
<h1 style="color: chartreuse">标题</h1>


</body>
</html>

CSS文件:

h1{
    color: crimson;
}

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

  1. 链接式:
<link rel="stylesheet" href="CSS/style.css">
  1. 导入式:
<style>
    @import "CSS/style.css";
</style>
  1. 区别:linkimport语法结构不同,linkhtml标签,只能放入html源代码种,import可看作是CSS样式,作用是引入CSS样式功能。

基本选择器

  1. 标签选择器:选择一类标签
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<!--标签选择器-->
<style>
    h1{
        color: chartreuse;
    }
</style>


<body>

<h1>标题1</h1>
<h1>标题2</h1>
<h1>标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>

</body>
</html>
  1. 类选择器 class:选择所有的class属性一致的标签,可跨标签使用 通过 .类名{} 的方式创建
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<!--类选择器-->
<style>
    .blue{
        color: aquamarine;
    }
</style>

<body>

<h1 class="blue">标题1</h1>
<h1 class="blue">标题2</h1>
<h1>标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>

</body>
</html>
  1. ID选择器:全局唯一:通过 #ID名称{} 的方式进行创建
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<!--ID选择器-->
<style>
    #red{
        color: red;
    }
</style>

<body>

<h1 id="red">标题1</h1>
<h1>标题2</h1>
<h1>标题3</h1>
<h1>标题4</h1>
<h1>标题5</h1>

</body>
</html>

优先级:ID选择器 > 类选择器 > 标签选择器

层次选择器

以下面这个网页标签图设计:

  1. 后代选择器:在某个元素后面 爷爷 爸爸 我 我儿子
body p{
	background: red;
}
  1. 子选择器:下一代 儿子
body>p{
	background: green;
}
  1. 弟弟选择器:同辈的下一个 弟弟
.active+p{
	background: black;
}
  1. 通用选择器:当前选中元素的向下的所有兄弟元素
body~p{
	background: yellow;
}

总体代码预览:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<!--后代选择器-->
<style>
    body p{
        background: red;
    }
</style>

<!--子选择器-->
<style>
    body>p{
        background: green;
</style>

<!--弟弟选择器-->
<style>
    .active+p{
        background: black;
    }
</style>

<!--通用选择器-->
<style>
    .active~p{
        background: yellow;
    }
</style>

<body>

<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
    <li>
        <p>p4</p>
    </li>
    <li>
        <p>p5</p>
    </li>
    <li>
        <p>p6</p>
    </li>
</ul>


</body>
</html>

效果展示:

结构伪类选择器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<style>
    /*ul的第一个子元素*/
    ul li:first-child{
        background: red;
    }

    /*ul的最后一个子元素*/
    ul li:last-child{
        background: green;
    }

    /* 选中p1:定位到父元素,选择当前的第一个元素
    选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效!
    */
    p:nth-child(1){
        background: yellow;
    }

    /* 选中父元素下的p元素的第二个,类型
    */
    p:nth-of-type(1){
        background: aqua;
    }

</style>



<body>
<h1>h1</h1>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<ul>
    <li>li1</li>
    <li>li2</li>
    <li>li3</li>
</ul>

</body>
</html>

效果展示:

属性选择器(重要,建议使用)

以下面这个网页标签图设计:

设计代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<style>
    .demo a{
        float: left;
        display: block;
        height: 50px;
        width: 50px;
        border-radius: 10px;
        background: #1177d0;
        text-align: center;
        color: aliceblue;
        text-decoration: none;
        margin-right: 5px;
        font:bold 20px/50px Arial;
    }
</style>


<p class="demo">
    <a href="http:www.baidu.com" class="links item first" id="first">1</a>
    <a href="a.jpg" class="item">2</a>
    <a href="a.doc" class="links item">3</a>
    <a href="a.pdf" class="links item">4</a>
    <a href="images/123.html" class="links item">5</a>
    <a href="images/123.jpg" class="links item">6</a>
    <a href="a.html" class="links item">7</a>
    <a href="a.css" class="links item">8</a>
    <a href="a.java" class="links item last">9</a>
</p>





</body>
</html>

效果展示:

=====>属性选择器的玩法:

标签名[属性名=属性值()或者直接写属性名]{}

  1. 将存在id属性的元素背景改成黄色:
/*法一*/
<style>
    a[id]{
        background: yellow;
    }
</style>

效果: 2. 将id属性为first的元素背景改成黄色:

<style>
    a[id=first]{
        background: yellow;
    }
</style>

效果: 3. 将class中只有items的元素背景改成黄色:

<style>
    a[class="item"]{
        background: yellow;
    }
</style>

效果:

4. 将class中有links的元素背景改成黄色:

<style>
    a[class*="links"]{
        background: yellow;
    }
</style>

效果:

5. 将href中以http开头的元素背景改成黄色:

<style>
    a[href^="http"]{
        background: yellow;
    }
</style>

效果:

  1. 将以jpg结尾的元素背景改成黄色:
<style>
    a[href$="jpg"]{
        background: yellow;
    }
</style>

效果:

小结:

  1. = 绝对等于
  2. *= 包含这个元素,所有
  3. ^= 以什么开头
  4. $= 以什么结尾

美化网页元素

  1. 为什么要美化网页?
  • 有效的传递页面信息
  • 美化网页,页面漂亮才能吸引客户
  • 凸显页面的主题
  • 提高用户的体验
  1. span标签:重点要突出的字,使用span标签套起来
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #title1{
            font-size: 50px;
        }
    </style>
</head>
<body>

学习语言<span id="title1">JAVA</span>
</body>
</html>
  1. 字体样式
  • font-family:字体
  • font-size:字体大小
  • font-weight:字体粗细
 <style>
	body{
		font-family:楷体;
		color:red;
	}
	h1{
		font-size50px;
}
.p1{
		font-weight:blod;
	
	}
</style>

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

  1. 文本样式
颜色–>color
文本对齐方式–>text-align:center
首行缩进–>text-indent:2em
行高–>line-height:300px;
下划线–>text-decoration
text-decoration:underline/*下划线*/
text-decoration:line-through/*中划线*/
text-decoration:overline/*上划线*/
text-decoration:none/*超链接去下划线*/
图片、文字水平对齐->img,span{vetical-align:middle}
  1. 文本,阴影和超链接伪类 重点:a:hover
<style>
	a{/*超链接有默认的颜色*/
		text-decoration:none;
		color:#000000;
	}
	a:hover{/*鼠标悬浮的状态*/
		color:orange;
	}
	a:active{/*鼠标按住未释放的状态*/
		color:green
	}
	a:visited{/*点击之后的状态*/
		color:red
	}
</style>
  1. 阴影:
	第一个参数:表示水平偏移
	第二个参数:表示垂直偏移
	第三个参数:表示模糊半径
	第四个参数:表示颜色
text-shadow:5px 5px 5px 颜色
  1. 列表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;
}
  1. 背景
背景颜色:background
背景图片
background-image:url("");/*默认是全部平铺的*/
background-repeat:repeat-x/*水平平铺*/
background-repeat:repeat-y/*垂直平铺*/
background-repeat:no-repeat/*不平铺*/

结合使用:background:red url("图片相对路劲") 270px 10px no-repeat background-position:/定位:背景位置/

  1. 渐变(径向渐变、圆形渐变)
background-color:#FFFFFF
background-image:linear-gradient(115deg #FFFFFF 0% #6284FF 50% #FF0000 100%)

网址:www.grablent.com 可以下载别人已经创建好的渐变颜色样例。

附加一道小题目来巩固: 设计下面这个样式:

参考代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>小列表</title>

    <style>
        .title{
            line-height: 50px;
            height: 300px;
            width: 357px;
            margin:0;
        }

        h1{
            background: orangered;
            text-align: center;
            font-size: 40px;
            margin-bottom: -20px;
        }

        ul{
            padding-left: 0px;
        }

        li{
            list-style: none;
            text-indent: 1em;
            font-size: 25px;
            margin-top: 0;
            margin-left: 0;
            background-image: url("images/r.png");
            background-repeat: no-repeat;
            background-position-x: 331px;
            background-position-y: 15px;
            background: blue;
        }

        a{
            text-decoration: none;
            color: #8e8686;
        }
        a:hover{
            color: orange;
        }

    </style>

</head>
<body>
<div class="title">
    <h1>主题市场</h1>
    <ul>
        <li>
            <a href="#">女装</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">内衣</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">家居</a>
        </li>
        <li>
            <a href="#">女鞋</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">男鞋</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">箱包</a>
        </li>
        <li>
            <a href="#">母婴</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">童鞋</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">玩具</a>
        </li>
        <li>
            <a href="#">男装</a>&nbsp;&nbsp;/&nbsp;&nbsp;
            <a href="#">运动户外</a>
        </li>

    </ul>
</div>


</body>
</html>

运行效果:

盒子模型

  1. 什么是盒子模型
margin:外边距
padding:内边距
border:边框
  1. 边框
border:粗细 样式 颜色
边框的粗细
边框的样式
边框的颜色
  1. 外边距----妙用:居中
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*/

总结:

  1. body总有一个默认的外边距 margin:0
  2. 常见操作:初始化
  • margin:0;
  • padding:0;
  • text-decoration:none;
  1. 盒子的计算方式:margin+border+padding+内容的大小
  1. 圆角边框----border-radius
border-radius有四个参数(顺时针),左上开始
圆圈:圆角=半径

5、浮动

块级元素:独占一行 h1~h6pdiv、 列表…
行内元素:不独占一行 spanaimgstrong
注: 行内元素可以包含在块级元素中,反之则不可以。
  1. display(重要)
block:块元素
inline:行内元素
inline-block:是块元素,但是可以内联,在一行
这也是一种实现行内元素排列的方式,但是我们很多情况用float
none:消失
Title div{ width: 100px; height: 100px; border: 1px solid red; display: inline-block; } span{ width: 100px; height: 100px; border: 1px solid red; display: inline-block; }
div块元素
span行内元素 在这里来一个小练习,制作下面这个界面:

参考代码:

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>QQ会员</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="wrap">
    <!--头部-->
    <header class="nav-header">
        <div class="head-contain">
            <a href="" class="top-logo"><img src="img/logo.png" width="145" height="90" /></a>
            <nav class="top-nav">
                <ul>
                    <li><a href="">功能特权</a> </li>
                    <li><a href="">游戏特权</a> </li>
                    <li><a href="">生活特权</a> </li>
                    <li><a href="">会员特权</a> </li>
                    <li><a href="">成长体系</a> </li>
                    <li><a href="">年费专区</a> </li>
                    <li><a href="">超级会员</a> </li>
                </ul>
            </nav>
            <div class="top-right">
                <a href="">登录</a>
                <a href="">开通超级会员</a>
            </div>
        </div>
    </header>
</div>
</body>
</html>

css代码:

*{
    padding:0;
    margin: 0;
}
a{
    text-decoration: none;
}
.nav-header{
    height: 90px;
    width: 100%;
    background: rgba(0,0,0,.6);
}
.head-contain{
    width: 1180px;
    height: 90px;
    margin: 0 auto;
    text-align: center;
}
.top-logo,.top-nav,.top-nav li,.top-right{
    height: 90px;
    display: inline-block;
    vertical-align: top;
}
.top-nav{
    margin: 0 48px;
}
.top-nav li{
    line-height: 90px;
    width: 90px;
}
.top-nav li a{
    display: block;
    text-align: center;
    font-size: 16px;
    color: #fff;
}
.top-nav li a:hover{
    color: blue;
}

.top-right a{
    display: inline-block;
    font-size: 16px;
    text-align: center;
    margin-top: 25px;
    border-radius: 35px;
}
.top-right a:first-of-type{
    width: 93px;
    height: 38px;
    line-height: 38px;
    color: #fad65c;
    border: 1px #fad65c solid;
}
.top-right a:first-of-type:hover{
    color: #986b0d;
    background: #fad65c;
}
.top-right a:last-of-type{
    width: 140px;
    height: 40px;
    font-weight: 700;
    line-height: 40px;
    background: #fad65c;
    color: #986b0d;
}
.top-right a:last-of-type:hover{
    background: #fddc6c;
}
  1. float:left/right左右浮动

clear:both

  1. overflow及父级边框塌陷问题
clearright:右侧不允许有浮动元素
left:左侧不允许有浮动元素
both:两侧不允许有浮动元素
none:没有浮动

解决塌陷问题方案:

  1. 增加父级元素的高度;
  2. 增加一个空的div标签,清除浮动
<div class = "clear"></div>
<style>
	.clear{
		clear:both;
		margin:0;
		padding:0;
}
</style>
  1. 在父级元素中增加一个overflow:hidden
overflow:hidden/*隐藏*/
overflow:scoll/*滚动*/
  1. 父类添加一个伪类:after
#father:after{
	content:'';
	display:block;
	clear:both;
}

小结:

浮动元素增加空div---->简单、代码尽量避免空div
设置父元素的高度----->简单,元素假设没有了固定的高度,就会超出
overflow---->简单,下拉的一些场景避免使用
父类添加一个伪类:after(推荐)---->写法稍微复杂,但是没有副作用,推荐使用
  1. display与float对比
  • display:方向不可以控制
  • float:浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题。
  1. 相对定位
相对定位:positon:relstive;
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留
top:-20px;
left:20px;
bottom:-10px;
right:20px;
  1. 绝对定位-absolute
定位:基于xxx定位,上下左右~
1、没有父级元素定位的前提下,相对于浏览器定位
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移
3、在父级元素范围内移动

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

<!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 #666;
            padding: 0;
            position: relative;
        }
        #first{
            background-color: #a13d30;
            border: 1px dashed #b27530;

        }
        #second{
            background-color: green;
            border: 1px dashed #0ece4f;
            position: absolute;
            right:30px;
            top:30px
        }
        #third{
            background-color: red;
            border: 1px dashed #ff1b87;
        }
    </style>
</head>
<body>
<div id = "father">
    <div id="first">第一个盒子</div>
    <div id="second">第二个盒子</div>
    <div id="third">第三个盒子</div>
</div>
</body>
</html>
  1. 固定定位-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>
  1. z-index:默认是0,最高无限~999
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css/style.css">

</head>
<body>
<div id="content">
    <ul>
        <li><img src="images/bg.jpg" alt=""></li>
        <li class="tipText">学习微服务,找狂神</li>
        <li class="tipBg"></li>
        <li>时间:2099-01=01</li>
        <li>地点:月球一号基地</li>
    </ul>
</div>
</body>
</html>
#content{
    width: 380;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px solid yellow;
}
ul,li{
    padding: 0px;
    margin: 0px;
    list-style: none;
}
/*父级元素相对定位*/
#content ul{
    position: relative;
}
.tipText,.tipBg{
    position: absolute;
    width: 380px;
    height: 25px;
    top:216px
}
.tipText{
    color: white;
    z-index: 999;
}
.tipBg{
    background: orange;
    opacity: 0.5;/*背景透明度*/
    filter: alpha(opacity=50);
}