开始今日份整理css
CSS的导入方式主要是有内联模式,行内模式,外部样式表html
内联模式:直接在<head>中直接写css,例如前端
p{ color:rgb(166, 226, 226); } #设置P标签的颜色
行内模式:在html中对应元素直接书写浏览器
<p style="color:cadetblue">第一段 世界大势,合久必分,分久必合</p>
外部样式表,主要是有俩种,一种为连接式,一种为外联样式表ide
<link rel="stylesheet" href="index.css"> <!-- CSS2.1的样式 --> @import.url()
俩者的区别以下布局
css的选择器主要分基本选择器以及高级选择器学习
注:必定要有公共类的概念,不要试图用一个类完整一个网页,尽可能把类拆分出来,每一个类尽可能的小,让更多的标签去使用,同一个标签能够携带多个类,类与类之间用空格隔开便可。测试
基本应用字体
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>基础选择器</title> <style> /*标签选择器*/ body{ background: #b6bbb2; } div{ background: #747F8C; color: red; } /*id选择器*/ #h1{ color: #501c56; } /*class选择器*/ .c1{ background: #2d4ca2; color: #41db50; } </style> </head> <body> <h1 id="h1">id</h1> <div> <p>内容1</p> </div> <span class="c1">class1</span> <div> <p>内容2</p> </div> <span class="c1">class1</span> <span class="c1">class1</span> </body> </html>
后代选择器:外层的选择器写在前面,内层的选择器写在后面,之间用空格分隔,标签嵌套时,内层的标签成为外层的标签的后代,eg:url
div p{ background-color: rgb(33, 140, 228); } /* 意思就是div标签内的P标签的样式 */
注:这个在正常的前端学习中频繁使用。
儿子选择器,经过’>’链接在一块儿的,仅用于子元素,eg:
div>p{ color:red; } /* 意思就是div的子标签p的颜色为红色 */
并集选择器:多个链接器,经过逗号链接而成,同时声明多个分格相同的样式
eg:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>组合选择器</title> <style> #i1, #i2, #i3{ background: #2d4ca2; color: #41db50; } </style> </head> <body> <div id="i1">第一行</div> <div id="i2">第二行</div> <div id="i3">第三行</div> </body> </html>
交集选择器:由俩个选择器链接而成,第一个必须为标签选择器,第二个为类选择器或者是ID选择器
除了HTML元素的id,classy也可使用特定的属性来选择元素
input[type='text']{ width:100px; height:200px; },对选择到的标签再经过属性再进行一次筛选,选择type属性等于text的input标签
eg:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>属性选择器</title> <style> input[type="text"]{ width:100px; height: 200px; } </style> </head> <body> <input type="text"> <input type="password"> </body> </html>
CSS伪类是用来给选择器添加一些特殊效果
a:link(没有接触过的连接),用于定义了连接的常规状态
a:hover(鼠标放在连接上的状态),用于产生视觉效果
a:visited(访问过的连接),用于阅读文章,能清楚的判断已经访问过的连接
a:active(在连接上按下鼠标时的状态),用于表现鼠标按下时的连接状态
a:link {color: #FF0000} /* 未访问的连接 */
a:visited {color: #00FF00} /* 已访问的连接 */
a:hover {color: #FF00FF} /* 鼠标移动到连接上 */
a:active {color: #0000FF} /* 选定的连接 */ 格式: 标签:伪类名称{ css代码; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hover示例</title> <style> body{ margin: 0 auto; } a{ text-decoration: none; } .pg-header{ height: 38px; line-height: 38px; width: 100%; background-color: #5d48ff; } .w{ width: 980px; margin: 0 auto; } .pg-header .log{ color: #8c3d41; } .pg-header .menu{ display: inline-block; padding: 0 10px; color: white; } /*当鼠标移动到标签上时如下CSS才生效*/ .pg-header .menu:hover{ background-color: #501c56; } </style> </head> <body> <div class="pg-header"> <div class="w"> <a href="" class="logo">Logo</a> <a href="" class="menu">所有</a> <a href="" class="menu">42区段子</a> <a href="" class="menu">1024</a> </div> </div> </body> </html> hover实例
:focus input标签获取光标焦点
应用eg:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>focus</title> <style> .container{ width: 600px; height: 350px; margin: 0 auto; border: 1px solid red; } .form{ margin-top: 135px; text-align: center; } input.text{ margin-bottom: 10px; } input.text:focus{ color: #8c3d41; background-color: grey; } </style> </head> <body> <div class="container"> <span>搜索框</span> <div class="form"> <form action=""> <input type="text" class="text"> <br/> <input type="submit" value="搜索" class="submit"> </form> </div> </div> </body> </html>
选择器的优先级是按照选择器的权重来计算
选择器 权重
style="" 1000
#id 100
class 10
p 1
#注意:权重计算永远不进位
补充:
继承:给父级设置一些属性,子级继承了父级的该属性,这就是CSS中的继承,有些属性是能够继承的,只有color,font-*,text-*,line-*能够继承,像一些盒子元素,定位的元素(浮动,绝对定位,固定定位,border, margin, padding, background等)不能继承。
层叠:谁的权重大就会显示谁的样式。
text-align规定元素中的文本的水平对齐方式。
line-height: 标签高度 垂直方向根据标签高度居中
text-decoration:文字装饰
color:字体颜色
颜色属性被用来设置文字的颜色:
font-weight:字重属性
font-family:字体系列
font-family
能够把多个字体名称做为一个“回退”系统来保存。若是浏览器不支持第一个字体,则会尝试下一个。浏览器会使用它可识别的第一个值。eg:
body { font-family: "Microsoft Yahei", "微软雅黑", "Arial", sans-serif }
font-size:字体大小,设置像素大小,便可设置字体的大小
经常使用背景属性
在HTML中能够直接给body设置背景以下所示
<body background=图片文件名>
CSS中的背景设置以下:
1 background-color: cornflowerblue -> 指定背景颜色
2 background-image: url('1.jpg'); -> 指定背景图片
3 background-repeat: no-repeat;(repeat:平铺满) -> 指定背景重复方式(no-repeat;repeat-x;repeat-y)
4 background-position: right top(20px 20px); -> 指定背景位置(横向:left center right 纵向:top center bottom )
其余
经过使用 CSS 边框属性,咱们能够建立出效果出色的边框,而且能够应用于任何元素
边框设置:
1 border-style: solid;
2 border-color: red;
3 border-width: 10px;
4 简写:border: 10px red solid;
经常使用边框样式:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>边框</title> <style> body{ margin: 0 auto; } p{ margin-left: 30px; width: 300px; height: 100px; } .c1{ border: 3px solid red; } .c2{ border-top: 3px dashed green; border-bottom: 3px dashed red; border-left: 3px dotted #804761; border-right: 3px dotted blue; } </style> </head> <body> <p class="c1">这是一句话</p> <br> <p class="c2">最后一句话!</p> </body> </html>
效果以下
显示属性:设置元素如何被显示
(1)display分类
(2)块级元素和内联元素
block元素:
inline元素:
inline-block元素:
简单来讲就是将对象呈现为inline对象,可是对象的内容做为block对象呈现。以后的内联对象会被排列在同一行内。好比咱们能够给一个link(a元素)设置inline-block属性,使其既具备block的可设置宽度和高度特性又具备inline的同行特性
(1)块级标签与内联标签实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>块级标签和内联标签</title> <style> div, p, span{ width: 100px; height: 100px; } div{ background: red; } p{ background: #747F8C; } span{ background: #41db50; } </style> </head> <body> <div>div(块级标签) </div> <p>p(块级标签)</p> <span>span(内联标签)</span> </body> </html>
(2)块级标签和内联标签经过display转换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>块级标签和内联标签经过display转换</title> <style> .inline{ background: red; display: inline; } .block{ background: red; display: block; } </style> </head> <body> <!--将块级标签变成行内标签--> <div class="inline">div</div> <!--将行内标签变成块级标签--> <span class="block">span</span> </body> </html>
display:inline-block可作列表布局,其中的相似于图片间的间隙小bug能够经过以下设置解决:
1 #outer{ 2 border: 3px dashed; 3 word-spacing: -6px; 4 }
ul,ol{ list-style: none; /*去掉样式*/ list-style: circle; list-style: disc; list-style: upper-alpha; 、、、 }
(1)overflow -> 隐藏溢出
overflow: hidden; //超过范围隐藏
overflow: auto; //超出加滚动条
overflow: scroll; //无论如何都带滚动条
(2)下划线
1 /*清除a标签的下划线*/ 2 a{ 3 text-decoration: none; 4 }
(3)居中显示
针对block元素居中:
居中测试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>居中显示</title> <style> *{ margin: 0; padding: 0; } .content-box{ width: 100%; height: 800px; /*实际中能够不用设置*/ } .content{ margin: 0 auto; width: 800px; height: 800px; /*实际中能够不用设置*/ background-color: #747F8C; } </style> </head> <body> <div class="content-box"> <div class="content"></div> </div> </body> </html> 居中显示
针对inline元素和inline-block元素居中:设置父元素的text-align属性为center
(4)隐藏的两个方法
(5)透明度设置及层设置
opacity: 0.5; //设置透明度
z-index: 10; //设置层(只能做用于定位过的元素!)
(6)清除浏览器默认的margin和padding
*{
margin: 0;
padding: 0;
}
(7)内联标签position设置
若是内联标签position设置为absolute或relative,此时内联标签能够直接设置长和宽而不用再设置display为inline-block
补充:float属性,盒模型,position属性放在下一节单独整理。
https://www.cnblogs.com/gbq-dog/p/10629328.html
a