行内式是在标记的style属性中设定CSS样式。不推荐大规模使用。css
<p style="color: red">Hello world.</p>
嵌入式是将CSS样式集中写在网页的<head></head>标签对的<style></style>标签对中。格式以下:html
<head> <meta charset="UTF-8"> <title>Title</title> <style> p{ background-color: #2b99ff; } </style> </head>
外部样式就是将css写在一个单独的文件中,而后在页面进行引入便可。推荐使用此方式。前端
<link href="mystyle.css" rel="stylesheet" type="text/css"/>
p {color: "red";}
#i1 { background-color: red; }
.c1 { font-size: 14px; } p.c1 { color: red; }
标签中的class属性若是有多个,要用空格分隔浏览器
* { color: white; }
/*li内部的a标签设置字体颜色*/ li a { color: green; }
/*选择全部父级是 <div> 元素的 <p> 元素*/ div>p { font-family: "Arial Black", arial-black, cursive; }
/*选择全部紧接着<div>元素以后的<p>元素*/ div+p { margin: 5px; }
/*i1后面全部的兄弟p标签*/ #i1~p { border: 2px solid royalblue; }
/*用于选取带有指定属性的元素。*/ p[title] { color: red; } /*用于选取带有指定属性和值的元素。*/ p[title="213"] { color: green; }
分组
当多个元素的样式相同的时候,咱们没有必要重复地为每一个元素都设置样式,咱们能够经过在多个选择器之间使用逗号分隔的分组选择器来统一设置元素样式。
例如:ide
div, p { color: red; }
.c1 p { color: red; }
同等选择器,越靠近标签的优先级越高(就近原则)性能
/* 鼠标移动到连接上 */ a:hover { color: #FF00FF } /*input输入框获取焦点时样式*/ input:focus { outline: none; background-color: #eee; }
first-letter
经常使用的给首字母设置特殊样式:字体
p:first-letter { font-size: 48px; color: red; }
beforecode
/*在每一个<p>元素以前插入内容*/ p:before { content:"*"; color:red; }
afterhtm
/*在每一个<p>元素以后插入内容*/ p:after { content:"[?]"; color:blue; }
before和after多用于清除浮动。blog
width属性能够为元素设置宽度。
height属性能够为元素设置高度。
块级标签才能设置宽度,内联标签的宽度由内容来决定。
文字字体、字体大小
body { font-family: "Microsoft Yahei", "微软雅黑", "Arial", sans-serif } p { font-size: 14px; }
若是设置成inherit表示继承父元素的字体大小值
字重(粗细)
font-weight用来设置字体的字重(粗细)。
值 | 描述 |
---|---|
bold | 粗体 |
文本颜色
颜色属性被用来设置文字的颜色。
颜色是经过CSS最常常的指定:
还有rgba(255,0,0,0.3),第四个值为alpha, 指定了色彩的透明度/不透明度,它的范围为0.0到1.0之间。
文字对齐
text-align 属性规定元素中的文本的水平对齐方式。
值 | 描述 |
---|---|
left | 左边对齐 默认值 |
right | 右对齐 |
center | 居中对齐 |
文字装饰
text-decoration 属性用来给文字添加特殊效果
经常使用的为去掉a标签默认的自划线:
a { text-decoration: none; }
首行缩进
将段落的第一行缩进 32像素:
p { text-indent: 32px; }
两倍于font-size的设置就是缩进两个字体
http://www.javashuo.com/article/p-xnrtvfmf-g.html
用这个属性能实现圆角边框的效果。
将border-radius设置为长或高的一半便可获得一个圆形。
.margin-test { margin-top:5px; margin-right:10px; margin-bottom:15px; margin-left:20px; }
推荐使用简写:
.margin-test { margin: 5px 10px 15px 20px; }
顺序:上右下左
常见居中:
.mycenter { margin: 0 auto; }
示例:设置一个列表
效果:
<style> /*取消ul标签前面的标志*/ ul { list-style-type: none; } /*把li标签转成行级标签*/ li { display: inline; } /*把a标签下划线取消,右边框设置1px实线红色,padding上下设置0,左右设置15px*/ li>a { text-decoration: none; border-right: 1px solid red; padding: 0 15px; } /*不显示最后一个li标签的右边框*/ li.last>a { border-right: none; } </style>
<ul> <li><a href="">大米商城</a></li> <li><a href="">小魅商城</a></li> <li><a href="">锤科商城</a></li> <li class="last"><a href="">华为商城</a></li> </ul>
在 CSS 中,任何元素均可以浮动。
浮动元素会生成一个块级框,而不论它自己是何种元素。
关于浮动的两个特色:
三种取值
left:向左浮动
right:向右浮动
none:默认值,不浮动
.c2 { float: left; height: 100px; width: 100px; }
clear属性规定元素的哪一侧不容许其余浮动元素。
值 | 描述 |
---|---|
left | 在左侧不容许浮动元素。 |
right | 在右侧不容许浮动元素。 |
both | 在左右两侧均不容许浮动元素。 |
clear属性只会对自身起做用,而不会影响其余元素。
举例:解决父标签塌陷,清除浮动
由于c1和c2都是浮动元素,因此撑不起父标签,此时利用伪元素添加一个元素,同事设置清除左侧浮动能够把父标签撑起来,这个伪元素自己不须要有高度
<div id="d1" class="clearfix"> <div class="c1">c1</div> <div class="c2">c2</div> </div>
.clearfix:after { content: ""; clear: left; display: block; }
值 | 描述 |
---|---|
visible | 默认值。内容不会被修剪,会呈如今元素框以外。 |
hidden | 内容会被修剪,而且其他内容是不可见的。 |
scroll | 内容会被修剪,可是浏览器会显示滚动条以便查看其他的内容。 |
auto | 若是内容被修剪,则浏览器会显示滚动条以便查看其他的内容。 |
inherit | 规定应该从父元素继承 overflow 属性的值。 |
圆形头像示例:
<style> .header-img { width: 120px; height: 120px; border: 2px solid silver; /*设置为原型*/ border-radius: 100%; /*若是溢出则隐藏*/ overflow: hidden; } .header-img>img { max-width: 100%; } </style>
<div class="header-img"> <img src="https://s4.51cto.com//oss/201901/03/d4ad6e4650cfe4f35e18a5c76d2af13f.jpg" alt=""> </div>
效果:
左 右 上 下
left right top bottom
示例:
返回顶部按钮样式示例
<div class="scrollTop">返回顶部</div>
<style> * { margin: 0; } .fixed-test { /*固定位置*/ position: fixed; /*用右和下定位*/ right: 20px; bottom: 20px; background-color: olivedrab; } </style>
脱离文档流的三种方式
float
absolute 绝对定位
fixed 固定定位
示例:自定义的模态框
<div class="c1"></div> <div class="c2"></div>
.c1 { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: rgba(0,0,0,0.5); z-index: 999; } .c2 { height: 400px; width: 600px; position: fixed; background-color: white; top: 50%; left: 50%; margin-top: -200px; margin-left: -300px; z-index: 1000; }
取值0~1
和rgba()的区别: